xref: /linux/drivers/gpu/drm/i915/display/intel_dp_mst.c (revision 17cfcb68af3bc7d5e8ae08779b1853310a2949f3)
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_helper.h>
27 #include <drm/drm_edid.h>
28 #include <drm/drm_probe_helper.h>
29 
30 #include "i915_drv.h"
31 #include "intel_atomic.h"
32 #include "intel_audio.h"
33 #include "intel_connector.h"
34 #include "intel_ddi.h"
35 #include "intel_display_types.h"
36 #include "intel_dp.h"
37 #include "intel_dp_mst.h"
38 #include "intel_dpio_phy.h"
39 
40 static int intel_dp_mst_compute_link_config(struct intel_encoder *encoder,
41 					    struct intel_crtc_state *crtc_state,
42 					    struct drm_connector_state *conn_state,
43 					    struct link_config_limits *limits)
44 {
45 	struct drm_atomic_state *state = crtc_state->base.state;
46 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
47 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
48 	struct intel_connector *connector =
49 		to_intel_connector(conn_state->connector);
50 	const struct drm_display_mode *adjusted_mode =
51 		&crtc_state->base.adjusted_mode;
52 	void *port = connector->port;
53 	bool constant_n = drm_dp_has_quirk(&intel_dp->desc,
54 					   DP_DPCD_QUIRK_CONSTANT_N);
55 	int bpp, slots = -EINVAL;
56 
57 	crtc_state->lane_count = limits->max_lane_count;
58 	crtc_state->port_clock = limits->max_clock;
59 
60 	for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
61 		crtc_state->pipe_bpp = bpp;
62 
63 		crtc_state->pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock,
64 						       crtc_state->pipe_bpp);
65 
66 		slots = drm_dp_atomic_find_vcpi_slots(state, &intel_dp->mst_mgr,
67 						      port, crtc_state->pbn);
68 		if (slots == -EDEADLK)
69 			return slots;
70 		if (slots >= 0)
71 			break;
72 	}
73 
74 	if (slots < 0) {
75 		DRM_DEBUG_KMS("failed finding vcpi slots:%d\n", slots);
76 		return slots;
77 	}
78 
79 	intel_link_compute_m_n(crtc_state->pipe_bpp,
80 			       crtc_state->lane_count,
81 			       adjusted_mode->crtc_clock,
82 			       crtc_state->port_clock,
83 			       &crtc_state->dp_m_n,
84 			       constant_n, crtc_state->fec_enable);
85 	crtc_state->dp_m_n.tu = slots;
86 
87 	return 0;
88 }
89 
90 static int intel_dp_mst_compute_config(struct intel_encoder *encoder,
91 				       struct intel_crtc_state *pipe_config,
92 				       struct drm_connector_state *conn_state)
93 {
94 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
95 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
96 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
97 	struct intel_connector *connector =
98 		to_intel_connector(conn_state->connector);
99 	struct intel_digital_connector_state *intel_conn_state =
100 		to_intel_digital_connector_state(conn_state);
101 	const struct drm_display_mode *adjusted_mode =
102 		&pipe_config->base.adjusted_mode;
103 	void *port = connector->port;
104 	struct link_config_limits limits;
105 	int ret;
106 
107 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
108 		return -EINVAL;
109 
110 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
111 	pipe_config->has_pch_encoder = false;
112 
113 	if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
114 		pipe_config->has_audio =
115 			drm_dp_mst_port_has_audio(&intel_dp->mst_mgr, port);
116 	else
117 		pipe_config->has_audio =
118 			intel_conn_state->force_audio == HDMI_AUDIO_ON;
119 
120 	/*
121 	 * for MST we always configure max link bw - the spec doesn't
122 	 * seem to suggest we should do otherwise.
123 	 */
124 	limits.min_clock =
125 	limits.max_clock = intel_dp_max_link_rate(intel_dp);
126 
127 	limits.min_lane_count =
128 	limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
129 
130 	limits.min_bpp = intel_dp_min_bpp(pipe_config);
131 	/*
132 	 * FIXME: If all the streams can't fit into the link with
133 	 * their current pipe_bpp we should reduce pipe_bpp across
134 	 * the board until things start to fit. Until then we
135 	 * limit to <= 8bpc since that's what was hardcoded for all
136 	 * MST streams previously. This hack should be removed once
137 	 * we have the proper retry logic in place.
138 	 */
139 	limits.max_bpp = min(pipe_config->pipe_bpp, 24);
140 
141 	intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
142 
143 	ret = intel_dp_mst_compute_link_config(encoder, pipe_config,
144 					       conn_state, &limits);
145 	if (ret)
146 		return ret;
147 
148 	pipe_config->limited_color_range =
149 		intel_dp_limited_color_range(pipe_config, conn_state);
150 
151 	if (IS_GEN9_LP(dev_priv))
152 		pipe_config->lane_lat_optim_mask =
153 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
154 
155 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
156 
157 	return 0;
158 }
159 
160 static int
161 intel_dp_mst_atomic_check(struct drm_connector *connector,
162 			  struct drm_atomic_state *state)
163 {
164 	struct drm_connector_state *new_conn_state =
165 		drm_atomic_get_new_connector_state(state, connector);
166 	struct drm_connector_state *old_conn_state =
167 		drm_atomic_get_old_connector_state(state, connector);
168 	struct intel_connector *intel_connector =
169 		to_intel_connector(connector);
170 	struct drm_crtc *new_crtc = new_conn_state->crtc;
171 	struct drm_crtc_state *crtc_state;
172 	struct drm_dp_mst_topology_mgr *mgr;
173 	int ret;
174 
175 	ret = intel_digital_connector_atomic_check(connector, state);
176 	if (ret)
177 		return ret;
178 
179 	if (!old_conn_state->crtc)
180 		return 0;
181 
182 	/* We only want to free VCPI if this state disables the CRTC on this
183 	 * connector
184 	 */
185 	if (new_crtc) {
186 		crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
187 
188 		if (!crtc_state ||
189 		    !drm_atomic_crtc_needs_modeset(crtc_state) ||
190 		    crtc_state->enable)
191 			return 0;
192 	}
193 
194 	mgr = &enc_to_mst(old_conn_state->best_encoder)->primary->dp.mst_mgr;
195 	ret = drm_dp_atomic_release_vcpi_slots(state, mgr,
196 					       intel_connector->port);
197 
198 	return ret;
199 }
200 
201 static void intel_mst_disable_dp(struct intel_encoder *encoder,
202 				 const struct intel_crtc_state *old_crtc_state,
203 				 const struct drm_connector_state *old_conn_state)
204 {
205 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
206 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
207 	struct intel_dp *intel_dp = &intel_dig_port->dp;
208 	struct intel_connector *connector =
209 		to_intel_connector(old_conn_state->connector);
210 	int ret;
211 
212 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
213 
214 	drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, connector->port);
215 
216 	ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
217 	if (ret) {
218 		DRM_DEBUG_KMS("failed to update payload %d\n", ret);
219 	}
220 	if (old_crtc_state->has_audio)
221 		intel_audio_codec_disable(encoder,
222 					  old_crtc_state, old_conn_state);
223 }
224 
225 static void intel_mst_post_disable_dp(struct intel_encoder *encoder,
226 				      const struct intel_crtc_state *old_crtc_state,
227 				      const struct drm_connector_state *old_conn_state)
228 {
229 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
230 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
231 	struct intel_dp *intel_dp = &intel_dig_port->dp;
232 	struct intel_connector *connector =
233 		to_intel_connector(old_conn_state->connector);
234 
235 	intel_ddi_disable_pipe_clock(old_crtc_state);
236 
237 	/* this can fail */
238 	drm_dp_check_act_status(&intel_dp->mst_mgr);
239 	/* and this can also fail */
240 	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
241 
242 	drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
243 
244 	/*
245 	 * Power down mst path before disabling the port, otherwise we end
246 	 * up getting interrupts from the sink upon detecting link loss.
247 	 */
248 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
249 				     false);
250 
251 	intel_dp->active_mst_links--;
252 
253 	intel_mst->connector = NULL;
254 	if (intel_dp->active_mst_links == 0) {
255 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
256 		intel_dig_port->base.post_disable(&intel_dig_port->base,
257 						  old_crtc_state, NULL);
258 	}
259 
260 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
261 }
262 
263 static void intel_mst_pre_pll_enable_dp(struct intel_encoder *encoder,
264 					const struct intel_crtc_state *pipe_config,
265 					const struct drm_connector_state *conn_state)
266 {
267 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
268 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
269 	struct intel_dp *intel_dp = &intel_dig_port->dp;
270 
271 	if (intel_dp->active_mst_links == 0)
272 		intel_dig_port->base.pre_pll_enable(&intel_dig_port->base,
273 						    pipe_config, NULL);
274 }
275 
276 static void intel_mst_post_pll_disable_dp(struct intel_encoder *encoder,
277 					  const struct intel_crtc_state *old_crtc_state,
278 					  const struct drm_connector_state *old_conn_state)
279 {
280 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
281 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
282 	struct intel_dp *intel_dp = &intel_dig_port->dp;
283 
284 	if (intel_dp->active_mst_links == 0)
285 		intel_dig_port->base.post_pll_disable(&intel_dig_port->base,
286 						      old_crtc_state,
287 						      old_conn_state);
288 }
289 
290 static void intel_mst_pre_enable_dp(struct intel_encoder *encoder,
291 				    const struct intel_crtc_state *pipe_config,
292 				    const struct drm_connector_state *conn_state)
293 {
294 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
295 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
296 	struct intel_dp *intel_dp = &intel_dig_port->dp;
297 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
298 	struct intel_connector *connector =
299 		to_intel_connector(conn_state->connector);
300 	int ret;
301 	u32 temp;
302 
303 	/* MST encoders are bound to a crtc, not to a connector,
304 	 * force the mapping here for get_hw_state.
305 	 */
306 	connector->encoder = encoder;
307 	intel_mst->connector = connector;
308 
309 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
310 
311 	if (intel_dp->active_mst_links == 0)
312 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
313 
314 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
315 
316 	if (intel_dp->active_mst_links == 0)
317 		intel_dig_port->base.pre_enable(&intel_dig_port->base,
318 						pipe_config, NULL);
319 
320 	ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
321 				       connector->port,
322 				       pipe_config->pbn,
323 				       pipe_config->dp_m_n.tu);
324 	if (!ret)
325 		DRM_ERROR("failed to allocate vcpi\n");
326 
327 	intel_dp->active_mst_links++;
328 	temp = I915_READ(intel_dp->regs.dp_tp_status);
329 	I915_WRITE(intel_dp->regs.dp_tp_status, temp);
330 
331 	ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
332 
333 	intel_ddi_enable_pipe_clock(pipe_config);
334 }
335 
336 static void intel_mst_enable_dp(struct intel_encoder *encoder,
337 				const struct intel_crtc_state *pipe_config,
338 				const struct drm_connector_state *conn_state)
339 {
340 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
341 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
342 	struct intel_dp *intel_dp = &intel_dig_port->dp;
343 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
344 
345 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
346 
347 	if (intel_de_wait_for_set(dev_priv, intel_dp->regs.dp_tp_status,
348 				  DP_TP_STATUS_ACT_SENT, 1))
349 		DRM_ERROR("Timed out waiting for ACT sent\n");
350 
351 	drm_dp_check_act_status(&intel_dp->mst_mgr);
352 
353 	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
354 	if (pipe_config->has_audio)
355 		intel_audio_codec_enable(encoder, pipe_config, conn_state);
356 }
357 
358 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
359 				      enum pipe *pipe)
360 {
361 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
362 	*pipe = intel_mst->pipe;
363 	if (intel_mst->connector)
364 		return true;
365 	return false;
366 }
367 
368 static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
369 					struct intel_crtc_state *pipe_config)
370 {
371 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
372 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
373 
374 	intel_ddi_get_config(&intel_dig_port->base, pipe_config);
375 }
376 
377 static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
378 {
379 	struct intel_connector *intel_connector = to_intel_connector(connector);
380 	struct intel_dp *intel_dp = intel_connector->mst_port;
381 	struct edid *edid;
382 	int ret;
383 
384 	if (drm_connector_is_unregistered(connector))
385 		return intel_connector_update_modes(connector, NULL);
386 
387 	edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
388 	ret = intel_connector_update_modes(connector, edid);
389 	kfree(edid);
390 
391 	return ret;
392 }
393 
394 static enum drm_connector_status
395 intel_dp_mst_detect(struct drm_connector *connector, bool force)
396 {
397 	struct intel_connector *intel_connector = to_intel_connector(connector);
398 	struct intel_dp *intel_dp = intel_connector->mst_port;
399 
400 	if (drm_connector_is_unregistered(connector))
401 		return connector_status_disconnected;
402 	return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr,
403 				      intel_connector->port);
404 }
405 
406 static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
407 	.detect = intel_dp_mst_detect,
408 	.fill_modes = drm_helper_probe_single_connector_modes,
409 	.atomic_get_property = intel_digital_connector_atomic_get_property,
410 	.atomic_set_property = intel_digital_connector_atomic_set_property,
411 	.late_register = intel_connector_register,
412 	.early_unregister = intel_connector_unregister,
413 	.destroy = intel_connector_destroy,
414 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
415 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
416 };
417 
418 static int intel_dp_mst_get_modes(struct drm_connector *connector)
419 {
420 	return intel_dp_mst_get_ddc_modes(connector);
421 }
422 
423 static enum drm_mode_status
424 intel_dp_mst_mode_valid(struct drm_connector *connector,
425 			struct drm_display_mode *mode)
426 {
427 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
428 	struct intel_connector *intel_connector = to_intel_connector(connector);
429 	struct intel_dp *intel_dp = intel_connector->mst_port;
430 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
431 	int max_rate, mode_rate, max_lanes, max_link_clock;
432 
433 	if (drm_connector_is_unregistered(connector))
434 		return MODE_ERROR;
435 
436 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
437 		return MODE_NO_DBLESCAN;
438 
439 	max_link_clock = intel_dp_max_link_rate(intel_dp);
440 	max_lanes = intel_dp_max_lane_count(intel_dp);
441 
442 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
443 	mode_rate = intel_dp_link_required(mode->clock, 18);
444 
445 	/* TODO - validate mode against available PBN for link */
446 	if (mode->clock < 10000)
447 		return MODE_CLOCK_LOW;
448 
449 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
450 		return MODE_H_ILLEGAL;
451 
452 	if (mode_rate > max_rate || mode->clock > max_dotclk)
453 		return MODE_CLOCK_HIGH;
454 
455 	return intel_mode_valid_max_plane_size(dev_priv, mode);
456 }
457 
458 static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
459 							 struct drm_connector_state *state)
460 {
461 	struct intel_connector *intel_connector = to_intel_connector(connector);
462 	struct intel_dp *intel_dp = intel_connector->mst_port;
463 	struct intel_crtc *crtc = to_intel_crtc(state->crtc);
464 
465 	return &intel_dp->mst_encoders[crtc->pipe]->base.base;
466 }
467 
468 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
469 	.get_modes = intel_dp_mst_get_modes,
470 	.mode_valid = intel_dp_mst_mode_valid,
471 	.atomic_best_encoder = intel_mst_atomic_best_encoder,
472 	.atomic_check = intel_dp_mst_atomic_check,
473 };
474 
475 static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
476 {
477 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
478 
479 	drm_encoder_cleanup(encoder);
480 	kfree(intel_mst);
481 }
482 
483 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
484 	.destroy = intel_dp_mst_encoder_destroy,
485 };
486 
487 static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
488 {
489 	if (connector->encoder && connector->base.state->crtc) {
490 		enum pipe pipe;
491 		if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
492 			return false;
493 		return true;
494 	}
495 	return false;
496 }
497 
498 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)
499 {
500 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
501 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
502 	struct drm_device *dev = intel_dig_port->base.base.dev;
503 	struct drm_i915_private *dev_priv = to_i915(dev);
504 	struct intel_connector *intel_connector;
505 	struct drm_connector *connector;
506 	enum pipe pipe;
507 	int ret;
508 
509 	intel_connector = intel_connector_alloc();
510 	if (!intel_connector)
511 		return NULL;
512 
513 	intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
514 	intel_connector->mst_port = intel_dp;
515 	intel_connector->port = port;
516 	drm_dp_mst_get_port_malloc(port);
517 
518 	connector = &intel_connector->base;
519 	ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
520 				 DRM_MODE_CONNECTOR_DisplayPort);
521 	if (ret) {
522 		intel_connector_free(intel_connector);
523 		return NULL;
524 	}
525 
526 	drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
527 
528 	for_each_pipe(dev_priv, pipe) {
529 		struct drm_encoder *enc =
530 			&intel_dp->mst_encoders[pipe]->base.base;
531 
532 		ret = drm_connector_attach_encoder(&intel_connector->base, enc);
533 		if (ret)
534 			goto err;
535 	}
536 
537 	drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
538 	drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
539 
540 	ret = drm_connector_set_path_property(connector, pathprop);
541 	if (ret)
542 		goto err;
543 
544 	intel_attach_force_audio_property(connector);
545 	intel_attach_broadcast_rgb_property(connector);
546 
547 	/*
548 	 * Reuse the prop from the SST connector because we're
549 	 * not allowed to create new props after device registration.
550 	 */
551 	connector->max_bpc_property =
552 		intel_dp->attached_connector->base.max_bpc_property;
553 	if (connector->max_bpc_property)
554 		drm_connector_attach_max_bpc_property(connector, 6, 12);
555 
556 	return connector;
557 
558 err:
559 	drm_connector_cleanup(connector);
560 	return NULL;
561 }
562 
563 static void intel_dp_register_mst_connector(struct drm_connector *connector)
564 {
565 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
566 
567 	if (dev_priv->fbdev)
568 		drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
569 						connector);
570 
571 	drm_connector_register(connector);
572 }
573 
574 static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
575 					   struct drm_connector *connector)
576 {
577 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
578 
579 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, connector->name);
580 	drm_connector_unregister(connector);
581 
582 	if (dev_priv->fbdev)
583 		drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
584 						   connector);
585 
586 	drm_connector_put(connector);
587 }
588 
589 static const struct drm_dp_mst_topology_cbs mst_cbs = {
590 	.add_connector = intel_dp_add_mst_connector,
591 	.register_connector = intel_dp_register_mst_connector,
592 	.destroy_connector = intel_dp_destroy_mst_connector,
593 };
594 
595 static struct intel_dp_mst_encoder *
596 intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
597 {
598 	struct intel_dp_mst_encoder *intel_mst;
599 	struct intel_encoder *intel_encoder;
600 	struct drm_device *dev = intel_dig_port->base.base.dev;
601 	struct drm_i915_private *dev_priv = to_i915(dev);
602 	enum pipe pipe_iter;
603 
604 	intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
605 
606 	if (!intel_mst)
607 		return NULL;
608 
609 	intel_mst->pipe = pipe;
610 	intel_encoder = &intel_mst->base;
611 	intel_mst->primary = intel_dig_port;
612 
613 	drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
614 			 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
615 
616 	intel_encoder->type = INTEL_OUTPUT_DP_MST;
617 	intel_encoder->power_domain = intel_dig_port->base.power_domain;
618 	intel_encoder->port = intel_dig_port->base.port;
619 	intel_encoder->cloneable = 0;
620 	for_each_pipe(dev_priv, pipe_iter)
621 		intel_encoder->crtc_mask |= BIT(pipe_iter);
622 
623 	intel_encoder->compute_config = intel_dp_mst_compute_config;
624 	intel_encoder->disable = intel_mst_disable_dp;
625 	intel_encoder->post_disable = intel_mst_post_disable_dp;
626 	intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
627 	intel_encoder->post_pll_disable = intel_mst_post_pll_disable_dp;
628 	intel_encoder->pre_enable = intel_mst_pre_enable_dp;
629 	intel_encoder->enable = intel_mst_enable_dp;
630 	intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
631 	intel_encoder->get_config = intel_dp_mst_enc_get_config;
632 
633 	return intel_mst;
634 
635 }
636 
637 static bool
638 intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
639 {
640 	struct intel_dp *intel_dp = &intel_dig_port->dp;
641 	struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
642 	enum pipe pipe;
643 
644 	for_each_pipe(dev_priv, pipe)
645 		intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(intel_dig_port, pipe);
646 	return true;
647 }
648 
649 int
650 intel_dp_mst_encoder_active_links(struct intel_digital_port *intel_dig_port)
651 {
652 	return intel_dig_port->dp.active_mst_links;
653 }
654 
655 int
656 intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
657 {
658 	struct drm_i915_private *i915 = to_i915(intel_dig_port->base.base.dev);
659 	struct intel_dp *intel_dp = &intel_dig_port->dp;
660 	enum port port = intel_dig_port->base.port;
661 	int ret;
662 
663 	if (!HAS_DP_MST(i915) || intel_dp_is_edp(intel_dp))
664 		return 0;
665 
666 	if (INTEL_GEN(i915) < 12 && port == PORT_A)
667 		return 0;
668 
669 	if (INTEL_GEN(i915) < 11 && port == PORT_E)
670 		return 0;
671 
672 	intel_dp->mst_mgr.cbs = &mst_cbs;
673 
674 	/* create encoders */
675 	intel_dp_create_fake_mst_encoders(intel_dig_port);
676 	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, &i915->drm,
677 					   &intel_dp->aux, 16, 3, conn_base_id);
678 	if (ret)
679 		return ret;
680 
681 	intel_dp->can_mst = true;
682 
683 	return 0;
684 }
685 
686 void
687 intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
688 {
689 	struct intel_dp *intel_dp = &intel_dig_port->dp;
690 
691 	if (!intel_dp->can_mst)
692 		return;
693 
694 	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
695 	/* encoders will get killed by normal cleanup */
696 }
697