intel_dp_mst.c 21.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright © 2008 Intel Corporation
 *             2014 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

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
39
#include "intel_drv.h"

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
static int intel_dp_mst_compute_link_config(struct intel_encoder *encoder,
					    struct intel_crtc_state *crtc_state,
					    struct drm_connector_state *conn_state,
					    struct link_config_limits *limits)
{
	struct drm_atomic_state *state = crtc_state->base.state;
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	struct intel_dp *intel_dp = &intel_mst->primary->dp;
	struct intel_connector *connector =
		to_intel_connector(conn_state->connector);
	const struct drm_display_mode *adjusted_mode =
		&crtc_state->base.adjusted_mode;
	void *port = connector->port;
	bool constant_n = drm_dp_has_quirk(&intel_dp->desc,
					   DP_DPCD_QUIRK_CONSTANT_N);
	int bpp, slots = -EINVAL;

	crtc_state->lane_count = limits->max_lane_count;
	crtc_state->port_clock = limits->max_clock;

	for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
		crtc_state->pipe_bpp = bpp;

		crtc_state->pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock,
						       crtc_state->pipe_bpp);

		slots = drm_dp_atomic_find_vcpi_slots(state, &intel_dp->mst_mgr,
						      port, crtc_state->pbn);
		if (slots == -EDEADLK)
			return slots;
		if (slots >= 0)
			break;
	}

	if (slots < 0) {
		DRM_DEBUG_KMS("failed finding vcpi slots:%d\n", slots);
		return slots;
	}

	intel_link_compute_m_n(crtc_state->pipe_bpp,
			       crtc_state->lane_count,
			       adjusted_mode->crtc_clock,
			       crtc_state->port_clock,
			       &crtc_state->dp_m_n,
			       constant_n);
	crtc_state->dp_m_n.tu = slots;

	return 0;
}

90
91
92
static int intel_dp_mst_compute_config(struct intel_encoder *encoder,
				       struct intel_crtc_state *pipe_config,
				       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
97
98
	struct intel_dp *intel_dp = &intel_mst->primary->dp;
	struct intel_connector *connector =
		to_intel_connector(conn_state->connector);
99
100
	struct intel_digital_connector_state *intel_conn_state =
		to_intel_digital_connector_state(conn_state);
101
102
103
104
105
	const struct drm_display_mode *adjusted_mode =
		&pipe_config->base.adjusted_mode;
	void *port = connector->port;
	struct link_config_limits limits;
	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
114
115
116
117
118
119

	if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
		pipe_config->has_audio =
			drm_dp_mst_port_has_audio(&intel_dp->mst_mgr, port);
	else
		pipe_config->has_audio =
			intel_conn_state->force_audio == HDMI_AUDIO_ON;

120
121
122
123
	/*
	 * for MST we always configure max link bw - the spec doesn't
	 * seem to suggest we should do otherwise.
	 */
124
125
	limits.min_clock =
	limits.max_clock = intel_dp_max_link_rate(intel_dp);
126

127
128
	limits.min_lane_count =
	limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
129

130
	limits.min_bpp = intel_dp_min_bpp(pipe_config);
131
132
133
134
135
136
137
138
139
	/*
	 * FIXME: If all the streams can't fit into the link with
	 * their current pipe_bpp we should reduce pipe_bpp across
	 * the board until things start to fit. Until then we
	 * limit to <= 8bpc since that's what was hardcoded for all
	 * MST streams previously. This hack should be removed once
	 * we have the proper retry logic in place.
	 */
	limits.max_bpp = min(pipe_config->pipe_bpp, 24);
140

141
142
143
144
145
146
	intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);

	ret = intel_dp_mst_compute_link_config(encoder, pipe_config,
					       conn_state, &limits);
	if (ret)
		return ret;
147

148
149
150
	pipe_config->limited_color_range =
		intel_dp_limited_color_range(pipe_config, conn_state);

151
152
153
154
	if (IS_GEN9_LP(dev_priv))
		pipe_config->lane_lat_optim_mask =
			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);

