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_dp.h"
36 #include "intel_dp_mst.h"
37 #include "intel_dpio_phy.h"
38 #include "intel_drv.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_wait_for_register(&dev_priv->uncore,
342 				    DP_TP_STATUS(port),
343 				    DP_TP_STATUS_ACT_SENT,
344 				    DP_TP_STATUS_ACT_SENT,
345 				    1))
346 		DRM_ERROR("Timed out waiting for ACT sent\n");
347 
348 	drm_dp_check_act_status(&intel_dp->mst_mgr);
349 
350 	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
351 	if (pipe_config->has_audio)
352 		intel_audio_codec_enable(encoder, pipe_config, conn_state);
353 }
354 
355 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
356 				      enum pipe *pipe)
357 {
358 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
359 	*pipe = intel_mst->pipe;
360 	if (intel_mst->connector)
361 		return true;
362 	return false;
363 }
364 
365 static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
366 					struct intel_crtc_state *pipe_config)
367 {
368 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
369 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
370 
371 	intel_ddi_get_config(&intel_dig_port->base, pipe_config);
372 }
373 
374 static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
375 {
376 	struct intel_connector *intel_connector = to_intel_connector(connector);
377 	struct intel_dp *intel_dp = intel_connector->mst_port;
378 	struct edid *edid;
379 	int ret;
380 
381 	if (drm_connector_is_unregistered(connector))
382 		return intel_connector_update_modes(connector, NULL);
383 
384 	edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
385 	ret = intel_connector_update_modes(connector, edid);
386 	kfree(edid);
387 
388 	return ret;
389 }
390 
391 static enum drm_connector_status
392 intel_dp_mst_detect(struct drm_connector *connector, bool force)
393 {
394 	struct intel_connector *intel_connector = to_intel_connector(connector);
395 	struct intel_dp *intel_dp = intel_connector->mst_port;
396 
397 	if (drm_connector_is_unregistered(connector))
398 		return connector_status_disconnected;
399 	return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr,
400 				      intel_connector->port);
401 }
402 
403 static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
404 	.detect = intel_dp_mst_detect,
405 	.fill_modes = drm_helper_probe_single_connector_modes,
406 	.atomic_get_property = intel_digital_connector_atomic_get_property,
407 	.atomic_set_property = intel_digital_connector_atomic_set_property,
408 	.late_register = intel_connector_register,
409 	.early_unregister = intel_connector_unregister,
410 	.destroy = intel_connector_destroy,
411 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
412 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
413 };
414 
415 static int intel_dp_mst_get_modes(struct drm_connector *connector)
416 {
417 	return intel_dp_mst_get_ddc_modes(connector);
418 }
419 
420 static enum drm_mode_status
421 intel_dp_mst_mode_valid(struct drm_connector *connector,
422 			struct drm_display_mode *mode)
423 {
424 	struct intel_connector *intel_connector = to_intel_connector(connector);
425 	struct intel_dp *intel_dp = intel_connector->mst_port;
426 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
427 	int max_rate, mode_rate, max_lanes, max_link_clock;
428 
429 	if (drm_connector_is_unregistered(connector))
430 		return MODE_ERROR;
431 
432 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
433 		return MODE_NO_DBLESCAN;
434 
435 	max_link_clock = intel_dp_max_link_rate(intel_dp);
436 	max_lanes = intel_dp_max_lane_count(intel_dp);
437 
438 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
439 	mode_rate = intel_dp_link_required(mode->clock, 18);
440 
441 	/* TODO - validate mode against available PBN for link */
442 	if (mode->clock < 10000)
443 		return MODE_CLOCK_LOW;
444 
445 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
446 		return MODE_H_ILLEGAL;
447 
448 	if (mode_rate > max_rate || mode->clock > max_dotclk)
449 		return MODE_CLOCK_HIGH;
450 
451 	return MODE_OK;
452 }
453 
454 static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
455 							 struct drm_connector_state *state)
456 {
457 	struct intel_connector *intel_connector = to_intel_connector(connector);
458 	struct intel_dp *intel_dp = intel_connector->mst_port;
459 	struct intel_crtc *crtc = to_intel_crtc(state->crtc);
460 
461 	return &intel_dp->mst_encoders[crtc->pipe]->base.base;
462 }
463 
464 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
465 	.get_modes = intel_dp_mst_get_modes,
466 	.mode_valid = intel_dp_mst_mode_valid,
467 	.atomic_best_encoder = intel_mst_atomic_best_encoder,
468 	.atomic_check = intel_dp_mst_atomic_check,
469 };
470 
471 static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
472 {
473 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
474 
475 	drm_encoder_cleanup(encoder);
476 	kfree(intel_mst);
477 }
478 
479 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
480 	.destroy = intel_dp_mst_encoder_destroy,
481 };
482 
483 static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
484 {
485 	if (connector->encoder && connector->base.state->crtc) {
486 		enum pipe pipe;
487 		if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
488 			return false;
489 		return true;
490 	}
491 	return false;
492 }
493 
494 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)
495 {
496 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
497 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
498 	struct drm_device *dev = intel_dig_port->base.base.dev;
499 	struct drm_i915_private *dev_priv = to_i915(dev);
500 	struct intel_connector *intel_connector;
501 	struct drm_connector *connector;
502 	enum pipe pipe;
503 	int ret;
504 
505 	intel_connector = intel_connector_alloc();
506 	if (!intel_connector)
507 		return NULL;
508 
509 	intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
510 	intel_connector->mst_port = intel_dp;
511 	intel_connector->port = port;
512 	drm_dp_mst_get_port_malloc(port);
513 
514 	connector = &intel_connector->base;
515 	ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
516 				 DRM_MODE_CONNECTOR_DisplayPort);
517 	if (ret) {
518 		intel_connector_free(intel_connector);
519 		return NULL;
520 	}
521 
522 	drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
523 
524 	for_each_pipe(dev_priv, pipe) {
525 		struct drm_encoder *enc =
526 			&intel_dp->mst_encoders[pipe]->base.base;
527 
528 		ret = drm_connector_attach_encoder(&intel_connector->base, enc);
529 		if (ret)
530 			goto err;
531 	}
532 
533 	drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
534 	drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
535 
536 	ret = drm_connector_set_path_property(connector, pathprop);
537 	if (ret)
538 		goto err;
539 
540 	intel_attach_force_audio_property(connector);
541 	intel_attach_broadcast_rgb_property(connector);
542 	drm_connector_attach_max_bpc_property(connector, 6, 12);
543 
544 	return connector;
545 
546 err:
547 	drm_connector_cleanup(connector);
548 	return NULL;
549 }
550 
551 static void intel_dp_register_mst_connector(struct drm_connector *connector)
552 {
553 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
554 
555 	if (dev_priv->fbdev)
556 		drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
557 						connector);
558 
559 	drm_connector_register(connector);
560 }
561 
562 static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
563 					   struct drm_connector *connector)
564 {
565 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
566 
567 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, connector->name);
568 	drm_connector_unregister(connector);
569 
570 	if (dev_priv->fbdev)
571 		drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
572 						   connector);
573 
574 	drm_connector_put(connector);
575 }
576 
577 static const struct drm_dp_mst_topology_cbs mst_cbs = {
578 	.add_connector = intel_dp_add_mst_connector,
579 	.register_connector = intel_dp_register_mst_connector,
580 	.destroy_connector = intel_dp_destroy_mst_connector,
581 };
582 
583 static struct intel_dp_mst_encoder *
584 intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
585 {
586 	struct intel_dp_mst_encoder *intel_mst;
587 	struct intel_encoder *intel_encoder;
588 	struct drm_device *dev = intel_dig_port->base.base.dev;
589 
590 	intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
591 
592 	if (!intel_mst)
593 		return NULL;
594 
595 	intel_mst->pipe = pipe;
596 	intel_encoder = &intel_mst->base;
597 	intel_mst->primary = intel_dig_port;
598 
599 	drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
600 			 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
601 
602 	intel_encoder->type = INTEL_OUTPUT_DP_MST;
603 	intel_encoder->power_domain = intel_dig_port->base.power_domain;
604 	intel_encoder->port = intel_dig_port->base.port;
605 	intel_encoder->crtc_mask = 0x7;
606 	intel_encoder->cloneable = 0;
607 
608 	intel_encoder->compute_config = intel_dp_mst_compute_config;
609 	intel_encoder->disable = intel_mst_disable_dp;
610 	intel_encoder->post_disable = intel_mst_post_disable_dp;
611 	intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
612 	intel_encoder->post_pll_disable = intel_mst_post_pll_disable_dp;
613 	intel_encoder->pre_enable = intel_mst_pre_enable_dp;
614 	intel_encoder->enable = intel_mst_enable_dp;
615 	intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
616 	intel_encoder->get_config = intel_dp_mst_enc_get_config;
617 
618 	return intel_mst;
619 
620 }
621 
622 static bool
623 intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
624 {
625 	struct intel_dp *intel_dp = &intel_dig_port->dp;
626 	struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
627 	enum pipe pipe;
628 
629 	for_each_pipe(dev_priv, pipe)
630 		intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(intel_dig_port, pipe);
631 	return true;
632 }
633 
634 int
635 intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
636 {
637 	struct intel_dp *intel_dp = &intel_dig_port->dp;
638 	struct drm_device *dev = intel_dig_port->base.base.dev;
639 	int ret;
640 
641 	intel_dp->can_mst = true;
642 	intel_dp->mst_mgr.cbs = &mst_cbs;
643 
644 	/* create encoders */
645 	intel_dp_create_fake_mst_encoders(intel_dig_port);
646 	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, dev,
647 					   &intel_dp->aux, 16, 3, conn_base_id);
648 	if (ret) {
649 		intel_dp->can_mst = false;
650 		return ret;
651 	}
652 	return 0;
653 }
654 
655 void
656 intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
657 {
658 	struct intel_dp *intel_dp = &intel_dig_port->dp;
659 
660 	if (!intel_dp->can_mst)
661 		return;
662 
663 	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
664 	/* encoders will get killed by normal cleanup */
665 }
666