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);
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 	limits.max_bpp = pipe_config->pipe_bpp;
132 
133 	intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
134 
135 	ret = intel_dp_mst_compute_link_config(encoder, pipe_config,
136 					       conn_state, &limits);
137 	if (ret)
138 		return ret;
139 
140 	pipe_config->limited_color_range =
141 		intel_dp_limited_color_range(pipe_config, conn_state);
142 
143 	if (IS_GEN9_LP(dev_priv))
144 		pipe_config->lane_lat_optim_mask =
145 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
146 
147 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
148 
149 	return 0;
150 }
151 
152 static int
153 intel_dp_mst_atomic_check(struct drm_connector *connector,
154 			  struct drm_atomic_state *state)
155 {
156 	struct drm_connector_state *new_conn_state =
157 		drm_atomic_get_new_connector_state(state, connector);
158 	struct drm_connector_state *old_conn_state =
159 		drm_atomic_get_old_connector_state(state, connector);
160 	struct intel_connector *intel_connector =
161 		to_intel_connector(connector);
162 	struct drm_crtc *new_crtc = new_conn_state->crtc;
163 	struct drm_crtc_state *crtc_state;
164 	struct drm_dp_mst_topology_mgr *mgr;
165 	int ret;
166 
167 	ret = intel_digital_connector_atomic_check(connector, state);
168 	if (ret)
169 		return ret;
170 
171 	if (!old_conn_state->crtc)
172 		return 0;
173 
174 	/* We only want to free VCPI if this state disables the CRTC on this
175 	 * connector
176 	 */
177 	if (new_crtc) {
178 		crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
179 
180 		if (!crtc_state ||
181 		    !drm_atomic_crtc_needs_modeset(crtc_state) ||
182 		    crtc_state->enable)
183 			return 0;
184 	}
185 
186 	mgr = &enc_to_mst(old_conn_state->best_encoder)->primary->dp.mst_mgr;
187 	ret = drm_dp_atomic_release_vcpi_slots(state, mgr,
188 					       intel_connector->port);
189 
190 	return ret;
191 }
192 
193 static void intel_mst_disable_dp(struct intel_encoder *encoder,
194 				 const struct intel_crtc_state *old_crtc_state,
195 				 const struct drm_connector_state *old_conn_state)
196 {
197 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
198 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
199 	struct intel_dp *intel_dp = &intel_dig_port->dp;
200 	struct intel_connector *connector =
201 		to_intel_connector(old_conn_state->connector);
202 	int ret;
203 
204 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
205 
206 	drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, connector->port);
207 
208 	ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
209 	if (ret) {
210 		DRM_ERROR("failed to update payload %d\n", ret);
211 	}
212 	if (old_crtc_state->has_audio)
213 		intel_audio_codec_disable(encoder,
214 					  old_crtc_state, old_conn_state);
215 }
216 
217 static void intel_mst_post_disable_dp(struct intel_encoder *encoder,
218 				      const struct intel_crtc_state *old_crtc_state,
219 				      const struct drm_connector_state *old_conn_state)
220 {
221 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
222 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
223 	struct intel_dp *intel_dp = &intel_dig_port->dp;
224 	struct intel_connector *connector =
225 		to_intel_connector(old_conn_state->connector);
226 
227 	intel_ddi_disable_pipe_clock(old_crtc_state);
228 
229 	/* this can fail */
230 	drm_dp_check_act_status(&intel_dp->mst_mgr);
231 	/* and this can also fail */
232 	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
233 
234 	drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
235 
236 	/*
237 	 * Power down mst path before disabling the port, otherwise we end
238 	 * up getting interrupts from the sink upon detecting link loss.
239 	 */
240 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
241 				     false);
242 
243 	intel_dp->active_mst_links--;
244 
245 	intel_mst->connector = NULL;
246 	if (intel_dp->active_mst_links == 0) {
247 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
248 		intel_dig_port->base.post_disable(&intel_dig_port->base,
249 						  old_crtc_state, NULL);
250 	}
251 
252 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
253 }
254 
255 static void intel_mst_pre_pll_enable_dp(struct intel_encoder *encoder,
256 					const struct intel_crtc_state *pipe_config,
257 					const struct drm_connector_state *conn_state)
258 {
259 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
260 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
261 	struct intel_dp *intel_dp = &intel_dig_port->dp;
262 
263 	if (intel_dp->active_mst_links == 0)
264 		intel_dig_port->base.pre_pll_enable(&intel_dig_port->base,
265 						    pipe_config, NULL);
266 }
267 
268 static void intel_mst_post_pll_disable_dp(struct intel_encoder *encoder,
269 					  const struct intel_crtc_state *old_crtc_state,
270 					  const struct drm_connector_state *old_conn_state)
271 {
272 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
273 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
274 	struct intel_dp *intel_dp = &intel_dig_port->dp;
275 
276 	if (intel_dp->active_mst_links == 0)
277 		intel_dig_port->base.post_pll_disable(&intel_dig_port->base,
278 						      old_crtc_state,
279 						      old_conn_state);
280 }
281 
282 static void intel_mst_pre_enable_dp(struct intel_encoder *encoder,
283 				    const struct intel_crtc_state *pipe_config,
284 				    const struct drm_connector_state *conn_state)
285 {
286 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
287 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
288 	struct intel_dp *intel_dp = &intel_dig_port->dp;
289 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
290 	enum port port = intel_dig_port->base.port;
291 	struct intel_connector *connector =
292 		to_intel_connector(conn_state->connector);
293 	int ret;
294 	u32 temp;
295 
296 	/* MST encoders are bound to a crtc, not to a connector,
297 	 * force the mapping here for get_hw_state.
298 	 */
299 	connector->encoder = encoder;
300 	intel_mst->connector = connector;
301 
302 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
303 
304 	if (intel_dp->active_mst_links == 0)
305 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
306 
307 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
308 
309 	if (intel_dp->active_mst_links == 0)
310 		intel_dig_port->base.pre_enable(&intel_dig_port->base,
311 						pipe_config, NULL);
312 
313 	ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
314 				       connector->port,
315 				       pipe_config->pbn,
316 				       pipe_config->dp_m_n.tu);
317 	if (!ret)
318 		DRM_ERROR("failed to allocate vcpi\n");
319 
320 	intel_dp->active_mst_links++;
321 	temp = I915_READ(DP_TP_STATUS(port));
322 	I915_WRITE(DP_TP_STATUS(port), temp);
323 
324 	ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
325 
326 	intel_ddi_enable_pipe_clock(pipe_config);
327 }
328 
329 static void intel_mst_enable_dp(struct intel_encoder *encoder,
330 				const struct intel_crtc_state *pipe_config,
331 				const struct drm_connector_state *conn_state)
332 {
333 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
334 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
335 	struct intel_dp *intel_dp = &intel_dig_port->dp;
336 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
337 	enum port port = intel_dig_port->base.port;
338 
339 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
340 
341 	if (intel_de_wait_for_set(dev_priv, DP_TP_STATUS(port),
342 				  DP_TP_STATUS_ACT_SENT, 1))
343 		DRM_ERROR("Timed out waiting for ACT sent\n");
344 
345 	drm_dp_check_act_status(&intel_dp->mst_mgr);
346 
347 	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
348 	if (pipe_config->has_audio)
349 		intel_audio_codec_enable(encoder, pipe_config, conn_state);
350 }
351 
352 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
353 				      enum pipe *pipe)
354 {
355 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
356 	*pipe = intel_mst->pipe;
357 	if (intel_mst->connector)
358 		return true;
359 	return false;
360 }
361 
362 static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
363 					struct intel_crtc_state *pipe_config)
364 {
365 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
366 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
367 
368 	intel_ddi_get_config(&intel_dig_port->base, pipe_config);
369 }
370 
371 static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
372 {
373 	struct intel_connector *intel_connector = to_intel_connector(connector);
374 	struct intel_dp *intel_dp = intel_connector->mst_port;
375 	struct edid *edid;
376 	int ret;
377 
378 	if (drm_connector_is_unregistered(connector))
379 		return intel_connector_update_modes(connector, NULL);
380 
381 	edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
382 	ret = intel_connector_update_modes(connector, edid);
383 	kfree(edid);
384 
385 	return ret;
386 }
387 
388 static enum drm_connector_status
389 intel_dp_mst_detect(struct drm_connector *connector, bool force)
390 {
391 	struct intel_connector *intel_connector = to_intel_connector(connector);
392 	struct intel_dp *intel_dp = intel_connector->mst_port;
393 
394 	if (drm_connector_is_unregistered(connector))
395 		return connector_status_disconnected;
396 	return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr,
397 				      intel_connector->port);
398 }
399 
400 static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
401 	.detect = intel_dp_mst_detect,
402 	.fill_modes = drm_helper_probe_single_connector_modes,
403 	.atomic_get_property = intel_digital_connector_atomic_get_property,
404 	.atomic_set_property = intel_digital_connector_atomic_set_property,
405 	.late_register = intel_connector_register,
406 	.early_unregister = intel_connector_unregister,
407 	.destroy = intel_connector_destroy,
408 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
409 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
410 };
411 
412 static int intel_dp_mst_get_modes(struct drm_connector *connector)
413 {
414 	return intel_dp_mst_get_ddc_modes(connector);
415 }
416 
417 static enum drm_mode_status
418 intel_dp_mst_mode_valid(struct drm_connector *connector,
419 			struct drm_display_mode *mode)
420 {
421 	struct intel_connector *intel_connector = to_intel_connector(connector);
422 	struct intel_dp *intel_dp = intel_connector->mst_port;
423 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
424 	int max_rate, mode_rate, max_lanes, max_link_clock;
425 
426 	if (drm_connector_is_unregistered(connector))
427 		return MODE_ERROR;
428 
429 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
430 		return MODE_NO_DBLESCAN;
431 
432 	max_link_clock = intel_dp_max_link_rate(intel_dp);
433 	max_lanes = intel_dp_max_lane_count(intel_dp);
434 
435 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
436 	mode_rate = intel_dp_link_required(mode->clock, 18);
437 
438 	/* TODO - validate mode against available PBN for link */
439 	if (mode->clock < 10000)
440 		return MODE_CLOCK_LOW;
441 
442 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
443 		return MODE_H_ILLEGAL;
444 
445 	if (mode_rate > max_rate || mode->clock > max_dotclk)
446 		return MODE_CLOCK_HIGH;
447 
448 	return MODE_OK;
449 }
450 
451 static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
452 							 struct drm_connector_state *state)
453 {
454 	struct intel_connector *intel_connector = to_intel_connector(connector);
455 	struct intel_dp *intel_dp = intel_connector->mst_port;
456 	struct intel_crtc *crtc = to_intel_crtc(state->crtc);
457 
458 	return &intel_dp->mst_encoders[crtc->pipe]->base.base;
459 }
460 
461 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
462 	.get_modes = intel_dp_mst_get_modes,
463 	.mode_valid = intel_dp_mst_mode_valid,
464 	.atomic_best_encoder = intel_mst_atomic_best_encoder,
465 	.atomic_check = intel_dp_mst_atomic_check,
466 };
467 
468 static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
469 {
470 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
471 
472 	drm_encoder_cleanup(encoder);
473 	kfree(intel_mst);
474 }
475 
476 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
477 	.destroy = intel_dp_mst_encoder_destroy,
478 };
479 
480 static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
481 {
482 	if (connector->encoder && connector->base.state->crtc) {
483 		enum pipe pipe;
484 		if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
485 			return false;
486 		return true;
487 	}
488 	return false;
489 }
490 
491 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)
492 {
493 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
494 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
495 	struct drm_device *dev = intel_dig_port->base.base.dev;
496 	struct drm_i915_private *dev_priv = to_i915(dev);
497 	struct intel_connector *intel_connector;
498 	struct drm_connector *connector;
499 	enum pipe pipe;
500 	int ret;
501 
502 	intel_connector = intel_connector_alloc();
503 	if (!intel_connector)
504 		return NULL;
505 
506 	intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
507 	intel_connector->mst_port = intel_dp;
508 	intel_connector->port = port;
509 	drm_dp_mst_get_port_malloc(port);
510 
511 	connector = &intel_connector->base;
512 	ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
513 				 DRM_MODE_CONNECTOR_DisplayPort);
514 	if (ret) {
515 		intel_connector_free(intel_connector);
516 		return NULL;
517 	}
518 
519 	drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
520 
521 	for_each_pipe(dev_priv, pipe) {
522 		struct drm_encoder *enc =
523 			&intel_dp->mst_encoders[pipe]->base.base;
524 
525 		ret = drm_connector_attach_encoder(&intel_connector->base, enc);
526 		if (ret)
527 			goto err;
528 	}
529 
530 	drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
531 	drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
532 
533 	ret = drm_connector_set_path_property(connector, pathprop);
534 	if (ret)
535 		goto err;
536 
537 	intel_attach_force_audio_property(connector);
538 	intel_attach_broadcast_rgb_property(connector);
539 
540 	/*
541 	 * Reuse the prop from the SST connector because we're
542 	 * not allowed to create new props after device registration.
543 	 */
544 	connector->max_bpc_property =
545 		intel_dp->attached_connector->base.max_bpc_property;
546 	if (connector->max_bpc_property)
547 		drm_connector_attach_max_bpc_property(connector, 6, 12);
548 
549 	return connector;
550 
551 err:
552 	drm_connector_cleanup(connector);
553 	return NULL;
554 }
555 
556 static void intel_dp_register_mst_connector(struct drm_connector *connector)
557 {
558 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
559 
560 	if (dev_priv->fbdev)
561 		drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
562 						connector);
563 
564 	drm_connector_register(connector);
565 }
566 
567 static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
568 					   struct drm_connector *connector)
569 {
570 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
571 
572 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, connector->name);
573 	drm_connector_unregister(connector);
574 
575 	if (dev_priv->fbdev)
576 		drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
577 						   connector);
578 
579 	drm_connector_put(connector);
580 }
581 
582 static const struct drm_dp_mst_topology_cbs mst_cbs = {
583 	.add_connector = intel_dp_add_mst_connector,
584 	.register_connector = intel_dp_register_mst_connector,
585 	.destroy_connector = intel_dp_destroy_mst_connector,
586 };
587 
588 static struct intel_dp_mst_encoder *
589 intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
590 {
591 	struct intel_dp_mst_encoder *intel_mst;
592 	struct intel_encoder *intel_encoder;
593 	struct drm_device *dev = intel_dig_port->base.base.dev;
594 
595 	intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
596 
597 	if (!intel_mst)
598 		return NULL;
599 
600 	intel_mst->pipe = pipe;
601 	intel_encoder = &intel_mst->base;
602 	intel_mst->primary = intel_dig_port;
603 
604 	drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
605 			 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
606 
607 	intel_encoder->type = INTEL_OUTPUT_DP_MST;
608 	intel_encoder->power_domain = intel_dig_port->base.power_domain;
609 	intel_encoder->port = intel_dig_port->base.port;
610 	intel_encoder->crtc_mask = BIT(pipe);
611 	intel_encoder->cloneable = 0;
612 
613 	intel_encoder->compute_config = intel_dp_mst_compute_config;
614 	intel_encoder->disable = intel_mst_disable_dp;
615 	intel_encoder->post_disable = intel_mst_post_disable_dp;
616 	intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
617 	intel_encoder->post_pll_disable = intel_mst_post_pll_disable_dp;
618 	intel_encoder->pre_enable = intel_mst_pre_enable_dp;
619 	intel_encoder->enable = intel_mst_enable_dp;
620 	intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
621 	intel_encoder->get_config = intel_dp_mst_enc_get_config;
622 
623 	return intel_mst;
624 
625 }
626 
627 static bool
628 intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
629 {
630 	struct intel_dp *intel_dp = &intel_dig_port->dp;
631 	struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
632 	enum pipe pipe;
633 
634 	for_each_pipe(dev_priv, pipe)
635 		intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(intel_dig_port, pipe);
636 	return true;
637 }
638 
639 int
640 intel_dp_mst_encoder_active_links(struct intel_digital_port *intel_dig_port)
641 {
642 	return intel_dig_port->dp.active_mst_links;
643 }
644 
645 int
646 intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
647 {
648 	struct intel_dp *intel_dp = &intel_dig_port->dp;
649 	struct drm_device *dev = intel_dig_port->base.base.dev;
650 	int ret;
651 
652 	intel_dp->can_mst = true;
653 	intel_dp->mst_mgr.cbs = &mst_cbs;
654 
655 	/* create encoders */
656 	intel_dp_create_fake_mst_encoders(intel_dig_port);
657 	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, dev,
658 					   &intel_dp->aux, 16, 3, conn_base_id);
659 	if (ret) {
660 		intel_dp->can_mst = false;
661 		return ret;
662 	}
663 	return 0;
664 }
665 
666 void
667 intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
668 {
669 	struct intel_dp *intel_dp = &intel_dig_port->dp;
670 
671 	if (!intel_dp->can_mst)
672 		return;
673 
674 	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
675 	/* encoders will get killed by normal cleanup */
676 }
677