blob: 3715386e427270bc7409224cd2312c7de9a28896 [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);
Pandiyan, Dhinakaranf424f552017-04-28 16:14:20 -070042 struct drm_atomic_state *state = pipe_config->base.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 */
Jani Nikula3d65a732017-04-06 16:44:14 +030059 lane_count = intel_dp_max_lane_count(intel_dp);
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;
Dave Airlie0e32b392014-05-02 14:02:48 +100064
Pandiyan, Dhinakaranf424f552017-04-28 16:14:20 -070065 pipe_config->port_clock = intel_dp_max_link_rate(intel_dp);
Ander Conselvan de Oliveirae75f4772015-03-20 16:18:13 +020066
Libin Yang7f9e7752016-11-28 20:07:07 +080067 if (drm_dp_mst_port_has_audio(&intel_dp->mst_mgr, connector->port))
68 pipe_config->has_audio = true;
Dave Airlie0e32b392014-05-02 14:02:48 +100069
Pandiyan, Dhinakaranf424f552017-04-28 16:14:20 -070070 mst_pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock, bpp);
Dave Airlie0e32b392014-05-02 14:02:48 +100071 pipe_config->pbn = mst_pbn;
Pandiyan, Dhinakaranf424f552017-04-28 16:14:20 -070072
73 slots = drm_dp_atomic_find_vcpi_slots(state, &intel_dp->mst_mgr,
74 connector->port, mst_pbn);
75 if (slots < 0) {
76 DRM_DEBUG_KMS("failed finding vcpi slots:%d\n", slots);
77 return false;
78 }
Dave Airlie0e32b392014-05-02 14:02:48 +100079
80 intel_link_compute_m_n(bpp, lane_count,
81 adjusted_mode->crtc_clock,
82 pipe_config->port_clock,
83 &pipe_config->dp_m_n);
84
85 pipe_config->dp_m_n.tu = slots;
Ander Conselvan de Oliveira6fa2d192015-08-31 11:23:28 +030086
Dave Airlie0e32b392014-05-02 14:02:48 +100087 return true;
Pandiyan, Dhinakaranf424f552017-04-28 16:14:20 -070088}
Dave Airlie0e32b392014-05-02 14:02:48 +100089
Pandiyan, Dhinakaranf424f552017-04-28 16:14:20 -070090static int intel_dp_mst_atomic_check(struct drm_connector *connector,
91 struct drm_connector_state *new_conn_state)
92{
93 struct drm_atomic_state *state = new_conn_state->state;
94 struct drm_connector_state *old_conn_state;
95 struct drm_crtc *old_crtc;
96 struct drm_crtc_state *crtc_state;
97 int slots, ret = 0;
98
99 old_conn_state = drm_atomic_get_old_connector_state(state, connector);
100 old_crtc = old_conn_state->crtc;
101 if (!old_crtc)
102 return ret;
103
104 crtc_state = drm_atomic_get_new_crtc_state(state, old_crtc);
105 slots = to_intel_crtc_state(crtc_state)->dp_m_n.tu;
106 if (drm_atomic_crtc_needs_modeset(crtc_state) && slots > 0) {
107 struct drm_dp_mst_topology_mgr *mgr;
108 struct drm_encoder *old_encoder;
109
110 old_encoder = old_conn_state->best_encoder;
111 mgr = &enc_to_mst(old_encoder)->primary->dp.mst_mgr;
112
113 ret = drm_dp_atomic_release_vcpi_slots(state, mgr, slots);
114 if (ret)
115 DRM_DEBUG_KMS("failed releasing %d vcpi slots:%d\n", slots, ret);
116 else
117 to_intel_crtc_state(crtc_state)->dp_m_n.tu = 0;
118 }
119 return ret;
Dave Airlie0e32b392014-05-02 14:02:48 +1000120}
121
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200122static void intel_mst_disable_dp(struct intel_encoder *encoder,
123 struct intel_crtc_state *old_crtc_state,
124 struct drm_connector_state *old_conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +1000125{
126 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
127 struct intel_digital_port *intel_dig_port = intel_mst->primary;
128 struct intel_dp *intel_dp = &intel_dig_port->dp;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200129 struct intel_connector *connector =
130 to_intel_connector(old_conn_state->connector);
Dave Airlie0e32b392014-05-02 14:02:48 +1000131 int ret;
132
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300133 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
Dave Airlie0e32b392014-05-02 14:02:48 +1000134
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200135 drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, connector->port);
Dave Airlie0e32b392014-05-02 14:02:48 +1000136
137 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
138 if (ret) {
139 DRM_ERROR("failed to update payload %d\n", ret);
140 }
Maarten Lankhorst37255d82016-12-15 15:29:43 +0100141 if (old_crtc_state->has_audio)
Libin Yang7f9e7752016-11-28 20:07:07 +0800142 intel_audio_codec_disable(encoder);
Dave Airlie0e32b392014-05-02 14:02:48 +1000143}
144
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200145static void intel_mst_post_disable_dp(struct intel_encoder *encoder,
146 struct intel_crtc_state *old_crtc_state,
147 struct drm_connector_state *old_conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +1000148{
149 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
150 struct intel_digital_port *intel_dig_port = intel_mst->primary;
151 struct intel_dp *intel_dp = &intel_dig_port->dp;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200152 struct intel_connector *connector =
153 to_intel_connector(old_conn_state->connector);
Dave Airlie0e32b392014-05-02 14:02:48 +1000154
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300155 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
Dave Airlie0e32b392014-05-02 14:02:48 +1000156
157 /* this can fail */
158 drm_dp_check_act_status(&intel_dp->mst_mgr);
159 /* and this can also fail */
160 drm_dp_update_payload_part2(&intel_dp->mst_mgr);
161
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200162 drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
Dave Airlie0e32b392014-05-02 14:02:48 +1000163
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300164 intel_dp->active_mst_links--;
Dave Airlie0552f762016-03-09 11:14:38 +1000165
166 intel_mst->connector = NULL;
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300167 if (intel_dp->active_mst_links == 0) {
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200168 intel_dig_port->base.post_disable(&intel_dig_port->base,
169 NULL, NULL);
170
Dave Airlie0e32b392014-05-02 14:02:48 +1000171 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
172 }
173}
174
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200175static void intel_mst_pre_enable_dp(struct intel_encoder *encoder,
176 struct intel_crtc_state *pipe_config,
177 struct drm_connector_state *conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +1000178{
179 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
180 struct intel_digital_port *intel_dig_port = intel_mst->primary;
181 struct intel_dp *intel_dp = &intel_dig_port->dp;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200182 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Dave Airlie0e32b392014-05-02 14:02:48 +1000183 enum port port = intel_dig_port->port;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200184 struct intel_connector *connector =
185 to_intel_connector(conn_state->connector);
Dave Airlie0e32b392014-05-02 14:02:48 +1000186 int ret;
187 uint32_t temp;
Dave Airlie0e32b392014-05-02 14:02:48 +1000188
Maarten Lankhorste85376c2015-08-27 13:13:31 +0200189 /* MST encoders are bound to a crtc, not to a connector,
190 * force the mapping here for get_hw_state.
191 */
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200192 connector->encoder = encoder;
193 intel_mst->connector = connector;
Maarten Lankhorste85376c2015-08-27 13:13:31 +0200194
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300195 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
Dave Airlie0552f762016-03-09 11:14:38 +1000196
Ander Conselvan de Oliveirae081c842017-03-02 14:58:57 +0200197 if (intel_dp->active_mst_links == 0)
198 intel_dig_port->base.pre_enable(&intel_dig_port->base,
199 pipe_config, NULL);
Dave Airlie0e32b392014-05-02 14:02:48 +1000200
201 ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200202 connector->port,
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -0700203 pipe_config->pbn,
204 pipe_config->dp_m_n.tu);
Dave Airlie0e32b392014-05-02 14:02:48 +1000205 if (ret == false) {
206 DRM_ERROR("failed to allocate vcpi\n");
207 return;
208 }
209
210
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300211 intel_dp->active_mst_links++;
Dave Airlie0e32b392014-05-02 14:02:48 +1000212 temp = I915_READ(DP_TP_STATUS(port));
213 I915_WRITE(DP_TP_STATUS(port), temp);
214
215 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
216}
217
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +0200218static void intel_mst_enable_dp(struct intel_encoder *encoder,
219 struct intel_crtc_state *pipe_config,
220 struct drm_connector_state *conn_state)
Dave Airlie0e32b392014-05-02 14:02:48 +1000221{
222 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
223 struct intel_digital_port *intel_dig_port = intel_mst->primary;
224 struct intel_dp *intel_dp = &intel_dig_port->dp;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200225 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Dave Airlie0e32b392014-05-02 14:02:48 +1000226 enum port port = intel_dig_port->port;
227 int ret;
228
Ville Syrjälä19e0b4c2016-08-05 19:05:42 +0300229 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
Dave Airlie0e32b392014-05-02 14:02:48 +1000230
Chris Wilson3016a312016-06-30 15:33:11 +0100231 if (intel_wait_for_register(dev_priv,
232 DP_TP_STATUS(port),
233 DP_TP_STATUS_ACT_SENT,
234 DP_TP_STATUS_ACT_SENT,
235 1))
Dave Airlie0e32b392014-05-02 14:02:48 +1000236 DRM_ERROR("Timed out waiting for ACT sent\n");
237
238 ret = drm_dp_check_act_status(&intel_dp->mst_mgr);
239
240 ret = drm_dp_update_payload_part2(&intel_dp->mst_mgr);
Maarten Lankhorst37255d82016-12-15 15:29:43 +0100241 if (pipe_config->has_audio)
Libin Yang7f9e7752016-11-28 20:07:07 +0800242 intel_audio_codec_enable(encoder, pipe_config, conn_state);
Dave Airlie0e32b392014-05-02 14:02:48 +1000243}
244
245static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
246 enum pipe *pipe)
247{
248 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
249 *pipe = intel_mst->pipe;
Dave Airlie0552f762016-03-09 11:14:38 +1000250 if (intel_mst->connector)
Dave Airlie0e32b392014-05-02 14:02:48 +1000251 return true;
252 return false;
253}
254
255static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200256 struct intel_crtc_state *pipe_config)
Dave Airlie0e32b392014-05-02 14:02:48 +1000257{
258 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
259 struct intel_digital_port *intel_dig_port = intel_mst->primary;
Maarten Lankhorst1e7bfa02016-08-09 17:04:12 +0200260 struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
261 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Ander Conselvan de Oliveira0cb09a92015-01-30 12:17:23 +0200262 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
Dave Airlie0e32b392014-05-02 14:02:48 +1000263 u32 temp, flags = 0;
264
Libin Yang7f9e7752016-11-28 20:07:07 +0800265 pipe_config->has_audio =
266 intel_ddi_is_audio_enabled(dev_priv, crtc);
267
Dave Airlie0e32b392014-05-02 14:02:48 +1000268 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
269 if (temp & TRANS_DDI_PHSYNC)
270 flags |= DRM_MODE_FLAG_PHSYNC;
271 else
272 flags |= DRM_MODE_FLAG_NHSYNC;
273 if (temp & TRANS_DDI_PVSYNC)
274 flags |= DRM_MODE_FLAG_PVSYNC;
275 else
276 flags |= DRM_MODE_FLAG_NVSYNC;
277
278 switch (temp & TRANS_DDI_BPC_MASK) {
279 case TRANS_DDI_BPC_6:
280 pipe_config->pipe_bpp = 18;
281 break;
282 case TRANS_DDI_BPC_8:
283 pipe_config->pipe_bpp = 24;
284 break;
285 case TRANS_DDI_BPC_10:
286 pipe_config->pipe_bpp = 30;
287 break;
288 case TRANS_DDI_BPC_12:
289 pipe_config->pipe_bpp = 36;
290 break;
291 default:
292 break;
293 }
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200294 pipe_config->base.adjusted_mode.flags |= flags;
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +0300295
296 pipe_config->lane_count =
297 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
298
Dave Airlie0e32b392014-05-02 14:02:48 +1000299 intel_dp_get_m_n(crtc, pipe_config);
300
301 intel_ddi_clock_get(&intel_dig_port->base, pipe_config);
302}
303
304static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
305{
306 struct intel_connector *intel_connector = to_intel_connector(connector);
307 struct intel_dp *intel_dp = intel_connector->mst_port;
308 struct edid *edid;
309 int ret;
310
Dave Airlie0552f762016-03-09 11:14:38 +1000311 if (!intel_dp) {
312 return intel_connector_update_modes(connector, NULL);
313 }
Dave Airlie0e32b392014-05-02 14:02:48 +1000314
Dave Airlie0552f762016-03-09 11:14:38 +1000315 edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
Dave Airlie0e32b392014-05-02 14:02:48 +1000316 ret = intel_connector_update_modes(connector, edid);
317 kfree(edid);
318
319 return ret;
320}
321
322static enum drm_connector_status
Daniel Vetterf7f3d482014-10-22 11:06:44 +0200323intel_dp_mst_detect(struct drm_connector *connector, bool force)
Dave Airlie0e32b392014-05-02 14:02:48 +1000324{
325 struct intel_connector *intel_connector = to_intel_connector(connector);
326 struct intel_dp *intel_dp = intel_connector->mst_port;
327
Dave Airlie0552f762016-03-09 11:14:38 +1000328 if (!intel_dp)
329 return connector_status_disconnected;
Dave Airliec6a0aed2014-10-20 16:28:02 +1000330 return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr, intel_connector->port);
Dave Airlie0e32b392014-05-02 14:02:48 +1000331}
332
Dave Airlie0e32b392014-05-02 14:02:48 +1000333static void
334intel_dp_mst_connector_destroy(struct drm_connector *connector)
335{
336 struct intel_connector *intel_connector = to_intel_connector(connector);
337
338 if (!IS_ERR_OR_NULL(intel_connector->edid))
339 kfree(intel_connector->edid);
340
341 drm_connector_cleanup(connector);
342 kfree(connector);
343}
344
345static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +0200346 .dpms = drm_atomic_helper_connector_dpms,
Dave Airlie0e32b392014-05-02 14:02:48 +1000347 .detect = intel_dp_mst_detect,
348 .fill_modes = drm_helper_probe_single_connector_modes,
Maarten Lankhorst2e222ed2017-04-10 11:07:09 +0200349 .set_property = drm_atomic_helper_connector_set_property,
Chris Wilson1ebaa0b2016-06-24 14:00:15 +0100350 .late_register = intel_connector_register,
Chris Wilsonc191eca2016-06-17 11:40:33 +0100351 .early_unregister = intel_connector_unregister,
Dave Airlie0e32b392014-05-02 14:02:48 +1000352 .destroy = intel_dp_mst_connector_destroy,
Matt Roperc6f95f22015-01-22 16:50:32 -0800353 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Ander Conselvan de Oliveira98969722015-03-20 16:18:06 +0200354 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
Dave Airlie0e32b392014-05-02 14:02:48 +1000355};
356
357static int intel_dp_mst_get_modes(struct drm_connector *connector)
358{
359 return intel_dp_mst_get_ddc_modes(connector);
360}
361
362static enum drm_mode_status
363intel_dp_mst_mode_valid(struct drm_connector *connector,
364 struct drm_display_mode *mode)
365{
Dhinakaran Pandiyan22a2c8e2016-11-15 12:59:06 -0800366 struct intel_connector *intel_connector = to_intel_connector(connector);
367 struct intel_dp *intel_dp = intel_connector->mst_port;
Mika Kahola832d5bf2016-02-02 15:16:40 +0200368 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
Dhinakaran Pandiyan22a2c8e2016-11-15 12:59:06 -0800369 int bpp = 24; /* MST uses fixed bpp */
370 int max_rate, mode_rate, max_lanes, max_link_clock;
371
372 max_link_clock = intel_dp_max_link_rate(intel_dp);
Jani Nikula3d65a732017-04-06 16:44:14 +0300373 max_lanes = intel_dp_max_lane_count(intel_dp);
Dhinakaran Pandiyan22a2c8e2016-11-15 12:59:06 -0800374
375 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
376 mode_rate = intel_dp_link_required(mode->clock, bpp);
Mika Kahola832d5bf2016-02-02 15:16:40 +0200377
Dave Airlie0e32b392014-05-02 14:02:48 +1000378 /* TODO - validate mode against available PBN for link */
379 if (mode->clock < 10000)
380 return MODE_CLOCK_LOW;
381
382 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
383 return MODE_H_ILLEGAL;
384
Dhinakaran Pandiyan22a2c8e2016-11-15 12:59:06 -0800385 if (mode_rate > max_rate || mode->clock > max_dotclk)
Mika Kahola832d5bf2016-02-02 15:16:40 +0200386 return MODE_CLOCK_HIGH;
387
Dave Airlie0e32b392014-05-02 14:02:48 +1000388 return MODE_OK;
389}
390
Daniel Vetter459485a2015-08-03 17:24:09 +0200391static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
392 struct drm_connector_state *state)
393{
394 struct intel_connector *intel_connector = to_intel_connector(connector);
395 struct intel_dp *intel_dp = intel_connector->mst_port;
396 struct intel_crtc *crtc = to_intel_crtc(state->crtc);
397
Dave Airlie0552f762016-03-09 11:14:38 +1000398 if (!intel_dp)
399 return NULL;
Daniel Vetter459485a2015-08-03 17:24:09 +0200400 return &intel_dp->mst_encoders[crtc->pipe]->base.base;
401}
402
Dave Airlie0e32b392014-05-02 14:02:48 +1000403static struct drm_encoder *intel_mst_best_encoder(struct drm_connector *connector)
404{
405 struct intel_connector *intel_connector = to_intel_connector(connector);
406 struct intel_dp *intel_dp = intel_connector->mst_port;
Dave Airlie0552f762016-03-09 11:14:38 +1000407 if (!intel_dp)
408 return NULL;
Dave Airlie0e32b392014-05-02 14:02:48 +1000409 return &intel_dp->mst_encoders[0]->base.base;
410}
411
412static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
413 .get_modes = intel_dp_mst_get_modes,
414 .mode_valid = intel_dp_mst_mode_valid,
Daniel Vetter459485a2015-08-03 17:24:09 +0200415 .atomic_best_encoder = intel_mst_atomic_best_encoder,
Dave Airlie0e32b392014-05-02 14:02:48 +1000416 .best_encoder = intel_mst_best_encoder,
Pandiyan, Dhinakaranf424f552017-04-28 16:14:20 -0700417 .atomic_check = intel_dp_mst_atomic_check,
Dave Airlie0e32b392014-05-02 14:02:48 +1000418};
419
420static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
421{
422 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
423
424 drm_encoder_cleanup(encoder);
425 kfree(intel_mst);
426}
427
428static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
429 .destroy = intel_dp_mst_encoder_destroy,
430};
431
432static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
433{
Maarten Lankhorste85376c2015-08-27 13:13:31 +0200434 if (connector->encoder && connector->base.state->crtc) {
Dave Airlie0e32b392014-05-02 14:02:48 +1000435 enum pipe pipe;
436 if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
437 return false;
438 return true;
439 }
440 return false;
441}
442
Chris Wilson7296c842014-07-22 20:10:28 +1000443static void intel_connector_add_to_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_add_one_connector(&dev_priv->fbdev->helper,
450 &connector->base);
Chris Wilson7296c842014-07-22 20:10:28 +1000451#endif
452}
453
454static void intel_connector_remove_from_fbdev(struct intel_connector *connector)
455{
Daniel Vetter06957262015-08-10 13:34:08 +0200456#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilson7296c842014-07-22 20:10:28 +1000457 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Lukas Wunner54632ab2015-11-18 13:43:20 +0100458
459 if (dev_priv->fbdev)
460 drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
461 &connector->base);
Chris Wilson7296c842014-07-22 20:10:28 +1000462#endif
463}
464
Thierry Reding12e6cec2014-05-13 11:38:36 +0200465static 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 +1000466{
467 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
468 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
469 struct drm_device *dev = intel_dig_port->base.base.dev;
Dave Airlie0e32b392014-05-02 14:02:48 +1000470 struct intel_connector *intel_connector;
471 struct drm_connector *connector;
472 int i;
473
Ander Conselvan de Oliveira9bdbd0b2015-04-10 10:59:10 +0300474 intel_connector = intel_connector_alloc();
Dave Airlie0e32b392014-05-02 14:02:48 +1000475 if (!intel_connector)
476 return NULL;
477
478 connector = &intel_connector->base;
479 drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs, DRM_MODE_CONNECTOR_DisplayPort);
480 drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
481
Dave Airlie0e32b392014-05-02 14:02:48 +1000482 intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
483 intel_connector->mst_port = intel_dp;
484 intel_connector->port = port;
485
486 for (i = PIPE_A; i <= PIPE_C; i++) {
487 drm_mode_connector_attach_encoder(&intel_connector->base,
488 &intel_dp->mst_encoders[i]->base.base);
489 }
Dave Airlie0e32b392014-05-02 14:02:48 +1000490
491 drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
Dave Airlie6f134d72014-10-20 16:30:50 +1000492 drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
493
Dave Airlie0e32b392014-05-02 14:02:48 +1000494 drm_mode_connector_set_path_property(connector, pathprop);
Dave Airlied9515c52015-09-16 17:55:23 +1000495 return connector;
496}
497
498static void intel_dp_register_mst_connector(struct drm_connector *connector)
499{
500 struct intel_connector *intel_connector = to_intel_connector(connector);
501 struct drm_device *dev = connector->dev;
Chris Wilson7a418e32016-06-24 14:00:14 +0100502
Daniel Vetter8bb4da12015-07-09 23:44:31 +0200503 drm_modeset_lock_all(dev);
Chris Wilson7296c842014-07-22 20:10:28 +1000504 intel_connector_add_to_fbdev(intel_connector);
Daniel Vetter8bb4da12015-07-09 23:44:31 +0200505 drm_modeset_unlock_all(dev);
Chris Wilson7a418e32016-06-24 14:00:14 +0100506
Dave Airlie0e32b392014-05-02 14:02:48 +1000507 drm_connector_register(&intel_connector->base);
Dave Airlie0e32b392014-05-02 14:02:48 +1000508}
509
510static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
511 struct drm_connector *connector)
512{
513 struct intel_connector *intel_connector = to_intel_connector(connector);
514 struct drm_device *dev = connector->dev;
Maarten Lankhorst20fae982015-08-06 13:47:36 +0200515
Chris Wilsonc191eca2016-06-17 11:40:33 +0100516 drm_connector_unregister(connector);
Lyude1f771752016-03-16 15:18:04 -0400517
Dave Airlie0e32b392014-05-02 14:02:48 +1000518 /* need to nuke the connector */
Daniel Vetter8bb4da12015-07-09 23:44:31 +0200519 drm_modeset_lock_all(dev);
Chris Wilson7296c842014-07-22 20:10:28 +1000520 intel_connector_remove_from_fbdev(intel_connector);
Dave Airlie0552f762016-03-09 11:14:38 +1000521 intel_connector->mst_port = NULL;
Daniel Vetter8bb4da12015-07-09 23:44:31 +0200522 drm_modeset_unlock_all(dev);
Dave Airlie0e32b392014-05-02 14:02:48 +1000523
Dave Airlie0552f762016-03-09 11:14:38 +1000524 drm_connector_unreference(&intel_connector->base);
Dave Airlie0e32b392014-05-02 14:02:48 +1000525 DRM_DEBUG_KMS("\n");
526}
527
528static void intel_dp_mst_hotplug(struct drm_dp_mst_topology_mgr *mgr)
529{
530 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
531 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
532 struct drm_device *dev = intel_dig_port->base.base.dev;
533
534 drm_kms_helper_hotplug_event(dev);
535}
536
Julia Lawall69a0f892015-12-30 22:20:30 +0100537static const struct drm_dp_mst_topology_cbs mst_cbs = {
Dave Airlie0e32b392014-05-02 14:02:48 +1000538 .add_connector = intel_dp_add_mst_connector,
Dave Airlied9515c52015-09-16 17:55:23 +1000539 .register_connector = intel_dp_register_mst_connector,
Dave Airlie0e32b392014-05-02 14:02:48 +1000540 .destroy_connector = intel_dp_destroy_mst_connector,
541 .hotplug = intel_dp_mst_hotplug,
542};
543
544static struct intel_dp_mst_encoder *
545intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
546{
547 struct intel_dp_mst_encoder *intel_mst;
548 struct intel_encoder *intel_encoder;
549 struct drm_device *dev = intel_dig_port->base.base.dev;
550
551 intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
552
553 if (!intel_mst)
554 return NULL;
555
556 intel_mst->pipe = pipe;
557 intel_encoder = &intel_mst->base;
558 intel_mst->primary = intel_dig_port;
559
560 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
Ville Syrjälä580d8ed2016-05-27 20:59:24 +0300561 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
Dave Airlie0e32b392014-05-02 14:02:48 +1000562
563 intel_encoder->type = INTEL_OUTPUT_DP_MST;
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +0200564 intel_encoder->power_domain = intel_dig_port->base.power_domain;
Pandiyan, Dhinakaran03cdc1d2016-09-19 18:24:38 -0700565 intel_encoder->port = intel_dig_port->port;
Dave Airlie0e32b392014-05-02 14:02:48 +1000566 intel_encoder->crtc_mask = 0x7;
567 intel_encoder->cloneable = 0;
568
569 intel_encoder->compute_config = intel_dp_mst_compute_config;
570 intel_encoder->disable = intel_mst_disable_dp;
571 intel_encoder->post_disable = intel_mst_post_disable_dp;
572 intel_encoder->pre_enable = intel_mst_pre_enable_dp;
573 intel_encoder->enable = intel_mst_enable_dp;
574 intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
575 intel_encoder->get_config = intel_dp_mst_enc_get_config;
576
577 return intel_mst;
578
579}
580
581static bool
582intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
583{
584 int i;
585 struct intel_dp *intel_dp = &intel_dig_port->dp;
586
587 for (i = PIPE_A; i <= PIPE_C; i++)
588 intel_dp->mst_encoders[i] = intel_dp_create_fake_mst_encoder(intel_dig_port, i);
589 return true;
590}
591
592int
593intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
594{
595 struct intel_dp *intel_dp = &intel_dig_port->dp;
596 struct drm_device *dev = intel_dig_port->base.base.dev;
597 int ret;
598
599 intel_dp->can_mst = true;
600 intel_dp->mst_mgr.cbs = &mst_cbs;
601
602 /* create encoders */
603 intel_dp_create_fake_mst_encoders(intel_dig_port);
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -0800604 ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, dev,
605 &intel_dp->aux, 16, 3, conn_base_id);
Dave Airlie0e32b392014-05-02 14:02:48 +1000606 if (ret) {
607 intel_dp->can_mst = false;
608 return ret;
609 }
610 return 0;
611}
612
613void
614intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
615{
616 struct intel_dp *intel_dp = &intel_dig_port->dp;
617
618 if (!intel_dp->can_mst)
619 return;
620
621 drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
622 /* encoders will get killed by normal cleanup */
623}