155
156
	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);

157
	return 0;
158
}
159

160
161
static int
intel_dp_mst_atomic_check(struct drm_connector *connector,
162
			  struct drm_atomic_state *state)
163
{
164
165
	struct drm_connector_state *new_conn_state =
		drm_atomic_get_new_connector_state(state, connector);
166
167
168
169
170
	struct drm_connector_state *old_conn_state =
		drm_atomic_get_old_connector_state(state, connector);
	struct intel_connector *intel_connector =
		to_intel_connector(connector);
	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
174
	int ret;

175
	ret = intel_digital_connector_atomic_check(connector, state);
176
177
	if (ret)
		return ret;
178
179
180
181
182
183
184
185
186
187
188
189
190
191

	if (!old_conn_state->crtc)
		return 0;

	/* We only want to free VCPI if this state disables the CRTC on this
	 * connector
	 */
	if (new_crtc) {
		crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);

		if (!crtc_state ||
		    !drm_atomic_crtc_needs_modeset(crtc_state) ||
		    crtc_state->enable)
			return 0;
192
	}
193
194
195
196
197

	mgr = &enc_to_mst(old_conn_state->best_encoder)->primary->dp.mst_mgr;
	ret = drm_dp_atomic_release_vcpi_slots(state, mgr,
					       intel_connector->port);

198
	return ret;
199
200
}

201
static void intel_mst_disable_dp(struct intel_encoder *encoder,
202
203
				 const struct intel_crtc_state *old_crtc_state,
				 const struct drm_connector_state *old_conn_state)
204
205
206
207
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	struct intel_digital_port *intel_dig_port = intel_mst->primary;
	struct intel_dp *intel_dp = &intel_dig_port->dp;
208
209
	struct intel_connector *connector =
		to_intel_connector(old_conn_state->connector);
210
211
	int ret;

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
217
218
219

	ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
	if (ret) {
		DRM_ERROR("failed to update payload %d\n", ret);
	}
220
	if (old_crtc_state->has_audio)
221
222
		intel_audio_codec_disable(encoder,
					  old_crtc_state, old_conn_state);
223
224
}

225
static void intel_mst_post_disable_dp(struct intel_encoder *encoder,
226
227
				      const struct intel_crtc_state *old_crtc_state,
				      const struct drm_connector_state *old_conn_state)
228
229
230
231
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	struct intel_digital_port *intel_dig_port = intel_mst->primary;
	struct intel_dp *intel_dp = &intel_dig_port->dp;
232
233
	struct intel_connector *connector =
		to_intel_connector(old_conn_state->connector);
234

235
236
	intel_ddi_disable_pipe_clock(old_crtc_state);

237
238
239
240
241
	/* this can fail */
	drm_dp_check_act_status(&intel_dp->mst_mgr);
	/* and this can also fail */
	drm_dp_update_payload_part2(&intel_dp->mst_mgr);

242
	drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
243

244
245
246
247
248
249
250
	/*
	 * Power down mst path before disabling the port, otherwise we end
	 * up getting interrupts from the sink upon detecting link loss.
	 */
	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
				     false);

251
	intel_dp->active_mst_links--;
252
253

	intel_mst->connector = NULL;
254
255
	if (intel_dp->active_mst_links == 0) {
		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
264
265
266
267
268
269
270
static void intel_mst_pre_pll_enable_dp(struct intel_encoder *encoder,
					const struct intel_crtc_state *pipe_config,
					const struct drm_connector_state *conn_state)
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	struct intel_digital_port *intel_dig_port = intel_mst->primary;
	struct intel_dp *intel_dp = &intel_dig_port->dp;

271
	if (intel_dp->active_mst_links == 0)
272
273
274
275
		intel_dig_port->base.pre_pll_enable(&intel_dig_port->base,
						    pipe_config, NULL);
}

