2 * Copyright © 2008 Intel Corporation
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
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
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 intel_connector
*connector
=
42 to_intel_connector(conn_state
->connector
);
43 struct drm_atomic_state
*state
= pipe_config
->base
.state
;
45 int lane_count
, slots
;
46 const struct drm_display_mode
*adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
48 bool reduce_m_n
= drm_dp_has_quirk(&intel_dp
->desc
,
49 DP_DPCD_QUIRK_LIMITED_M_N
);
51 pipe_config
->has_pch_encoder
= false;
53 if (intel_dp
->compliance
.test_data
.bpc
) {
54 bpp
= intel_dp
->compliance
.test_data
.bpc
* 3;
55 DRM_DEBUG_KMS("Setting pipe bpp to %d\n",
59 * for MST we always configure max link bw - the spec doesn't
60 * seem to suggest we should do otherwise.
62 lane_count
= intel_dp_max_lane_count(intel_dp
);
64 pipe_config
->lane_count
= lane_count
;
66 pipe_config
->pipe_bpp
= bpp
;
68 pipe_config
->port_clock
= intel_dp_max_link_rate(intel_dp
);
70 if (drm_dp_mst_port_has_audio(&intel_dp
->mst_mgr
, connector
->port
))
71 pipe_config
->has_audio
= true;
73 mst_pbn
= drm_dp_calc_pbn_mode(adjusted_mode
->crtc_clock
, bpp
);
74 pipe_config
->pbn
= mst_pbn
;
76 slots
= drm_dp_atomic_find_vcpi_slots(state
, &intel_dp
->mst_mgr
,
77 connector
->port
, mst_pbn
);
79 DRM_DEBUG_KMS("failed finding vcpi slots:%d\n", slots
);
83 intel_link_compute_m_n(bpp
, lane_count
,
84 adjusted_mode
->crtc_clock
,
85 pipe_config
->port_clock
,
89 pipe_config
->dp_m_n
.tu
= slots
;
91 if (IS_GEN9_LP(dev_priv
))
92 pipe_config
->lane_lat_optim_mask
=
93 bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config
->lane_count
);
95 intel_ddi_compute_min_voltage_level(dev_priv
, pipe_config
);
100 static int intel_dp_mst_atomic_check(struct drm_connector
*connector
,
101 struct drm_connector_state
*new_conn_state
)
103 struct drm_atomic_state
*state
= new_conn_state
->state
;
104 struct drm_connector_state
*old_conn_state
;
105 struct drm_crtc
*old_crtc
;
106 struct drm_crtc_state
*crtc_state
;
109 old_conn_state
= drm_atomic_get_old_connector_state(state
, connector
);
110 old_crtc
= old_conn_state
->crtc
;
114 crtc_state
= drm_atomic_get_new_crtc_state(state
, old_crtc
);
115 slots
= to_intel_crtc_state(crtc_state
)->dp_m_n
.tu
;
116 if (drm_atomic_crtc_needs_modeset(crtc_state
) && slots
> 0) {
117 struct drm_dp_mst_topology_mgr
*mgr
;
118 struct drm_encoder
*old_encoder
;
120 old_encoder
= old_conn_state
->best_encoder
;
121 mgr
= &enc_to_mst(old_encoder
)->primary
->dp
.mst_mgr
;
123 ret
= drm_dp_atomic_release_vcpi_slots(state
, mgr
, slots
);
125 DRM_DEBUG_KMS("failed releasing %d vcpi slots:%d\n", slots
, ret
);
127 to_intel_crtc_state(crtc_state
)->dp_m_n
.tu
= 0;
132 static void intel_mst_disable_dp(struct intel_encoder
*encoder
,
133 const struct intel_crtc_state
*old_crtc_state
,
134 const struct drm_connector_state
*old_conn_state
)
136 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
137 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
138 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
139 struct intel_connector
*connector
=
140 to_intel_connector(old_conn_state
->connector
);
143 DRM_DEBUG_KMS("active links %d\n", intel_dp
->active_mst_links
);
145 drm_dp_mst_reset_vcpi_slots(&intel_dp
->mst_mgr
, connector
->port
);
147 ret
= drm_dp_update_payload_part1(&intel_dp
->mst_mgr
);
149 DRM_ERROR("failed to update payload %d\n", ret
);
151 if (old_crtc_state
->has_audio
)
152 intel_audio_codec_disable(encoder
,
153 old_crtc_state
, old_conn_state
);
156 static void intel_mst_post_disable_dp(struct intel_encoder
*encoder
,
157 const struct intel_crtc_state
*old_crtc_state
,
158 const struct drm_connector_state
*old_conn_state
)
160 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
161 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
162 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
163 struct intel_connector
*connector
=
164 to_intel_connector(old_conn_state
->connector
);
167 drm_dp_check_act_status(&intel_dp
->mst_mgr
);
168 /* and this can also fail */
169 drm_dp_update_payload_part2(&intel_dp
->mst_mgr
);
171 drm_dp_mst_deallocate_vcpi(&intel_dp
->mst_mgr
, connector
->port
);
174 * Power down mst path before disabling the port, otherwise we end
175 * up getting interrupts from the sink upon detecting link loss.
177 drm_dp_send_power_updown_phy(&intel_dp
->mst_mgr
, connector
->port
,
180 intel_dp
->active_mst_links
--;
182 intel_mst
->connector
= NULL
;
183 if (intel_dp
->active_mst_links
== 0)
184 intel_dig_port
->base
.post_disable(&intel_dig_port
->base
,
185 old_crtc_state
, NULL
);
187 DRM_DEBUG_KMS("active links %d\n", intel_dp
->active_mst_links
);
190 static void intel_mst_pre_pll_enable_dp(struct intel_encoder
*encoder
,
191 const struct intel_crtc_state
*pipe_config
,
192 const struct drm_connector_state
*conn_state
)
194 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
195 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
196 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
198 if (intel_dp
->active_mst_links
== 0 &&
199 intel_dig_port
->base
.pre_pll_enable
)
200 intel_dig_port
->base
.pre_pll_enable(&intel_dig_port
->base
,
204 static void intel_mst_pre_enable_dp(struct intel_encoder
*encoder
,
205 const struct intel_crtc_state
*pipe_config
,
206 const struct drm_connector_state
*conn_state
)
208 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
209 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
210 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
211 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
212 enum port port
= intel_dig_port
->base
.port
;
213 struct intel_connector
*connector
=
214 to_intel_connector(conn_state
->connector
);
218 /* MST encoders are bound to a crtc, not to a connector,
219 * force the mapping here for get_hw_state.
221 connector
->encoder
= encoder
;
222 intel_mst
->connector
= connector
;
224 DRM_DEBUG_KMS("active links %d\n", intel_dp
->active_mst_links
);
226 drm_dp_send_power_updown_phy(&intel_dp
->mst_mgr
, connector
->port
, true);
227 if (intel_dp
->active_mst_links
== 0)
228 intel_dig_port
->base
.pre_enable(&intel_dig_port
->base
,
231 ret
= drm_dp_mst_allocate_vcpi(&intel_dp
->mst_mgr
,
234 pipe_config
->dp_m_n
.tu
);
236 DRM_ERROR("failed to allocate vcpi\n");
241 intel_dp
->active_mst_links
++;
242 temp
= I915_READ(DP_TP_STATUS(port
));
243 I915_WRITE(DP_TP_STATUS(port
), temp
);
245 ret
= drm_dp_update_payload_part1(&intel_dp
->mst_mgr
);
248 static void intel_mst_enable_dp(struct intel_encoder
*encoder
,
249 const struct intel_crtc_state
*pipe_config
,
250 const struct drm_connector_state
*conn_state
)
252 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
253 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
254 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
255 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
256 enum port port
= intel_dig_port
->base
.port
;
259 DRM_DEBUG_KMS("active links %d\n", intel_dp
->active_mst_links
);
261 if (intel_wait_for_register(dev_priv
,
263 DP_TP_STATUS_ACT_SENT
,
264 DP_TP_STATUS_ACT_SENT
,
266 DRM_ERROR("Timed out waiting for ACT sent\n");
268 ret
= drm_dp_check_act_status(&intel_dp
->mst_mgr
);
270 ret
= drm_dp_update_payload_part2(&intel_dp
->mst_mgr
);
271 if (pipe_config
->has_audio
)
272 intel_audio_codec_enable(encoder
, pipe_config
, conn_state
);
275 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder
*encoder
,
278 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
279 *pipe
= intel_mst
->pipe
;
280 if (intel_mst
->connector
)
285 static void intel_dp_mst_enc_get_config(struct intel_encoder
*encoder
,
286 struct intel_crtc_state
*pipe_config
)
288 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
289 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
291 intel_ddi_get_config(&intel_dig_port
->base
, pipe_config
);
294 static int intel_dp_mst_get_ddc_modes(struct drm_connector
*connector
)
296 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
297 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
302 return intel_connector_update_modes(connector
, NULL
);
305 edid
= drm_dp_mst_get_edid(connector
, &intel_dp
->mst_mgr
, intel_connector
->port
);
306 ret
= intel_connector_update_modes(connector
, edid
);
312 static enum drm_connector_status
313 intel_dp_mst_detect(struct drm_connector
*connector
, bool force
)
315 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
316 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
319 return connector_status_disconnected
;
320 return drm_dp_mst_detect_port(connector
, &intel_dp
->mst_mgr
, intel_connector
->port
);
324 intel_dp_mst_connector_destroy(struct drm_connector
*connector
)
326 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
328 if (!IS_ERR_OR_NULL(intel_connector
->edid
))
329 kfree(intel_connector
->edid
);
331 drm_connector_cleanup(connector
);
335 static const struct drm_connector_funcs intel_dp_mst_connector_funcs
= {
336 .detect
= intel_dp_mst_detect
,
337 .fill_modes
= drm_helper_probe_single_connector_modes
,
338 .late_register
= intel_connector_register
,
339 .early_unregister
= intel_connector_unregister
,
340 .destroy
= intel_dp_mst_connector_destroy
,
341 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
342 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
345 static int intel_dp_mst_get_modes(struct drm_connector
*connector
)
347 return intel_dp_mst_get_ddc_modes(connector
);
350 static enum drm_mode_status
351 intel_dp_mst_mode_valid(struct drm_connector
*connector
,
352 struct drm_display_mode
*mode
)
354 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
355 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
356 int max_dotclk
= to_i915(connector
->dev
)->max_dotclk_freq
;
357 int bpp
= 24; /* MST uses fixed bpp */
358 int max_rate
, mode_rate
, max_lanes
, max_link_clock
;
363 max_link_clock
= intel_dp_max_link_rate(intel_dp
);
364 max_lanes
= intel_dp_max_lane_count(intel_dp
);
366 max_rate
= intel_dp_max_data_rate(max_link_clock
, max_lanes
);
367 mode_rate
= intel_dp_link_required(mode
->clock
, bpp
);
369 /* TODO - validate mode against available PBN for link */
370 if (mode
->clock
< 10000)
371 return MODE_CLOCK_LOW
;
373 if (mode
->flags
& DRM_MODE_FLAG_DBLCLK
)
374 return MODE_H_ILLEGAL
;
376 if (mode_rate
> max_rate
|| mode
->clock
> max_dotclk
)
377 return MODE_CLOCK_HIGH
;
382 static struct drm_encoder
*intel_mst_atomic_best_encoder(struct drm_connector
*connector
,
383 struct drm_connector_state
*state
)
385 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
386 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
387 struct intel_crtc
*crtc
= to_intel_crtc(state
->crtc
);
391 return &intel_dp
->mst_encoders
[crtc
->pipe
]->base
.base
;
394 static struct drm_encoder
*intel_mst_best_encoder(struct drm_connector
*connector
)
396 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
397 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
400 return &intel_dp
->mst_encoders
[0]->base
.base
;
403 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs
= {
404 .get_modes
= intel_dp_mst_get_modes
,
405 .mode_valid
= intel_dp_mst_mode_valid
,
406 .atomic_best_encoder
= intel_mst_atomic_best_encoder
,
407 .best_encoder
= intel_mst_best_encoder
,
408 .atomic_check
= intel_dp_mst_atomic_check
,
411 static void intel_dp_mst_encoder_destroy(struct drm_encoder
*encoder
)
413 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(encoder
);
415 drm_encoder_cleanup(encoder
);
419 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs
= {
420 .destroy
= intel_dp_mst_encoder_destroy
,
423 static bool intel_dp_mst_get_hw_state(struct intel_connector
*connector
)
425 if (connector
->encoder
&& connector
->base
.state
->crtc
) {
427 if (!connector
->encoder
->get_hw_state(connector
->encoder
, &pipe
))
434 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
)
436 struct intel_dp
*intel_dp
= container_of(mgr
, struct intel_dp
, mst_mgr
);
437 struct intel_digital_port
*intel_dig_port
= dp_to_dig_port(intel_dp
);
438 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
439 struct drm_i915_private
*dev_priv
= to_i915(dev
);
440 struct intel_connector
*intel_connector
;
441 struct drm_connector
*connector
;
445 intel_connector
= intel_connector_alloc();
446 if (!intel_connector
)
449 connector
= &intel_connector
->base
;
450 ret
= drm_connector_init(dev
, connector
, &intel_dp_mst_connector_funcs
,
451 DRM_MODE_CONNECTOR_DisplayPort
);
453 intel_connector_free(intel_connector
);
457 drm_connector_helper_add(connector
, &intel_dp_mst_connector_helper_funcs
);
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 for_each_pipe(dev_priv
, pipe
) {
464 struct drm_encoder
*enc
=
465 &intel_dp
->mst_encoders
[pipe
]->base
.base
;
467 ret
= drm_mode_connector_attach_encoder(&intel_connector
->base
,
473 drm_object_attach_property(&connector
->base
, dev
->mode_config
.path_property
, 0);
474 drm_object_attach_property(&connector
->base
, dev
->mode_config
.tile_property
, 0);
476 ret
= drm_mode_connector_set_path_property(connector
, pathprop
);
483 drm_connector_cleanup(connector
);
487 static void intel_dp_register_mst_connector(struct drm_connector
*connector
)
489 struct drm_i915_private
*dev_priv
= to_i915(connector
->dev
);
492 drm_fb_helper_add_one_connector(&dev_priv
->fbdev
->helper
,
495 drm_connector_register(connector
);
498 static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr
*mgr
,
499 struct drm_connector
*connector
)
501 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
502 struct drm_i915_private
*dev_priv
= to_i915(connector
->dev
);
504 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector
->base
.id
, connector
->name
);
505 drm_connector_unregister(connector
);
508 drm_fb_helper_remove_one_connector(&dev_priv
->fbdev
->helper
,
510 /* prevent race with the check in ->detect */
511 drm_modeset_lock(&connector
->dev
->mode_config
.connection_mutex
, NULL
);
512 intel_connector
->mst_port
= NULL
;
513 drm_modeset_unlock(&connector
->dev
->mode_config
.connection_mutex
);
515 drm_connector_unreference(connector
);
518 static void intel_dp_mst_hotplug(struct drm_dp_mst_topology_mgr
*mgr
)
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
;
524 drm_kms_helper_hotplug_event(dev
);
527 static const struct drm_dp_mst_topology_cbs mst_cbs
= {
528 .add_connector
= intel_dp_add_mst_connector
,
529 .register_connector
= intel_dp_register_mst_connector
,
530 .destroy_connector
= intel_dp_destroy_mst_connector
,
531 .hotplug
= intel_dp_mst_hotplug
,
534 static struct intel_dp_mst_encoder
*
535 intel_dp_create_fake_mst_encoder(struct intel_digital_port
*intel_dig_port
, enum pipe pipe
)
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
;
541 intel_mst
= kzalloc(sizeof(*intel_mst
), GFP_KERNEL
);
546 intel_mst
->pipe
= pipe
;
547 intel_encoder
= &intel_mst
->base
;
548 intel_mst
->primary
= intel_dig_port
;
550 drm_encoder_init(dev
, &intel_encoder
->base
, &intel_dp_mst_enc_funcs
,
551 DRM_MODE_ENCODER_DPMST
, "DP-MST %c", pipe_name(pipe
));
553 intel_encoder
->type
= INTEL_OUTPUT_DP_MST
;
554 intel_encoder
->power_domain
= intel_dig_port
->base
.power_domain
;
555 intel_encoder
->port
= intel_dig_port
->base
.port
;
556 intel_encoder
->crtc_mask
= 0x7;
557 intel_encoder
->cloneable
= 0;
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_pll_enable
= intel_mst_pre_pll_enable_dp
;
563 intel_encoder
->pre_enable
= intel_mst_pre_enable_dp
;
564 intel_encoder
->enable
= intel_mst_enable_dp
;
565 intel_encoder
->get_hw_state
= intel_dp_mst_enc_get_hw_state
;
566 intel_encoder
->get_config
= intel_dp_mst_enc_get_config
;
573 intel_dp_create_fake_mst_encoders(struct intel_digital_port
*intel_dig_port
)
575 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
576 struct drm_i915_private
*dev_priv
= to_i915(intel_dig_port
->base
.base
.dev
);
579 for_each_pipe(dev_priv
, pipe
)
580 intel_dp
->mst_encoders
[pipe
] = intel_dp_create_fake_mst_encoder(intel_dig_port
, pipe
);
585 intel_dp_mst_encoder_init(struct intel_digital_port
*intel_dig_port
, int conn_base_id
)
587 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
588 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
591 intel_dp
->can_mst
= true;
592 intel_dp
->mst_mgr
.cbs
= &mst_cbs
;
594 /* create encoders */
595 intel_dp_create_fake_mst_encoders(intel_dig_port
);
596 ret
= drm_dp_mst_topology_mgr_init(&intel_dp
->mst_mgr
, dev
,
597 &intel_dp
->aux
, 16, 3, conn_base_id
);
599 intel_dp
->can_mst
= false;
606 intel_dp_mst_encoder_cleanup(struct intel_digital_port
*intel_dig_port
)
608 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
610 if (!intel_dp
->can_mst
)
613 drm_dp_mst_topology_mgr_destroy(&intel_dp
->mst_mgr
);
614 /* encoders will get killed by normal cleanup */