vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / intel_dp_mst.c
blob58ba14966d4f1128ec396da03db01370033f6212
1 /*
2 * Copyright © 2008 Intel Corporation
3 * 2014 Red Hat Inc.
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:
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.
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.
26 #include <drm/drmP.h>
27 #include "i915_drv.h"
28 #include "intel_drv.h"
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_crtc_helper.h>
31 #include <drm/drm_edid.h>
33 static bool intel_dp_mst_compute_config(struct intel_encoder *encoder,
34 struct intel_crtc_state *pipe_config,
35 struct drm_connector_state *conn_state)
37 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
38 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
39 struct intel_digital_port *intel_dig_port = intel_mst->primary;
40 struct intel_dp *intel_dp = &intel_dig_port->dp;
41 struct drm_connector *connector = conn_state->connector;
42 void *port = to_intel_connector(connector)->port;
43 struct drm_atomic_state *state = pipe_config->base.state;
44 int bpp;
45 int lane_count, slots = 0;
46 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
47 int mst_pbn;
48 bool reduce_m_n = drm_dp_has_quirk(&intel_dp->desc,
49 DP_DPCD_QUIRK_LIMITED_M_N);
51 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
52 return false;
54 pipe_config->has_pch_encoder = false;
55 bpp = 24;
56 if (intel_dp->compliance.test_data.bpc) {
57 bpp = intel_dp->compliance.test_data.bpc * 3;
58 DRM_DEBUG_KMS("Setting pipe bpp to %d\n",
59 bpp);
62 * for MST we always configure max link bw - the spec doesn't
63 * seem to suggest we should do otherwise.
65 lane_count = intel_dp_max_lane_count(intel_dp);
67 pipe_config->lane_count = lane_count;
69 pipe_config->pipe_bpp = bpp;
71 pipe_config->port_clock = intel_dp_max_link_rate(intel_dp);
73 if (drm_dp_mst_port_has_audio(&intel_dp->mst_mgr, port))
74 pipe_config->has_audio = true;
76 mst_pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock, bpp);
77 pipe_config->pbn = mst_pbn;
79 /* Zombie connectors can't have VCPI slots */
80 if (READ_ONCE(connector->registered)) {
81 slots = drm_dp_atomic_find_vcpi_slots(state,
82 &intel_dp->mst_mgr,
83 port,
84 mst_pbn);
85 if (slots < 0) {
86 DRM_DEBUG_KMS("failed finding vcpi slots:%d\n",
87 slots);
88 return false;
92 intel_link_compute_m_n(bpp, lane_count,
93 adjusted_mode->crtc_clock,
94 pipe_config->port_clock,
95 &pipe_config->dp_m_n,
96 reduce_m_n);
98 pipe_config->dp_m_n.tu = slots;
100 if (IS_GEN9_LP(dev_priv))
101 pipe_config->lane_lat_optim_mask =
102 bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
104 intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
106 return true;
109 static int intel_dp_mst_atomic_check(struct drm_connector *connector,
110 struct drm_connector_state *new_conn_state)
112 struct drm_atomic_state *state = new_conn_state->state;
113 struct drm_connector_state *old_conn_state;
114 struct drm_crtc *old_crtc;
115 struct drm_crtc_state *crtc_state;
116 int slots, ret = 0;
118 old_conn_state = drm_atomic_get_old_connector_state(state, connector);
119 old_crtc = old_conn_state->crtc;
120 if (!old_crtc)
121 return ret;
123 crtc_state = drm_atomic_get_new_crtc_state(state, old_crtc);
124 slots = to_intel_crtc_state(crtc_state)->dp_m_n.tu;
125 if (drm_atomic_crtc_needs_modeset(crtc_state) && slots > 0) {
126 struct drm_dp_mst_topology_mgr *mgr;
127 struct drm_encoder *old_encoder;
129 old_encoder = old_conn_state->best_encoder;
130 mgr = &enc_to_mst(old_encoder)->primary->dp.mst_mgr;
132 ret = drm_dp_atomic_release_vcpi_slots(state, mgr, slots);
133 if (ret)
134 DRM_DEBUG_KMS("failed releasing %d vcpi slots:%d\n", slots, ret);
135 else
136 to_intel_crtc_state(crtc_state)->dp_m_n.tu = 0;
138 return ret;
141 static void intel_mst_disable_dp(struct intel_encoder *encoder,
142 const struct intel_crtc_state *old_crtc_state,
143 const struct drm_connector_state *old_conn_state)
145 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
146 struct intel_digital_port *intel_dig_port = intel_mst->primary;
147 struct intel_dp *intel_dp = &intel_dig_port->dp;
148 struct intel_connector *connector =
149 to_intel_connector(old_conn_state->connector);
150 int ret;
152 DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
154 drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, connector->port);
156 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
157 if (ret) {
158 DRM_ERROR("failed to update payload %d\n", ret);
160 if (old_crtc_state->has_audio)
161 intel_audio_codec_disable(encoder,
162 old_crtc_state, old_conn_state);
165 static void intel_mst_post_disable_dp(struct intel_encoder *encoder,
166 const struct intel_crtc_state *old_crtc_state,
167 const struct drm_connector_state *old_conn_state)
169 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
170 struct intel_digital_port *intel_dig_port = intel_mst->primary;
171 struct intel_dp *intel_dp = &intel_dig_port->dp;
172 struct intel_connector *connector =
173 to_intel_connector(old_conn_state->connector);
175 intel_ddi_disable_pipe_clock(old_crtc_state);
177 /* this can fail */
178 drm_dp_check_act_status(&intel_dp->mst_mgr);
179 /* and this can also fail */
180 drm_dp_update_payload_part2(&intel_dp->mst_mgr);
182 drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
185 * Power down mst path before disabling the port, otherwise we end
186 * up getting interrupts from the sink upon detecting link loss.
188 drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
189 false);
191 intel_dp->active_mst_links--;
193 intel_mst->connector = NULL;
194 if (intel_dp->active_mst_links == 0) {
195 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
196 intel_dig_port->base.post_disable(&intel_dig_port->base,
197 old_crtc_state, NULL);
200 DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
203 static void intel_mst_pre_pll_enable_dp(struct intel_encoder *encoder,
204 const struct intel_crtc_state *pipe_config,
205 const struct drm_connector_state *conn_state)
207 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
208 struct intel_digital_port *intel_dig_port = intel_mst->primary;
209 struct intel_dp *intel_dp = &intel_dig_port->dp;
211 if (intel_dp->active_mst_links == 0 &&
212 intel_dig_port->base.pre_pll_enable)
213 intel_dig_port->base.pre_pll_enable(&intel_dig_port->base,
214 pipe_config, NULL);
217 static void intel_mst_pre_enable_dp(struct intel_encoder *encoder,
218 const struct intel_crtc_state *pipe_config,
219 const struct drm_connector_state *conn_state)
221 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
222 struct intel_digital_port *intel_dig_port = intel_mst->primary;
223 struct intel_dp *intel_dp = &intel_dig_port->dp;
224 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
225 enum port port = intel_dig_port->base.port;
226 struct intel_connector *connector =
227 to_intel_connector(conn_state->connector);
228 int ret;
229 uint32_t temp;
231 /* MST encoders are bound to a crtc, not to a connector,
232 * force the mapping here for get_hw_state.
234 connector->encoder = encoder;
235 intel_mst->connector = connector;
237 DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
239 if (intel_dp->active_mst_links == 0)
240 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
242 drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
244 if (intel_dp->active_mst_links == 0)
245 intel_dig_port->base.pre_enable(&intel_dig_port->base,
246 pipe_config, NULL);
248 ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
249 connector->port,
250 pipe_config->pbn,
251 pipe_config->dp_m_n.tu);
252 if (ret == false) {
253 DRM_ERROR("failed to allocate vcpi\n");
254 return;
258 intel_dp->active_mst_links++;
259 temp = I915_READ(DP_TP_STATUS(port));
260 I915_WRITE(DP_TP_STATUS(port), temp);
262 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
264 intel_ddi_enable_pipe_clock(pipe_config);
267 static void intel_mst_enable_dp(struct intel_encoder *encoder,
268 const struct intel_crtc_state *pipe_config,
269 const struct drm_connector_state *conn_state)
271 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
272 struct intel_digital_port *intel_dig_port = intel_mst->primary;
273 struct intel_dp *intel_dp = &intel_dig_port->dp;
274 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
275 enum port port = intel_dig_port->base.port;
276 int ret;
278 DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
280 if (intel_wait_for_register(dev_priv,
281 DP_TP_STATUS(port),
282 DP_TP_STATUS_ACT_SENT,
283 DP_TP_STATUS_ACT_SENT,
285 DRM_ERROR("Timed out waiting for ACT sent\n");
287 ret = drm_dp_check_act_status(&intel_dp->mst_mgr);
289 ret = drm_dp_update_payload_part2(&intel_dp->mst_mgr);
290 if (pipe_config->has_audio)
291 intel_audio_codec_enable(encoder, pipe_config, conn_state);
294 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
295 enum pipe *pipe)
297 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
298 *pipe = intel_mst->pipe;
299 if (intel_mst->connector)
300 return true;
301 return false;
304 static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
305 struct intel_crtc_state *pipe_config)
307 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
308 struct intel_digital_port *intel_dig_port = intel_mst->primary;
310 intel_ddi_get_config(&intel_dig_port->base, pipe_config);
313 static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
315 struct intel_connector *intel_connector = to_intel_connector(connector);
316 struct intel_dp *intel_dp = intel_connector->mst_port;
317 struct edid *edid;
318 int ret;
320 if (!READ_ONCE(connector->registered))
321 return intel_connector_update_modes(connector, NULL);
323 edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
324 ret = intel_connector_update_modes(connector, edid);
325 kfree(edid);
327 return ret;
330 static enum drm_connector_status
331 intel_dp_mst_detect(struct drm_connector *connector, bool force)
333 struct intel_connector *intel_connector = to_intel_connector(connector);
334 struct intel_dp *intel_dp = intel_connector->mst_port;
336 if (!READ_ONCE(connector->registered))
337 return connector_status_disconnected;
338 return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr,
339 intel_connector->port);
342 static void
343 intel_dp_mst_connector_destroy(struct drm_connector *connector)
345 struct intel_connector *intel_connector = to_intel_connector(connector);
347 if (!IS_ERR_OR_NULL(intel_connector->edid))
348 kfree(intel_connector->edid);
350 drm_connector_cleanup(connector);
351 kfree(connector);
354 static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
355 .detect = intel_dp_mst_detect,
356 .fill_modes = drm_helper_probe_single_connector_modes,
357 .late_register = intel_connector_register,
358 .early_unregister = intel_connector_unregister,
359 .destroy = intel_dp_mst_connector_destroy,
360 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
361 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
364 static int intel_dp_mst_get_modes(struct drm_connector *connector)
366 return intel_dp_mst_get_ddc_modes(connector);
369 static enum drm_mode_status
370 intel_dp_mst_mode_valid(struct drm_connector *connector,
371 struct drm_display_mode *mode)
373 struct intel_connector *intel_connector = to_intel_connector(connector);
374 struct intel_dp *intel_dp = intel_connector->mst_port;
375 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
376 int bpp = 24; /* MST uses fixed bpp */
377 int max_rate, mode_rate, max_lanes, max_link_clock;
379 if (!READ_ONCE(connector->registered))
380 return MODE_ERROR;
382 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
383 return MODE_NO_DBLESCAN;
385 max_link_clock = intel_dp_max_link_rate(intel_dp);
386 max_lanes = intel_dp_max_lane_count(intel_dp);
388 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
389 mode_rate = intel_dp_link_required(mode->clock, bpp);
391 /* TODO - validate mode against available PBN for link */
392 if (mode->clock < 10000)
393 return MODE_CLOCK_LOW;
395 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
396 return MODE_H_ILLEGAL;
398 if (mode_rate > max_rate || mode->clock > max_dotclk)
399 return MODE_CLOCK_HIGH;
401 return MODE_OK;
404 static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
405 struct drm_connector_state *state)
407 struct intel_connector *intel_connector = to_intel_connector(connector);
408 struct intel_dp *intel_dp = intel_connector->mst_port;
409 struct intel_crtc *crtc = to_intel_crtc(state->crtc);
411 return &intel_dp->mst_encoders[crtc->pipe]->base.base;
414 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
415 .get_modes = intel_dp_mst_get_modes,
416 .mode_valid = intel_dp_mst_mode_valid,
417 .atomic_best_encoder = intel_mst_atomic_best_encoder,
418 .atomic_check = intel_dp_mst_atomic_check,
421 static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
423 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
425 drm_encoder_cleanup(encoder);
426 kfree(intel_mst);
429 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
430 .destroy = intel_dp_mst_encoder_destroy,
433 static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
435 if (connector->encoder && connector->base.state->crtc) {
436 enum pipe pipe;
437 if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
438 return false;
439 return true;
441 return false;
444 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)
446 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
447 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
448 struct drm_device *dev = intel_dig_port->base.base.dev;
449 struct drm_i915_private *dev_priv = to_i915(dev);
450 struct intel_connector *intel_connector;
451 struct drm_connector *connector;
452 enum pipe pipe;
453 int ret;
455 intel_connector = intel_connector_alloc();
456 if (!intel_connector)
457 return NULL;
459 intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
460 intel_connector->mst_port = intel_dp;
461 intel_connector->port = port;
463 connector = &intel_connector->base;
464 ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
465 DRM_MODE_CONNECTOR_DisplayPort);
466 if (ret) {
467 intel_connector_free(intel_connector);
468 return NULL;
471 drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
473 for_each_pipe(dev_priv, pipe) {
474 struct drm_encoder *enc =
475 &intel_dp->mst_encoders[pipe]->base.base;
477 ret = drm_connector_attach_encoder(&intel_connector->base, enc);
478 if (ret)
479 goto err;
482 drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
483 drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
485 ret = drm_connector_set_path_property(connector, pathprop);
486 if (ret)
487 goto err;
489 return connector;
491 err:
492 drm_connector_cleanup(connector);
493 return NULL;
496 static void intel_dp_register_mst_connector(struct drm_connector *connector)
498 struct drm_i915_private *dev_priv = to_i915(connector->dev);
500 if (dev_priv->fbdev)
501 drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
502 connector);
504 drm_connector_register(connector);
507 static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
508 struct drm_connector *connector)
510 struct drm_i915_private *dev_priv = to_i915(connector->dev);
512 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, connector->name);
513 drm_connector_unregister(connector);
515 if (dev_priv->fbdev)
516 drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
517 connector);
519 drm_connector_put(connector);
522 static void intel_dp_mst_hotplug(struct drm_dp_mst_topology_mgr *mgr)
524 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
525 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
526 struct drm_device *dev = intel_dig_port->base.base.dev;
528 drm_kms_helper_hotplug_event(dev);
531 static const struct drm_dp_mst_topology_cbs mst_cbs = {
532 .add_connector = intel_dp_add_mst_connector,
533 .register_connector = intel_dp_register_mst_connector,
534 .destroy_connector = intel_dp_destroy_mst_connector,
535 .hotplug = intel_dp_mst_hotplug,
538 static struct intel_dp_mst_encoder *
539 intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
541 struct intel_dp_mst_encoder *intel_mst;
542 struct intel_encoder *intel_encoder;
543 struct drm_device *dev = intel_dig_port->base.base.dev;
545 intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
547 if (!intel_mst)
548 return NULL;
550 intel_mst->pipe = pipe;
551 intel_encoder = &intel_mst->base;
552 intel_mst->primary = intel_dig_port;
554 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
555 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
557 intel_encoder->type = INTEL_OUTPUT_DP_MST;
558 intel_encoder->power_domain = intel_dig_port->base.power_domain;
559 intel_encoder->port = intel_dig_port->base.port;
560 intel_encoder->crtc_mask = 0x7;
561 intel_encoder->cloneable = 0;
563 intel_encoder->compute_config = intel_dp_mst_compute_config;
564 intel_encoder->disable = intel_mst_disable_dp;
565 intel_encoder->post_disable = intel_mst_post_disable_dp;
566 intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
567 intel_encoder->pre_enable = intel_mst_pre_enable_dp;
568 intel_encoder->enable = intel_mst_enable_dp;
569 intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
570 intel_encoder->get_config = intel_dp_mst_enc_get_config;
572 return intel_mst;
576 static bool
577 intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
579 struct intel_dp *intel_dp = &intel_dig_port->dp;
580 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
581 enum pipe pipe;
583 for_each_pipe(dev_priv, pipe)
584 intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(intel_dig_port, pipe);
585 return true;
589 intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
591 struct intel_dp *intel_dp = &intel_dig_port->dp;
592 struct drm_device *dev = intel_dig_port->base.base.dev;
593 int ret;
595 intel_dp->can_mst = true;
596 intel_dp->mst_mgr.cbs = &mst_cbs;
598 /* create encoders */
599 intel_dp_create_fake_mst_encoders(intel_dig_port);
600 ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, dev,
601 &intel_dp->aux, 16, 3, conn_base_id);
602 if (ret) {
603 intel_dp->can_mst = false;
604 return ret;
606 return 0;
609 void
610 intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
612 struct intel_dp *intel_dp = &intel_dig_port->dp;
614 if (!intel_dp->can_mst)
615 return;
617 drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
618 /* encoders will get killed by normal cleanup */