xref: /linux/drivers/gpu/drm/i915/display/intel_dp_mst.c (revision 8a79db5e83a5d52c74e6f3c40d6f312cf899213e)
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 	intel_ddi_set_dp_msa(pipe_config, conn_state);
336 }
337 
338 static void intel_mst_enable_dp(struct intel_encoder *encoder,
339 				const struct intel_crtc_state *pipe_config,
340 				const struct drm_connector_state *conn_state)
341 {
342 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
343 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
344 	struct intel_dp *intel_dp = &intel_dig_port->dp;
345 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
346 
347 	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
348 
349 	if (intel_de_wait_for_set(dev_priv, intel_dp->regs.dp_tp_status,
350 				  DP_TP_STATUS_ACT_SENT, 1))
351 		DRM_ERROR("Timed out waiting for ACT sent\n");
352 
353 	drm_dp_check_act_status(&intel_dp->mst_mgr);
354 
355 	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
356 	if (pipe_config->has_audio)
357 		intel_audio_codec_enable(encoder, pipe_config, conn_state);
358 }
359 
360 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
361 				      enum pipe *pipe)
362 {
363 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
364 	*pipe = intel_mst->pipe;
365 	if (intel_mst->connector)
366 		return true;
367 	return false;
368 }
369 
370 static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
371 					struct intel_crtc_state *pipe_config)
372 {
373 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
374 	struct intel_digital_port *intel_dig_port = intel_mst->primary;
375 
376 	intel_ddi_get_config(&intel_dig_port->base, pipe_config);
377 }
378 
379 static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
380 {
381 	struct intel_connector *intel_connector = to_intel_connector(connector);
382 	struct intel_dp *intel_dp = intel_connector->mst_port;
383 	struct edid *edid;
384 	int ret;
385 
386 	if (drm_connector_is_unregistered(connector))
387 		return intel_connector_update_modes(connector, NULL);
388 
389 	edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
390 	ret = intel_connector_update_modes(connector, edid);
391 	kfree(edid);
392 
393 	return ret;
394 }
395 
396 static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
397 	.fill_modes = drm_helper_probe_single_connector_modes,
398 	.atomic_get_property = intel_digital_connector_atomic_get_property,
399 	.atomic_set_property = intel_digital_connector_atomic_set_property,
400 	.late_register = intel_connector_register,
401 	.early_unregister = intel_connector_unregister,
402 	.destroy = intel_connector_destroy,
403 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
404 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
405 };
406 
407 static int intel_dp_mst_get_modes(struct drm_connector *connector)
408 {
409 	return intel_dp_mst_get_ddc_modes(connector);
410 }
411 
412 static enum drm_mode_status
413 intel_dp_mst_mode_valid(struct drm_connector *connector,
414 			struct drm_display_mode *mode)
415 {
416 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
417 	struct intel_connector *intel_connector = to_intel_connector(connector);
418 	struct intel_dp *intel_dp = intel_connector->mst_port;
419 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
420 	int max_rate, mode_rate, max_lanes, max_link_clock;
421 
422 	if (drm_connector_is_unregistered(connector))
423 		return MODE_ERROR;
424 
425 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
426 		return MODE_NO_DBLESCAN;
427 
428 	max_link_clock = intel_dp_max_link_rate(intel_dp);
429 	max_lanes = intel_dp_max_lane_count(intel_dp);
430 
431 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
432 	mode_rate = intel_dp_link_required(mode->clock, 18);
433 
434 	/* TODO - validate mode against available PBN for link */
435 	if (mode->clock < 10000)
436 		return MODE_CLOCK_LOW;
437 
438 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
439 		return MODE_H_ILLEGAL;
440 
441 	if (mode_rate > max_rate || mode->clock > max_dotclk)
442 		return MODE_CLOCK_HIGH;
443 
444 	return intel_mode_valid_max_plane_size(dev_priv, mode);
445 }
446 
447 static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
448 							 struct drm_connector_state *state)
449 {
450 	struct intel_connector *intel_connector = to_intel_connector(connector);
451 	struct intel_dp *intel_dp = intel_connector->mst_port;
452 	struct intel_crtc *crtc = to_intel_crtc(state->crtc);
453 
454 	return &intel_dp->mst_encoders[crtc->pipe]->base.base;
455 }
456 
457 static int
458 intel_dp_mst_detect(struct drm_connector *connector,
459 		    struct drm_modeset_acquire_ctx *ctx, bool force)
460 {
461 	struct intel_connector *intel_connector = to_intel_connector(connector);
462 	struct intel_dp *intel_dp = intel_connector->mst_port;
463 
464 	if (drm_connector_is_unregistered(connector))
465 		return connector_status_disconnected;
466 
467 	return drm_dp_mst_detect_port(connector, ctx, &intel_dp->mst_mgr,
468 				      intel_connector->port);
469 }
470 
471 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
472 	.get_modes = intel_dp_mst_get_modes,
473 	.mode_valid = intel_dp_mst_mode_valid,
474 	.atomic_best_encoder = intel_mst_atomic_best_encoder,
475 	.atomic_check = intel_dp_mst_atomic_check,
476 	.detect_ctx = intel_dp_mst_detect,
477 };
478 
479 static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
480 {
481 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
482 
483 	drm_encoder_cleanup(encoder);
484 	kfree(intel_mst);
485 }
486 
487 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
488 	.destroy = intel_dp_mst_encoder_destroy,
489 };
490 
491 static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
492 {
493 	if (connector->encoder && connector->base.state->crtc) {
494 		enum pipe pipe;
495 		if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
496 			return false;
497 		return true;
498 	}
499 	return false;
500 }
501 
502 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)
503 {
504 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
505 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
506 	struct drm_device *dev = intel_dig_port->base.base.dev;
507 	struct drm_i915_private *dev_priv = to_i915(dev);
508 	struct intel_connector *intel_connector;
509 	struct drm_connector *connector;
510 	enum pipe pipe;
511 	int ret;
512 
513 	intel_connector = intel_connector_alloc();
514 	if (!intel_connector)
515 		return NULL;
516 
517 	intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
518 	intel_connector->mst_port = intel_dp;
519 	intel_connector->port = port;
520 	drm_dp_mst_get_port_malloc(port);
521 
522 	connector = &intel_connector->base;
523 	ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
524 				 DRM_MODE_CONNECTOR_DisplayPort);
525 	if (ret) {
526 		intel_connector_free(intel_connector);
527 		return NULL;
528 	}
529 
530 	drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
531 
532 	for_each_pipe(dev_priv, pipe) {
533 		struct drm_encoder *enc =
534 			&intel_dp->mst_encoders[pipe]->base.base;
535 
536 		ret = drm_connector_attach_encoder(&intel_connector->base, enc);
537 		if (ret)
538 			goto err;
539 	}
540 
541 	drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
542 	drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
543 
544 	ret = drm_connector_set_path_property(connector, pathprop);
545 	if (ret)
546 		goto err;
547 
548 	intel_attach_force_audio_property(connector);
549 	intel_attach_broadcast_rgb_property(connector);
550 
551 	/*
552 	 * Reuse the prop from the SST connector because we're
553 	 * not allowed to create new props after device registration.
554 	 */
555 	connector->max_bpc_property =
556 		intel_dp->attached_connector->base.max_bpc_property;
557 	if (connector->max_bpc_property)
558 		drm_connector_attach_max_bpc_property(connector, 6, 12);
559 
560 	return connector;
561 
562 err:
563 	drm_connector_cleanup(connector);
564 	return NULL;
565 }
566 
567 static void intel_dp_register_mst_connector(struct drm_connector *connector)
568 {
569 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
570 
571 	if (dev_priv->fbdev)
572 		drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
573 						connector);
574 
575 	drm_connector_register(connector);
576 }
577 
578 static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
579 					   struct drm_connector *connector)
580 {
581 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
582 
583 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, connector->name);
584 	drm_connector_unregister(connector);
585 
586 	if (dev_priv->fbdev)
587 		drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
588 						   connector);
589 
590 	drm_connector_put(connector);
591 }
592 
593 static const struct drm_dp_mst_topology_cbs mst_cbs = {
594 	.add_connector = intel_dp_add_mst_connector,
595 	.register_connector = intel_dp_register_mst_connector,
596 	.destroy_connector = intel_dp_destroy_mst_connector,
597 };
598 
599 static struct intel_dp_mst_encoder *
600 intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
601 {
602 	struct intel_dp_mst_encoder *intel_mst;
603 	struct intel_encoder *intel_encoder;
604 	struct drm_device *dev = intel_dig_port->base.base.dev;
605 
606 	intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
607 
608 	if (!intel_mst)
609 		return NULL;
610 
611 	intel_mst->pipe = pipe;
612 	intel_encoder = &intel_mst->base;
613 	intel_mst->primary = intel_dig_port;
614 
615 	drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
616 			 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
617 
618 	intel_encoder->type = INTEL_OUTPUT_DP_MST;
619 	intel_encoder->power_domain = intel_dig_port->base.power_domain;
620 	intel_encoder->port = intel_dig_port->base.port;
621 	intel_encoder->cloneable = 0;
622 	/*
623 	 * This is wrong, but broken userspace uses the intersection
624 	 * of possible_crtcs of all the encoders of a given connector
625 	 * to figure out which crtcs can drive said connector. What
626 	 * should be used instead is the union of possible_crtcs.
627 	 * To keep such userspace functioning we must misconfigure
628 	 * this to make sure the intersection is not empty :(
629 	 */
630 	intel_encoder->pipe_mask = ~0;
631 
632 	intel_encoder->compute_config = intel_dp_mst_compute_config;
633 	intel_encoder->disable = intel_mst_disable_dp;
634 	intel_encoder->post_disable = intel_mst_post_disable_dp;
635 	intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
636 	intel_encoder->post_pll_disable = intel_mst_post_pll_disable_dp;
637 	intel_encoder->pre_enable = intel_mst_pre_enable_dp;
638 	intel_encoder->enable = intel_mst_enable_dp;
639 	intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
640 	intel_encoder->get_config = intel_dp_mst_enc_get_config;
641 
642 	return intel_mst;
643 
644 }
645 
646 static bool
647 intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
648 {
649 	struct intel_dp *intel_dp = &intel_dig_port->dp;
650 	struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
651 	enum pipe pipe;
652 
653 	for_each_pipe(dev_priv, pipe)
654 		intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(intel_dig_port, pipe);
655 	return true;
656 }
657 
658 int
659 intel_dp_mst_encoder_active_links(struct intel_digital_port *intel_dig_port)
660 {
661 	return intel_dig_port->dp.active_mst_links;
662 }
663 
664 int
665 intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
666 {
667 	struct drm_i915_private *i915 = to_i915(intel_dig_port->base.base.dev);
668 	struct intel_dp *intel_dp = &intel_dig_port->dp;
669 	enum port port = intel_dig_port->base.port;
670 	int ret;
671 
672 	if (!HAS_DP_MST(i915) || intel_dp_is_edp(intel_dp))
673 		return 0;
674 
675 	if (INTEL_GEN(i915) < 12 && port == PORT_A)
676 		return 0;
677 
678 	if (INTEL_GEN(i915) < 11 && port == PORT_E)
679 		return 0;
680 
681 	intel_dp->mst_mgr.cbs = &mst_cbs;
682 
683 	/* create encoders */
684 	intel_dp_create_fake_mst_encoders(intel_dig_port);
685 	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, &i915->drm,
686 					   &intel_dp->aux, 16, 3, conn_base_id);
687 	if (ret)
688 		return ret;
689 
690 	intel_dp->can_mst = true;
691 
692 	return 0;
693 }
694 
695 void
696 intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
697 {
698 	struct intel_dp *intel_dp = &intel_dig_port->dp;
699 
700 	if (!intel_dp->can_mst)
701 		return;
702 
703 	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
704 	/* encoders will get killed by normal cleanup */
705 }
706