276
277
278
279
280
281
282
283
284
285
286
287
288
289
static void intel_mst_post_pll_disable_dp(struct intel_encoder *encoder,
					  const struct intel_crtc_state *old_crtc_state,
					  const struct drm_connector_state *old_conn_state)
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	struct intel_digital_port *intel_dig_port = intel_mst->primary;
	struct intel_dp *intel_dp = &intel_dig_port->dp;

	if (intel_dp->active_mst_links == 0)
		intel_dig_port->base.post_pll_disable(&intel_dig_port->base,
						      old_crtc_state,
						      old_conn_state);
}

290
static void intel_mst_pre_enable_dp(struct intel_encoder *encoder,
291
292
				    const struct intel_crtc_state *pipe_config,
				    const struct drm_connector_state *conn_state)
293
294
295
296
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	struct intel_digital_port *intel_dig_port = intel_mst->primary;
	struct intel_dp *intel_dp = &intel_dig_port->dp;
297
	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
298
	enum port port = intel_dig_port->base.port;
299
300
	struct intel_connector *connector =
		to_intel_connector(conn_state->connector);
301
	int ret;
302
	u32 temp;
303

304
305
306
	/* MST encoders are bound to a crtc, not to a connector,
	 * force the mapping here for get_hw_state.
	 */
307
308
	connector->encoder = encoder;
	intel_mst->connector = connector;
309

310
	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
311

312
313
314
	if (intel_dp->active_mst_links == 0)
		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);

315
	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
316

317
318
319
	if (intel_dp->active_mst_links == 0)
		intel_dig_port->base.pre_enable(&intel_dig_port->base,
						pipe_config, NULL);
320
321

	ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
322
				       connector->port,
323
324
				       pipe_config->pbn,
				       pipe_config->dp_m_n.tu);
325
	if (!ret)
326
327
		DRM_ERROR("failed to allocate vcpi\n");

328
	intel_dp->active_mst_links++;
329
330
331
332
	temp = I915_READ(DP_TP_STATUS(port));
	I915_WRITE(DP_TP_STATUS(port), temp);

	ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
333
334

	intel_ddi_enable_pipe_clock(pipe_config);
335
336
}

337
static void intel_mst_enable_dp(struct intel_encoder *encoder,
338
339
				const struct intel_crtc_state *pipe_config,
				const struct drm_connector_state *conn_state)
340
341
342
343
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	struct intel_digital_port *intel_dig_port = intel_mst->primary;
	struct intel_dp *intel_dp = &intel_dig_port->dp;
344
	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
345
	enum port port = intel_dig_port->base.port;
346

347
	DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
348

349
	if (intel_wait_for_register(&dev_priv->uncore,
350
351
352
353
				    DP_TP_STATUS(port),
				    DP_TP_STATUS_ACT_SENT,
				    DP_TP_STATUS_ACT_SENT,
				    1))
354
355
		DRM_ERROR("Timed out waiting for ACT sent\n");

356
	drm_dp_check_act_status(&intel_dp->mst_mgr);
357

358
	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
359
	if (pipe_config->has_audio)
Libin Yang's avatar
Libin Yang committed
360
		intel_audio_codec_enable(encoder, pipe_config, conn_state);
361
362
363
364
365
366
367
}

static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
				      enum pipe *pipe)
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	*pipe = intel_mst->pipe;
368
	if (intel_mst->connector)
369
370
371
372
373
		return true;
	return false;
}

static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
374
					struct intel_crtc_state *pipe_config)
375
376
377
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
	struct intel_digital_port *intel_dig_port = intel_mst->primary;
378

379
	intel_ddi_get_config(&intel_dig_port->base, pipe_config);
380
381
382
383
384
385
386
387
388
}

static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
{
	struct intel_connector *intel_connector = to_intel_connector(connector);
	struct intel_dp *intel_dp = intel_connector->mst_port;
	struct edid *edid;
	int ret;

389
	if (drm_connector_is_unregistered(connector))
390
		return intel_connector_update_modes(connector, NULL);
391

392
	edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
393
394
395
396
397
398
399
	ret = intel_connector_update_modes(connector, edid);
	kfree(edid);

	return ret;
}

