blob: a8334e18c4fc08a5381d600fb1b8d05ce50477ca [file] [log] [blame]
Dave Airlie0e32b392014-05-02 14:02:48 +10001/*
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/drmP.h>
27#include "i915_drv.h"
28#include "intel_drv.h"
Matt Roperc6f95f22015-01-22 16:50:32 -080029#include <drm/drm_atomic_helper.h>
Dave Airlie0e32b392014-05-02 14:02:48 +100030#include <drm/drm_crtc_helper.h>
31#include <drm/drm_edid.h>
32
33static bool intel_dp_mst_compute_config(struct intel_encoder *encoder,
Maarten Lankhorst0a478c22016-08-09 17:04:05 +020034 struct intel_crtc_state *pipe_config,
35 struct drm_connector_state *conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +100036{
37 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
38 struct intel_digital_port *intel_dig_port = intel_mst->primary;
39 struct intel_dp *intel_dp = &intel_dig_port->dp;
Libin Yang7f9e7752016-11-28 20:07:07 +080040 struct intel_connector *connector =
41 to_intel_connector(conn_state->connector);
Ander Conselvan de Oliveirae75f4772015-03-20 16:18:13 +020042 struct drm_atomic_state *state;
Maarten Lankhorst1189e4f2016-08-09 17:04:06 +020043 int bpp;
Ville Syrjälä04a60f92015-07-06 15:10:06 +030044 int lane_count, slots;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +030045 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Dave Airlie0e32b392014-05-02 14:02:48 +100046 int mst_pbn;
47
Dave Airlie0e32b392014-05-02 14:02:48 +100048 pipe_config->has_pch_encoder = false;
Dave Airlie0e32b392014-05-02 14:02:48 +100049 bpp = 24;
Manasi Navare611032b2017-01-24 08:21:49 -080050 if (intel_dp->compliance.test_data.bpc) {
51 bpp = intel_dp->compliance.test_data.bpc * 3;
52 DRM_DEBUG_KMS("Setting pipe bpp to %d\n",
53 bpp);
54 }
Dave Airlie0e32b392014-05-02 14:02:48 +100055 /*
56 * for MST we always configure max link bw - the spec doesn't
57 * seem to suggest we should do otherwise.
58 */
59 lane_count = drm_dp_max_lane_count(intel_dp->dpcd);
Ville Syrjäläed4e9c12015-03-12 17:10:36 +020060
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030061 pipe_config->lane_count = lane_count;
Dave Airlie0e32b392014-05-02 14:02:48 +100062
Manasi Navare611032b2017-01-24 08:21:49 -080063 pipe_config->pipe_bpp = bpp;
Ville Syrjälä04a60f92015-07-06 15:10:06 +030064 pipe_config->port_clock = intel_dp_max_link_rate(intel_dp);
Dave Airlie0e32b392014-05-02 14:02:48 +100065
Ander Conselvan de Oliveirae75f4772015-03-20 16:18:13 +020066 state = pipe_config->base.state;
67
Libin Yang7f9e7752016-11-28 20:07:07 +080068 if (drm_dp_mst_port_has_audio(&intel_dp->mst_mgr, connector->port))
69 pipe_config->has_audio = true;
Ville Syrjäläaad941d2015-09-25 16:38:56 +030070 mst_pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock, bpp);
Dave Airlie0e32b392014-05-02 14:02:48 +100071
72 pipe_config->pbn = mst_pbn;
73 slots = drm_dp_find_vcpi_slots(&intel_dp->mst_mgr, mst_pbn);
74
75 intel_link_compute_m_n(bpp, lane_count,
76 adjusted_mode->crtc_clock,
77 pipe_config->port_clock,
78 &pipe_config->dp_m_n);
79
80 pipe_config->dp_m_n.tu = slots;
Ander Conselvan de Oliveira6fa2d192015-08-31 11:23:28 +030081
Dave Airlie0e32b392014-05-02 14:02:48 +100082 return true;
83
84}
85
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +020086static void intel_mst_disable_dp(struct intel_encoder *encoder,
87 struct intel_crtc_state *old_crtc_state,
88 struct drm_connector_state *old_conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +100089{
90 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
91 struct intel_digital_port *intel_dig_port = intel_mst->primary;
92 struct intel_dp *intel_dp = &intel_dig_port->dp;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +020093 struct intel_connector *connector =
94 to_intel_connector(old_conn_state->connector);
Dave Airlie0e32b392014-05-02 14:02:48 +100095 int ret;
96
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +030097 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
Dave Airlie0e32b392014-05-02 14:02:48 +100098
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +020099 drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, connector->port);
Dave Airlie0e32b392014-05-02 14:02:48 +1000100
101 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
102 if (ret) {
103 DRM_ERROR("failed to update payload %d\n", ret);
104 }
Maarten Lankhorst37255d82016-12-15 15:29:43 +0100105 if (old_crtc_state->has_audio)
Libin Yang7f9e7752016-11-28 20:07:07 +0800106 intel_audio_codec_disable(encoder);
Dave Airlie0e32b392014-05-02 14:02:48 +1000107}
108
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200109static void intel_mst_post_disable_dp(struct intel_encoder *encoder,
110 struct intel_crtc_state *old_crtc_state,
111 struct drm_connector_state *old_conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +1000112{
113 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
114 struct intel_digital_port *intel_dig_port = intel_mst->primary;
115 struct intel_dp *intel_dp = &intel_dig_port->dp;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200116 struct intel_connector *connector =
117 to_intel_connector(old_conn_state->connector);
Dave Airlie0e32b392014-05-02 14:02:48 +1000118
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300119 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
Dave Airlie0e32b392014-05-02 14:02:48 +1000120
121 /* this can fail */
122 drm_dp_check_act_status(&intel_dp->mst_mgr);
123 /* and this can also fail */
124 drm_dp_update_payload_part2(&intel_dp->mst_mgr);
125
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200126 drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
Dave Airlie0e32b392014-05-02 14:02:48 +1000127
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300128 intel_dp->active_mst_links--;
Dave Airlie0552f762016-03-09 11:14:38 +1000129
130 intel_mst->connector = NULL;
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300131 if (intel_dp->active_mst_links == 0) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200132 intel_dig_port->base.post_disable(&intel_dig_port->base,
133 NULL, NULL);
134
Dave Airlie0e32b392014-05-02 14:02:48 +1000135 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
136 }
137}
138
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200139static void intel_mst_pre_enable_dp(struct intel_encoder *encoder,
140 struct intel_crtc_state *pipe_config,
141 struct drm_connector_state *conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +1000142{
143 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
144 struct intel_digital_port *intel_dig_port = intel_mst->primary;
145 struct intel_dp *intel_dp = &intel_dig_port->dp;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200146 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Dave Airlie0e32b392014-05-02 14:02:48 +1000147 enum port port = intel_dig_port->port;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200148 struct intel_connector *connector =
149 to_intel_connector(conn_state->connector);
Dave Airlie0e32b392014-05-02 14:02:48 +1000150 int ret;
151 uint32_t temp;
Dave Airlie0e32b392014-05-02 14:02:48 +1000152 int slots;
Dave Airlie0e32b392014-05-02 14:02:48 +1000153
Maarten Lankhorste85376c2015-08-27 13:13:31 +0200154 /* MST encoders are bound to a crtc, not to a connector,
155 * force the mapping here for get_hw_state.
156 */
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200157 connector->encoder = encoder;
158 intel_mst->connector = connector;
Maarten Lankhorste85376c2015-08-27 13:13:31 +0200159
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300160 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
Dave Airlie0552f762016-03-09 11:14:38 +1000161
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300162 if (intel_dp->active_mst_links == 0) {
Ander Conselvan de Oliveirac8560522016-09-01 15:08:07 -0700163 intel_ddi_clk_select(&intel_dig_port->base,
164 pipe_config->shared_dpll);
Dave Airlie0e32b392014-05-02 14:02:48 +1000165
Ander Conselvan de Oliveiraa7460952017-03-01 16:13:11 +0200166 intel_display_power_get(dev_priv,
167 intel_dig_port->ddi_io_power_domain);
168
Ville Syrjälä32bdc402016-07-12 15:59:33 +0300169 intel_prepare_dp_ddi_buffers(&intel_dig_port->base);
Ander Conselvan de Oliveiradfa10482016-09-01 15:08:06 -0700170 intel_dp_set_link_params(intel_dp,
171 pipe_config->port_clock,
172 pipe_config->lane_count,
173 true);
Ville Syrjälä901c2da2015-08-17 18:05:12 +0300174
Dave Airlie0e32b392014-05-02 14:02:48 +1000175 intel_ddi_init_dp_buf_reg(&intel_dig_port->base);
176
177 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
178
Dave Airlie0e32b392014-05-02 14:02:48 +1000179 intel_dp_start_link_train(intel_dp);
Dave Airlie0e32b392014-05-02 14:02:48 +1000180 intel_dp_stop_link_train(intel_dp);
181 }
182
183 ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200184 connector->port,
185 pipe_config->pbn, &slots);
Dave Airlie0e32b392014-05-02 14:02:48 +1000186 if (ret == false) {
187 DRM_ERROR("failed to allocate vcpi\n");
188 return;
189 }
190
191
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300192 intel_dp->active_mst_links++;
Dave Airlie0e32b392014-05-02 14:02:48 +1000193 temp = I915_READ(DP_TP_STATUS(port));
194 I915_WRITE(DP_TP_STATUS(port), temp);
195
196 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
197}
198
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200199static void intel_mst_enable_dp(struct intel_encoder *encoder,
200 struct intel_crtc_state *pipe_config,
201 struct drm_connector_state *conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +1000202{
203 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
204 struct intel_digital_port *intel_dig_port = intel_mst->primary;
205 struct intel_dp *intel_dp = &intel_dig_port->dp;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200206 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Dave Airlie0e32b392014-05-02 14:02:48 +1000207 enum port port = intel_dig_port->port;
208 int ret;
209
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300210 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
Dave Airlie0e32b392014-05-02 14:02:48 +1000211
Chris Wilson3016a312016-06-30 15:33:11 +0100212 if (intel_wait_for_register(dev_priv,
213 DP_TP_STATUS(port),
214 DP_TP_STATUS_ACT_SENT,
215 DP_TP_STATUS_ACT_SENT,
216 1))
Dave Airlie0e32b392014-05-02 14:02:48 +1000217 DRM_ERROR("Timed out waiting for ACT sent\n");
218
219 ret = drm_dp_check_act_status(&intel_dp->mst_mgr);
220
221 ret = drm_dp_update_payload_part2(&intel_dp->mst_mgr);
Maarten Lankhorst37255d82016-12-15 15:29:43 +0100222 if (pipe_config->has_audio)
Libin Yang7f9e7752016-11-28 20:07:07 +0800223 intel_audio_codec_enable(encoder, pipe_config, conn_state);
Dave Airlie0e32b392014-05-02 14:02:48 +1000224}
225
226static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
227 enum pipe *pipe)
228{
229 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
230 *pipe = intel_mst->pipe;
Dave Airlie0552f762016-03-09 11:14:38 +1000231 if (intel_mst->connector)
Dave Airlie0e32b392014-05-02 14:02:48 +1000232 return true;
233 return false;
234}
235
236static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200237 struct intel_crtc_state *pipe_config)
Dave Airlie0e32b392014-05-02 14:02:48 +1000238{
239 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
240 struct intel_digital_port *intel_dig_port = intel_mst->primary;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200241 struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
242 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Ander Conselvan de Oliveira0cb09a92015-01-30 12:17:23 +0200243 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
Dave Airlie0e32b392014-05-02 14:02:48 +1000244 u32 temp, flags = 0;
245
Libin Yang7f9e7752016-11-28 20:07:07 +0800246 pipe_config->has_audio =
247 intel_ddi_is_audio_enabled(dev_priv, crtc);
248
Dave Airlie0e32b392014-05-02 14:02:48 +1000249 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
250 if (temp & TRANS_DDI_PHSYNC)
251 flags |= DRM_MODE_FLAG_PHSYNC;
252 else
253 flags |= DRM_MODE_FLAG_NHSYNC;
254 if (temp & TRANS_DDI_PVSYNC)
255 flags |= DRM_MODE_FLAG_PVSYNC;
256 else
257 flags |= DRM_MODE_FLAG_NVSYNC;
258
259 switch (temp & TRANS_DDI_BPC_MASK) {
260 case TRANS_DDI_BPC_6:
261 pipe_config->pipe_bpp = 18;
262 break;
263 case TRANS_DDI_BPC_8:
264 pipe_config->pipe_bpp = 24;
265 break;
266 case TRANS_DDI_BPC_10:
267 pipe_config->pipe_bpp = 30;
268 break;
269 case TRANS_DDI_BPC_12:
270 pipe_config->pipe_bpp = 36;
271 break;
272 default:
273 break;
274 }
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200275 pipe_config->base.adjusted_mode.flags |= flags;
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +0300276
277 pipe_config->lane_count =
278 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
279
Dave Airlie0e32b392014-05-02 14:02:48 +1000280 intel_dp_get_m_n(crtc, pipe_config);
281
282 intel_ddi_clock_get(&intel_dig_port->base, pipe_config);
283}
284
285static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
286{
287 struct intel_connector *intel_connector = to_intel_connector(connector);
288 struct intel_dp *intel_dp = intel_connector->mst_port;
289 struct edid *edid;
290 int ret;
291
Dave Airlie0552f762016-03-09 11:14:38 +1000292 if (!intel_dp) {
293 return intel_connector_update_modes(connector, NULL);
294 }
Dave Airlie0e32b392014-05-02 14:02:48 +1000295
Dave Airlie0552f762016-03-09 11:14:38 +1000296 edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
Dave Airlie0e32b392014-05-02 14:02:48 +1000297 ret = intel_connector_update_modes(connector, edid);
298 kfree(edid);
299
300 return ret;
301}
302
303static enum drm_connector_status
Daniel Vetterf7f3d482014-10-22 11:06:44 +0200304intel_dp_mst_detect(struct drm_connector *connector, bool force)
Dave Airlie0e32b392014-05-02 14:02:48 +1000305{
306 struct intel_connector *intel_connector = to_intel_connector(connector);
307 struct intel_dp *intel_dp = intel_connector->mst_port;
308
Dave Airlie0552f762016-03-09 11:14:38 +1000309 if (!intel_dp)
310 return connector_status_disconnected;
Dave Airliec6a0aed2014-10-20 16:28:02 +1000311 return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr, intel_connector->port);
Dave Airlie0e32b392014-05-02 14:02:48 +1000312}
313
Dave Airlie0e32b392014-05-02 14:02:48 +1000314static int
315intel_dp_mst_set_property(struct drm_connector *connector,
316 struct drm_property *property,
317 uint64_t val)
318{
319 return 0;
320}
321
322static void
323intel_dp_mst_connector_destroy(struct drm_connector *connector)
324{
325 struct intel_connector *intel_connector = to_intel_connector(connector);
326
327 if (!IS_ERR_OR_NULL(intel_connector->edid))
328 kfree(intel_connector->edid);
329
330 drm_connector_cleanup(connector);
331 kfree(connector);
332}
333
334static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +0200335 .dpms = drm_atomic_helper_connector_dpms,
Dave Airlie0e32b392014-05-02 14:02:48 +1000336 .detect = intel_dp_mst_detect,
337 .fill_modes = drm_helper_probe_single_connector_modes,
338 .set_property = intel_dp_mst_set_property,
Matt Roper2545e4a2015-01-22 16:51:27 -0800339 .atomic_get_property = intel_connector_atomic_get_property,
Chris Wilson1ebaa0b2016-06-24 14:00:15 +0100340 .late_register = intel_connector_register,
Chris Wilsonc191eca2016-06-17 11:40:33 +0100341 .early_unregister = intel_connector_unregister,
Dave Airlie0e32b392014-05-02 14:02:48 +1000342 .destroy = intel_dp_mst_connector_destroy,
Matt Roperc6f95f22015-01-22 16:50:32 -0800343 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Ander Conselvan de Oliveira98969722015-03-20 16:18:06 +0200344 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Dave Airlie0e32b392014-05-02 14:02:48 +1000345};
346
347static int intel_dp_mst_get_modes(struct drm_connector *connector)
348{
349 return intel_dp_mst_get_ddc_modes(connector);
350}
351
352static enum drm_mode_status
353intel_dp_mst_mode_valid(struct drm_connector *connector,
354 struct drm_display_mode *mode)
355{
Dhinakaran Pandiyan22a2c8e2016-11-15 12:59:06 -0800356 struct intel_connector *intel_connector = to_intel_connector(connector);
357 struct intel_dp *intel_dp = intel_connector->mst_port;
Mika Kahola832d5bf2016-02-02 15:16:40 +0200358 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
Dhinakaran Pandiyan22a2c8e2016-11-15 12:59:06 -0800359 int bpp = 24; /* MST uses fixed bpp */
360 int max_rate, mode_rate, max_lanes, max_link_clock;
361
362 max_link_clock = intel_dp_max_link_rate(intel_dp);
363 max_lanes = drm_dp_max_lane_count(intel_dp->dpcd);
364
365 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
366 mode_rate = intel_dp_link_required(mode->clock, bpp);
Mika Kahola832d5bf2016-02-02 15:16:40 +0200367
Dave Airlie0e32b392014-05-02 14:02:48 +1000368 /* TODO - validate mode against available PBN for link */
369 if (mode->clock < 10000)
370 return MODE_CLOCK_LOW;
371
372 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
373 return MODE_H_ILLEGAL;
374
Dhinakaran Pandiyan22a2c8e2016-11-15 12:59:06 -0800375 if (mode_rate > max_rate || mode->clock > max_dotclk)
Mika Kahola832d5bf2016-02-02 15:16:40 +0200376 return MODE_CLOCK_HIGH;
377
Dave Airlie0e32b392014-05-02 14:02:48 +1000378 return MODE_OK;
379}
380
Daniel Vetter459485a2015-08-03 17:24:09 +0200381static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
382 struct drm_connector_state *state)
383{
384 struct intel_connector *intel_connector = to_intel_connector(connector);
385 struct intel_dp *intel_dp = intel_connector->mst_port;
386 struct intel_crtc *crtc = to_intel_crtc(state->crtc);
387
Dave Airlie0552f762016-03-09 11:14:38 +1000388 if (!intel_dp)
389 return NULL;
Daniel Vetter459485a2015-08-03 17:24:09 +0200390 return &intel_dp->mst_encoders[crtc->pipe]->base.base;
391}
392
Dave Airlie0e32b392014-05-02 14:02:48 +1000393static struct drm_encoder *intel_mst_best_encoder(struct drm_connector *connector)
394{
395 struct intel_connector *intel_connector = to_intel_connector(connector);
396 struct intel_dp *intel_dp = intel_connector->mst_port;
Dave Airlie0552f762016-03-09 11:14:38 +1000397 if (!intel_dp)
398 return NULL;
Dave Airlie0e32b392014-05-02 14:02:48 +1000399 return &intel_dp->mst_encoders[0]->base.base;
400}
401
402static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
403 .get_modes = intel_dp_mst_get_modes,
404 .mode_valid = intel_dp_mst_mode_valid,
Daniel Vetter459485a2015-08-03 17:24:09 +0200405 .atomic_best_encoder = intel_mst_atomic_best_encoder,
Dave Airlie0e32b392014-05-02 14:02:48 +1000406 .best_encoder = intel_mst_best_encoder,
407};
408
409static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
410{
411 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
412
413 drm_encoder_cleanup(encoder);
414 kfree(intel_mst);
415}
416
417static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
418 .destroy = intel_dp_mst_encoder_destroy,
419};
420
421static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
422{
Maarten Lankhorste85376c2015-08-27 13:13:31 +0200423 if (connector->encoder && connector->base.state->crtc) {
Dave Airlie0e32b392014-05-02 14:02:48 +1000424 enum pipe pipe;
425 if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
426 return false;
427 return true;
428 }
429 return false;
430}
431
Chris Wilson7296c842014-07-22 20:10:28 +1000432static void intel_connector_add_to_fbdev(struct intel_connector *connector)
433{
Daniel Vetter06957262015-08-10 13:34:08 +0200434#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilson7296c842014-07-22 20:10:28 +1000435 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Lukas Wunner54632ab2015-11-18 13:43:20 +0100436
437 if (dev_priv->fbdev)
438 drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
439 &connector->base);
Chris Wilson7296c842014-07-22 20:10:28 +1000440#endif
441}
442
443static void intel_connector_remove_from_fbdev(struct intel_connector *connector)
444{
Daniel Vetter06957262015-08-10 13:34:08 +0200445#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilson7296c842014-07-22 20:10:28 +1000446 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Lukas Wunner54632ab2015-11-18 13:43:20 +0100447
448 if (dev_priv->fbdev)
449 drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
450 &connector->base);
Chris Wilson7296c842014-07-22 20:10:28 +1000451#endif
452}
453
Thierry Reding12e6cec2014-05-13 11:38:36 +0200454static struct drm_connector *intel_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, const char *pathprop)
Dave Airlie0e32b392014-05-02 14:02:48 +1000455{
456 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
457 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
458 struct drm_device *dev = intel_dig_port->base.base.dev;
Dave Airlie0e32b392014-05-02 14:02:48 +1000459 struct intel_connector *intel_connector;
460 struct drm_connector *connector;
461 int i;
462
Ander Conselvan de Oliveira9bdbd0b2015-04-10 10:59:10 +0300463 intel_connector = intel_connector_alloc();
Dave Airlie0e32b392014-05-02 14:02:48 +1000464 if (!intel_connector)
465 return NULL;
466
467 connector = &intel_connector->base;
468 drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs, DRM_MODE_CONNECTOR_DisplayPort);
469 drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
470
Dave Airlie0e32b392014-05-02 14:02:48 +1000471 intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
472 intel_connector->mst_port = intel_dp;
473 intel_connector->port = port;
474
475 for (i = PIPE_A; i <= PIPE_C; i++) {
476 drm_mode_connector_attach_encoder(&intel_connector->base,
477 &intel_dp->mst_encoders[i]->base.base);
478 }
479 intel_dp_add_properties(intel_dp, connector);
480
481 drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
Dave Airlie6f134d72014-10-20 16:30:50 +1000482 drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
483
Dave Airlie0e32b392014-05-02 14:02:48 +1000484 drm_mode_connector_set_path_property(connector, pathprop);
Dave Airlied9515c52015-09-16 17:55:23 +1000485 return connector;
486}
487
488static void intel_dp_register_mst_connector(struct drm_connector *connector)
489{
490 struct intel_connector *intel_connector = to_intel_connector(connector);
491 struct drm_device *dev = connector->dev;
Chris Wilson7a418e32016-06-24 14:00:14 +0100492
Daniel Vetter8bb4da12015-07-09 23:44:31 +0200493 drm_modeset_lock_all(dev);
Chris Wilson7296c842014-07-22 20:10:28 +1000494 intel_connector_add_to_fbdev(intel_connector);
Daniel Vetter8bb4da12015-07-09 23:44:31 +0200495 drm_modeset_unlock_all(dev);
Chris Wilson7a418e32016-06-24 14:00:14 +0100496
Dave Airlie0e32b392014-05-02 14:02:48 +1000497 drm_connector_register(&intel_connector->base);
Dave Airlie0e32b392014-05-02 14:02:48 +1000498}
499
500static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
501 struct drm_connector *connector)
502{
503 struct intel_connector *intel_connector = to_intel_connector(connector);
504 struct drm_device *dev = connector->dev;
Maarten Lankhorst20fae982015-08-06 13:47:36 +0200505
Chris Wilsonc191eca2016-06-17 11:40:33 +0100506 drm_connector_unregister(connector);
Lyude1f771752016-03-16 15:18:04 -0400507
Dave Airlie0e32b392014-05-02 14:02:48 +1000508 /* need to nuke the connector */
Daniel Vetter8bb4da12015-07-09 23:44:31 +0200509 drm_modeset_lock_all(dev);
Chris Wilson7296c842014-07-22 20:10:28 +1000510 intel_connector_remove_from_fbdev(intel_connector);
Dave Airlie0552f762016-03-09 11:14:38 +1000511 intel_connector->mst_port = NULL;
Daniel Vetter8bb4da12015-07-09 23:44:31 +0200512 drm_modeset_unlock_all(dev);
Dave Airlie0e32b392014-05-02 14:02:48 +1000513
Dave Airlie0552f762016-03-09 11:14:38 +1000514 drm_connector_unreference(&intel_connector->base);
Dave Airlie0e32b392014-05-02 14:02:48 +1000515 DRM_DEBUG_KMS("\n");
516}
517
518static void intel_dp_mst_hotplug(struct drm_dp_mst_topology_mgr *mgr)
519{
520 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
521 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
522 struct drm_device *dev = intel_dig_port->base.base.dev;
523
524 drm_kms_helper_hotplug_event(dev);
525}
526
Julia Lawall69a0f892015-12-30 22:20:30 +0100527static const struct drm_dp_mst_topology_cbs mst_cbs = {
Dave Airlie0e32b392014-05-02 14:02:48 +1000528 .add_connector = intel_dp_add_mst_connector,
Dave Airlied9515c52015-09-16 17:55:23 +1000529 .register_connector = intel_dp_register_mst_connector,
Dave Airlie0e32b392014-05-02 14:02:48 +1000530 .destroy_connector = intel_dp_destroy_mst_connector,
531 .hotplug = intel_dp_mst_hotplug,
532};
533
534static struct intel_dp_mst_encoder *
535intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
536{
537 struct intel_dp_mst_encoder *intel_mst;
538 struct intel_encoder *intel_encoder;
539 struct drm_device *dev = intel_dig_port->base.base.dev;
540
541 intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
542
543 if (!intel_mst)
544 return NULL;
545
546 intel_mst->pipe = pipe;
547 intel_encoder = &intel_mst->base;
548 intel_mst->primary = intel_dig_port;
549
550 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
Ville Syrjälä580d8ed2016-05-27 20:59:24 +0300551 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
Dave Airlie0e32b392014-05-02 14:02:48 +1000552
553 intel_encoder->type = INTEL_OUTPUT_DP_MST;
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +0200554 intel_encoder->power_domain = intel_dig_port->base.power_domain;
Pandiyan, Dhinakaran03cdc1d2016-09-19 18:24:38 -0700555 intel_encoder->port = intel_dig_port->port;
Dave Airlie0e32b392014-05-02 14:02:48 +1000556 intel_encoder->crtc_mask = 0x7;
557 intel_encoder->cloneable = 0;
558
559 intel_encoder->compute_config = intel_dp_mst_compute_config;
560 intel_encoder->disable = intel_mst_disable_dp;
561 intel_encoder->post_disable = intel_mst_post_disable_dp;
562 intel_encoder->pre_enable = intel_mst_pre_enable_dp;
563 intel_encoder->enable = intel_mst_enable_dp;
564 intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
565 intel_encoder->get_config = intel_dp_mst_enc_get_config;
566
567 return intel_mst;
568
569}
570
571static bool
572intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
573{
574 int i;
575 struct intel_dp *intel_dp = &intel_dig_port->dp;
576
577 for (i = PIPE_A; i <= PIPE_C; i++)
578 intel_dp->mst_encoders[i] = intel_dp_create_fake_mst_encoder(intel_dig_port, i);
579 return true;
580}
581
582int
583intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
584{
585 struct intel_dp *intel_dp = &intel_dig_port->dp;
586 struct drm_device *dev = intel_dig_port->base.base.dev;
587 int ret;
588
589 intel_dp->can_mst = true;
590 intel_dp->mst_mgr.cbs = &mst_cbs;
591
592 /* create encoders */
593 intel_dp_create_fake_mst_encoders(intel_dig_port);
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -0800594 ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, dev,
595 &intel_dp->aux, 16, 3, conn_base_id);
Dave Airlie0e32b392014-05-02 14:02:48 +1000596 if (ret) {
597 intel_dp->can_mst = false;
598 return ret;
599 }
600 return 0;
601}
602
603void
604intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
605{
606 struct intel_dp *intel_dp = &intel_dig_port->dp;
607
608 if (!intel_dp->can_mst)
609 return;
610
611 drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
612 /* encoders will get killed by normal cleanup */
613}