static enum drm_connector_status
400
intel_dp_mst_detect(struct drm_connector *connector, bool force)
401
402
403
404
{
	struct intel_connector *intel_connector = to_intel_connector(connector);
	struct intel_dp *intel_dp = intel_connector->mst_port;

405
	if (drm_connector_is_unregistered(connector))
406
		return connector_status_disconnected;
407
408
	return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr,
				      intel_connector->port);
409
410
411
412
413
}

static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
	.detect = intel_dp_mst_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
414
415
	.atomic_get_property = intel_digital_connector_atomic_get_property,
	.atomic_set_property = intel_digital_connector_atomic_set_property,
416
	.late_register = intel_connector_register,
417
	.early_unregister = intel_connector_unregister,
418
	.destroy = intel_connector_destroy,
419
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
420
	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
421
422
423
424
425
426
427
428
429
430
431
};

static int intel_dp_mst_get_modes(struct drm_connector *connector)
{
	return intel_dp_mst_get_ddc_modes(connector);
}

static enum drm_mode_status
intel_dp_mst_mode_valid(struct drm_connector *connector,
			struct drm_display_mode *mode)
{
432
433
	struct intel_connector *intel_connector = to_intel_connector(connector);
	struct intel_dp *intel_dp = intel_connector->mst_port;
434
	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
435
436
	int max_rate, mode_rate, max_lanes, max_link_clock;

437
	if (drm_connector_is_unregistered(connector))
438
439
		return MODE_ERROR;

440
441
442
	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
		return MODE_NO_DBLESCAN;

443
	max_link_clock = intel_dp_max_link_rate(intel_dp);
444
	max_lanes = intel_dp_max_lane_count(intel_dp);
445
446

	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
447
	mode_rate = intel_dp_link_required(mode->clock, 18);
448

449
450
451
452
453
454
455
	/* TODO - validate mode against available PBN for link */
	if (mode->clock < 10000)
		return MODE_CLOCK_LOW;

	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
		return MODE_H_ILLEGAL;

456
	if (mode_rate > max_rate || mode->clock > max_dotclk)
457
458
		return MODE_CLOCK_HIGH;

459
460
461
	return MODE_OK;
}

462
463
464
465
466
467
468
469
470
471
static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
							 struct drm_connector_state *state)
{
	struct intel_connector *intel_connector = to_intel_connector(connector);
	struct intel_dp *intel_dp = intel_connector->mst_port;
	struct intel_crtc *crtc = to_intel_crtc(state->crtc);

	return &intel_dp->mst_encoders[crtc->pipe]->base.base;
}

472
473
474
static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
	.get_modes = intel_dp_mst_get_modes,
	.mode_valid = intel_dp_mst_mode_valid,
475
	.atomic_best_encoder = intel_mst_atomic_best_encoder,
476
	.atomic_check = intel_dp_mst_atomic_check,
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
};

static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
{
	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);

	drm_encoder_cleanup(encoder);
	kfree(intel_mst);
}

static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
	.destroy = intel_dp_mst_encoder_destroy,
};

static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
{
493
	if (connector->encoder && connector->base.state->crtc) {
494
495
496
497
498
499
500
501
		enum pipe pipe;
		if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
			return false;
		return true;
	}
	return false;
}

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
505
506
{
	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
	struct drm_device *dev = intel_dig_port->base.base.dev;
507
	struct drm_i915_private *dev_priv = to_i915(dev);
508
509
	struct intel_connector *intel_connector;
	struct drm_connector *connector;
510
	enum pipe pipe;
511
	int ret;
512

513
	intel_connector = intel_connector_alloc();
514
515
516
	if (!intel_connector)
		return NULL;

517
518
519
	intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
	intel_connector->mst_port = intel_dp;
	intel_connector->port = port;
520
	drm_dp_mst_get_port_malloc(port);
521

522
	connector = &intel_connector->base;
523
524
525
526
527
528
529
	ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
				 DRM_MODE_CONNECTOR_DisplayPort);
	if (ret) {
		intel_connector_free(intel_connector);
		return NULL;
	}

530
531
	drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);

532
	for_each_pipe(dev_priv, pipe) {
533
534
535
		struct drm_encoder *enc =
			&intel_dp->mst_encoders[pipe]->base.base;

536
		ret = drm_connector_attach_encoder(&intel_connector->base, enc);
537
538
		if (ret)
			goto err;
539
540
541
	}

	drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
542
543
	drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);

544
	ret = drm_connector_set_path_property(connector, pathprop);
545
546
547
	if (ret)
		goto err;

548
	intel_attach_force_audio_property(connector);
549
	intel_attach_broadcast_rgb_property(connector);
550
551
552
553
554
555
556
557
558

	/*
	 * Reuse the prop from the SST connector because we're
	 * not allowed to create new props after device registration.
	 */
	connector->max_bpc_property =
		intel_dp->attached_connector->base.max_bpc_property;
	if (connector->max_bpc_property)
		drm_connector_attach_max_bpc_property(connector, 6, 12);
559

560
	return connector;
561
562
563
564

err:
	drm_connector_cleanup(connector);
	return NULL;
565
566
567
568
}

static void intel_dp_register_mst_connector(struct drm_connector *connector)
{
569
	struct drm_i915_private *dev_priv = to_i915(connector->dev);
570

571
572
573
	if (dev_priv->fbdev)
		drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
						connector);
574

575
	drm_connector_register(connector);
576
577
578
579
580
}

static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
					   struct drm_connector *connector)
{
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
587
588
	if (dev_priv->fbdev)
		drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
						   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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
	.destroy_connector = intel_dp_destroy_mst_connector,
};

static struct intel_dp_mst_encoder *
intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
{
	struct intel_dp_mst_encoder *intel_mst;
	struct intel_encoder *intel_encoder;
	struct drm_device *dev = intel_dig_port->base.base.dev;

	intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);

	if (!intel_mst)
		return NULL;

	intel_mst->pipe = pipe;
	intel_encoder = &intel_mst->base;
	intel_mst->primary = intel_dig_port;

	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
622
623
624
625
626
	intel_encoder->crtc_mask = 0x7;
	intel_encoder->cloneable = 0;

	intel_encoder->compute_config = intel_dp_mst_compute_config;
	intel_encoder->disable = intel_mst_disable_dp;
	intel_encoder->post_disable = intel_mst_post_disable_dp;
627
	intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
628
	intel_encoder->post_pll_disable = intel_mst_post_pll_disable_dp;
629
630
631
632
633
634
635
636
637
638
639
640
641
	intel_encoder->pre_enable = intel_mst_pre_enable_dp;
	intel_encoder->enable = intel_mst_enable_dp;
	intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
	intel_encoder->get_config = intel_dp_mst_enc_get_config;

	return intel_mst;

}

static bool
intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
{
	struct intel_dp *intel_dp = &intel_dig_port->dp;
642
643
	struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
	enum pipe pipe;
644

645
646
	for_each_pipe(dev_priv, pipe)
		intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(intel_dig_port, pipe);
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
	return true;
}

int
intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
{
	struct intel_dp *intel_dp = &intel_dig_port->dp;
	struct drm_device *dev = intel_dig_port->base.base.dev;
	int ret;

	intel_dp->can_mst = true;
	intel_dp->mst_mgr.cbs = &mst_cbs;

	/* create encoders */
	intel_dp_create_fake_mst_encoders(intel_dig_port);
662
663
	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, dev,
					   &intel_dp->aux, 16, 3, conn_base_id);
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
	if (ret) {
		intel_dp->can_mst = false;
		return ret;
	}
	return 0;
}

void
intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
{
	struct intel_dp *intel_dp = &intel_dig_port->dp;

	if (!intel_dp->can_mst)
		return;

	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
	/* encoders will get killed by normal cleanup */
}