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
)
36 struct drm_device
*dev
= encoder
->base
.dev
;
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
;
40 struct drm_atomic_state
*state
;
42 int lane_count
, slots
;
43 const struct drm_display_mode
*adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
44 struct drm_connector
*drm_connector
;
45 struct intel_connector
*connector
, *found
= NULL
;
46 struct drm_connector_state
*connector_state
;
49 pipe_config
->dp_encoder_is_mst
= true;
50 pipe_config
->has_pch_encoder
= false;
51 pipe_config
->has_dp_encoder
= true;
54 * for MST we always configure max link bw - the spec doesn't
55 * seem to suggest we should do otherwise.
57 lane_count
= drm_dp_max_lane_count(intel_dp
->dpcd
);
60 pipe_config
->lane_count
= lane_count
;
62 pipe_config
->pipe_bpp
= 24;
63 pipe_config
->port_clock
= intel_dp_max_link_rate(intel_dp
);
65 state
= pipe_config
->base
.state
;
67 for_each_connector_in_state(state
, drm_connector
, connector_state
, i
) {
68 connector
= to_intel_connector(drm_connector
);
70 if (connector_state
->best_encoder
== &encoder
->base
) {
77 DRM_ERROR("can't find connector\n");
81 if (drm_dp_mst_port_has_audio(&intel_dp
->mst_mgr
, found
->port
))
82 pipe_config
->has_audio
= true;
83 mst_pbn
= drm_dp_calc_pbn_mode(adjusted_mode
->crtc_clock
, bpp
);
85 pipe_config
->pbn
= mst_pbn
;
86 slots
= drm_dp_find_vcpi_slots(&intel_dp
->mst_mgr
, mst_pbn
);
88 intel_link_compute_m_n(bpp
, lane_count
,
89 adjusted_mode
->crtc_clock
,
90 pipe_config
->port_clock
,
91 &pipe_config
->dp_m_n
);
93 pipe_config
->dp_m_n
.tu
= slots
;
95 if (IS_HASWELL(dev
) || IS_BROADWELL(dev
))
96 hsw_dp_set_ddi_pll_sel(pipe_config
);
102 static void intel_mst_disable_dp(struct intel_encoder
*encoder
)
104 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
105 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
106 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
107 struct drm_device
*dev
= encoder
->base
.dev
;
108 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
109 struct drm_crtc
*crtc
= encoder
->base
.crtc
;
110 struct intel_crtc
*intel_crtc
= to_intel_crtc(crtc
);
114 DRM_DEBUG_KMS("%d\n", intel_dp
->active_mst_links
);
116 drm_dp_mst_reset_vcpi_slots(&intel_dp
->mst_mgr
, intel_mst
->port
);
118 ret
= drm_dp_update_payload_part1(&intel_dp
->mst_mgr
);
120 DRM_ERROR("failed to update payload %d\n", ret
);
122 if (intel_crtc
->config
->has_audio
) {
123 intel_audio_codec_disable(encoder
);
124 intel_display_power_put(dev_priv
, POWER_DOMAIN_AUDIO
);
128 static void intel_mst_post_disable_dp(struct intel_encoder
*encoder
)
130 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
131 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
132 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
134 DRM_DEBUG_KMS("%d\n", intel_dp
->active_mst_links
);
137 drm_dp_check_act_status(&intel_dp
->mst_mgr
);
138 /* and this can also fail */
139 drm_dp_update_payload_part2(&intel_dp
->mst_mgr
);
141 drm_dp_mst_deallocate_vcpi(&intel_dp
->mst_mgr
, intel_mst
->port
);
143 intel_dp
->active_mst_links
--;
144 intel_mst
->port
= NULL
;
145 if (intel_dp
->active_mst_links
== 0) {
146 intel_dig_port
->base
.post_disable(&intel_dig_port
->base
);
147 intel_dp_sink_dpms(intel_dp
, DRM_MODE_DPMS_OFF
);
151 static void intel_mst_pre_enable_dp(struct intel_encoder
*encoder
)
153 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
154 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
155 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
156 struct drm_device
*dev
= encoder
->base
.dev
;
157 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
158 enum port port
= intel_dig_port
->port
;
161 struct intel_connector
*found
= NULL
, *connector
;
163 struct drm_crtc
*crtc
= encoder
->base
.crtc
;
164 struct intel_crtc
*intel_crtc
= to_intel_crtc(crtc
);
166 for_each_intel_connector(dev
, connector
) {
167 if (connector
->base
.state
->best_encoder
== &encoder
->base
) {
174 DRM_ERROR("can't find connector\n");
178 /* MST encoders are bound to a crtc, not to a connector,
179 * force the mapping here for get_hw_state.
181 found
->encoder
= encoder
;
183 DRM_DEBUG_KMS("%d\n", intel_dp
->active_mst_links
);
184 intel_mst
->port
= found
->port
;
186 if (intel_dp
->active_mst_links
== 0) {
187 intel_ddi_clk_select(encoder
, intel_crtc
->config
);
189 intel_dp_set_link_params(intel_dp
, intel_crtc
->config
);
191 intel_ddi_init_dp_buf_reg(&intel_dig_port
->base
);
193 intel_dp_sink_dpms(intel_dp
, DRM_MODE_DPMS_ON
);
195 intel_dp_start_link_train(intel_dp
);
196 intel_dp_stop_link_train(intel_dp
);
199 ret
= drm_dp_mst_allocate_vcpi(&intel_dp
->mst_mgr
,
201 intel_crtc
->config
->pbn
, &slots
);
203 DRM_ERROR("failed to allocate vcpi\n");
208 intel_dp
->active_mst_links
++;
209 temp
= I915_READ(DP_TP_STATUS(port
));
210 I915_WRITE(DP_TP_STATUS(port
), temp
);
212 ret
= drm_dp_update_payload_part1(&intel_dp
->mst_mgr
);
215 static void intel_mst_enable_dp(struct intel_encoder
*encoder
)
217 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
218 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
219 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
220 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
221 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
222 struct intel_crtc
*crtc
= to_intel_crtc(encoder
->base
.crtc
);
223 enum port port
= intel_dig_port
->port
;
226 DRM_DEBUG_KMS("%d\n", intel_dp
->active_mst_links
);
228 if (wait_for((I915_READ(DP_TP_STATUS(port
)) & DP_TP_STATUS_ACT_SENT
),
230 DRM_ERROR("Timed out waiting for ACT sent\n");
232 ret
= drm_dp_check_act_status(&intel_dp
->mst_mgr
);
234 ret
= drm_dp_update_payload_part2(&intel_dp
->mst_mgr
);
236 if (crtc
->config
->has_audio
) {
237 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
238 pipe_name(crtc
->pipe
));
239 intel_display_power_get(dev_priv
, POWER_DOMAIN_AUDIO
);
240 intel_audio_codec_enable(encoder
);
244 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder
*encoder
,
247 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
248 *pipe
= intel_mst
->pipe
;
254 static void intel_dp_mst_enc_get_config(struct intel_encoder
*encoder
,
255 struct intel_crtc_state
*pipe_config
)
257 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
258 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
259 struct intel_crtc
*crtc
= to_intel_crtc(encoder
->base
.crtc
);
260 struct drm_device
*dev
= encoder
->base
.dev
;
261 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
262 enum transcoder cpu_transcoder
= pipe_config
->cpu_transcoder
;
265 pipe_config
->has_dp_encoder
= true;
267 pipe_config
->has_audio
=
268 intel_ddi_is_audio_enabled(dev_priv
, crtc
);
270 temp
= I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder
));
271 if (temp
& TRANS_DDI_PHSYNC
)
272 flags
|= DRM_MODE_FLAG_PHSYNC
;
274 flags
|= DRM_MODE_FLAG_NHSYNC
;
275 if (temp
& TRANS_DDI_PVSYNC
)
276 flags
|= DRM_MODE_FLAG_PVSYNC
;
278 flags
|= DRM_MODE_FLAG_NVSYNC
;
280 switch (temp
& TRANS_DDI_BPC_MASK
) {
281 case TRANS_DDI_BPC_6
:
282 pipe_config
->pipe_bpp
= 18;
284 case TRANS_DDI_BPC_8
:
285 pipe_config
->pipe_bpp
= 24;
287 case TRANS_DDI_BPC_10
:
288 pipe_config
->pipe_bpp
= 30;
290 case TRANS_DDI_BPC_12
:
291 pipe_config
->pipe_bpp
= 36;
296 pipe_config
->base
.adjusted_mode
.flags
|= flags
;
298 pipe_config
->lane_count
=
299 ((temp
& DDI_PORT_WIDTH_MASK
) >> DDI_PORT_WIDTH_SHIFT
) + 1;
301 intel_dp_get_m_n(crtc
, pipe_config
);
303 intel_ddi_clock_get(&intel_dig_port
->base
, pipe_config
);
306 static int intel_dp_mst_get_ddc_modes(struct drm_connector
*connector
)
308 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
309 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
313 edid
= drm_dp_mst_get_edid(connector
, &intel_dp
->mst_mgr
, intel_connector
->port
);
317 ret
= intel_connector_update_modes(connector
, edid
);
323 static enum drm_connector_status
324 intel_dp_mst_detect(struct drm_connector
*connector
, bool force
)
326 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
327 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
329 return drm_dp_mst_detect_port(connector
, &intel_dp
->mst_mgr
, intel_connector
->port
);
333 intel_dp_mst_set_property(struct drm_connector
*connector
,
334 struct drm_property
*property
,
341 intel_dp_mst_connector_destroy(struct drm_connector
*connector
)
343 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
345 if (!IS_ERR_OR_NULL(intel_connector
->edid
))
346 kfree(intel_connector
->edid
);
348 drm_connector_cleanup(connector
);
352 static const struct drm_connector_funcs intel_dp_mst_connector_funcs
= {
353 .dpms
= drm_atomic_helper_connector_dpms
,
354 .detect
= intel_dp_mst_detect
,
355 .fill_modes
= drm_helper_probe_single_connector_modes
,
356 .set_property
= intel_dp_mst_set_property
,
357 .atomic_get_property
= intel_connector_atomic_get_property
,
358 .destroy
= intel_dp_mst_connector_destroy
,
359 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
360 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
363 static int intel_dp_mst_get_modes(struct drm_connector
*connector
)
365 return intel_dp_mst_get_ddc_modes(connector
);
368 static enum drm_mode_status
369 intel_dp_mst_mode_valid(struct drm_connector
*connector
,
370 struct drm_display_mode
*mode
)
372 /* TODO - validate mode against available PBN for link */
373 if (mode
->clock
< 10000)
374 return MODE_CLOCK_LOW
;
376 if (mode
->flags
& DRM_MODE_FLAG_DBLCLK
)
377 return MODE_H_ILLEGAL
;
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
);
389 return &intel_dp
->mst_encoders
[crtc
->pipe
]->base
.base
;
392 static struct drm_encoder
*intel_mst_best_encoder(struct drm_connector
*connector
)
394 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
395 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
396 return &intel_dp
->mst_encoders
[0]->base
.base
;
399 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs
= {
400 .get_modes
= intel_dp_mst_get_modes
,
401 .mode_valid
= intel_dp_mst_mode_valid
,
402 .atomic_best_encoder
= intel_mst_atomic_best_encoder
,
403 .best_encoder
= intel_mst_best_encoder
,
406 static void intel_dp_mst_encoder_destroy(struct drm_encoder
*encoder
)
408 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(encoder
);
410 drm_encoder_cleanup(encoder
);
414 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs
= {
415 .destroy
= intel_dp_mst_encoder_destroy
,
418 static bool intel_dp_mst_get_hw_state(struct intel_connector
*connector
)
420 if (connector
->encoder
&& connector
->base
.state
->crtc
) {
422 if (!connector
->encoder
->get_hw_state(connector
->encoder
, &pipe
))
429 static void intel_connector_add_to_fbdev(struct intel_connector
*connector
)
431 #ifdef CONFIG_DRM_FBDEV_EMULATION
432 struct drm_i915_private
*dev_priv
= to_i915(connector
->base
.dev
);
435 drm_fb_helper_add_one_connector(&dev_priv
->fbdev
->helper
,
440 static void intel_connector_remove_from_fbdev(struct intel_connector
*connector
)
442 #ifdef CONFIG_DRM_FBDEV_EMULATION
443 struct drm_i915_private
*dev_priv
= to_i915(connector
->base
.dev
);
446 drm_fb_helper_remove_one_connector(&dev_priv
->fbdev
->helper
,
451 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
)
453 struct intel_dp
*intel_dp
= container_of(mgr
, struct intel_dp
, mst_mgr
);
454 struct intel_digital_port
*intel_dig_port
= dp_to_dig_port(intel_dp
);
455 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
456 struct intel_connector
*intel_connector
;
457 struct drm_connector
*connector
;
460 intel_connector
= intel_connector_alloc();
461 if (!intel_connector
)
464 connector
= &intel_connector
->base
;
465 drm_connector_init(dev
, connector
, &intel_dp_mst_connector_funcs
, DRM_MODE_CONNECTOR_DisplayPort
);
466 drm_connector_helper_add(connector
, &intel_dp_mst_connector_helper_funcs
);
468 intel_connector
->unregister
= intel_connector_unregister
;
469 intel_connector
->get_hw_state
= intel_dp_mst_get_hw_state
;
470 intel_connector
->mst_port
= intel_dp
;
471 intel_connector
->port
= port
;
473 for (i
= PIPE_A
; i
<= PIPE_C
; i
++) {
474 drm_mode_connector_attach_encoder(&intel_connector
->base
,
475 &intel_dp
->mst_encoders
[i
]->base
.base
);
477 intel_dp_add_properties(intel_dp
, connector
);
479 drm_object_attach_property(&connector
->base
, dev
->mode_config
.path_property
, 0);
480 drm_object_attach_property(&connector
->base
, dev
->mode_config
.tile_property
, 0);
482 drm_mode_connector_set_path_property(connector
, pathprop
);
486 static void intel_dp_register_mst_connector(struct drm_connector
*connector
)
488 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
489 struct drm_device
*dev
= connector
->dev
;
490 drm_modeset_lock_all(dev
);
491 intel_connector_add_to_fbdev(intel_connector
);
492 drm_modeset_unlock_all(dev
);
493 drm_connector_register(&intel_connector
->base
);
496 static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr
*mgr
,
497 struct drm_connector
*connector
)
499 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
500 struct drm_device
*dev
= connector
->dev
;
502 /* need to nuke the connector */
503 drm_modeset_lock_all(dev
);
504 if (connector
->state
->crtc
) {
505 struct drm_mode_set set
;
508 memset(&set
, 0, sizeof(set
));
509 set
.crtc
= connector
->state
->crtc
,
511 ret
= drm_atomic_helper_set_config(&set
);
513 WARN(ret
, "Disabling mst crtc failed with %i\n", ret
);
515 drm_modeset_unlock_all(dev
);
517 intel_connector
->unregister(intel_connector
);
519 drm_modeset_lock_all(dev
);
520 intel_connector_remove_from_fbdev(intel_connector
);
521 drm_connector_cleanup(connector
);
522 drm_modeset_unlock_all(dev
);
524 kfree(intel_connector
);
528 static void intel_dp_mst_hotplug(struct drm_dp_mst_topology_mgr
*mgr
)
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
;
534 drm_kms_helper_hotplug_event(dev
);
537 static struct drm_dp_mst_topology_cbs mst_cbs
= {
538 .add_connector
= intel_dp_add_mst_connector
,
539 .register_connector
= intel_dp_register_mst_connector
,
540 .destroy_connector
= intel_dp_destroy_mst_connector
,
541 .hotplug
= intel_dp_mst_hotplug
,
544 static struct intel_dp_mst_encoder
*
545 intel_dp_create_fake_mst_encoder(struct intel_digital_port
*intel_dig_port
, enum pipe pipe
)
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
;
551 intel_mst
= kzalloc(sizeof(*intel_mst
), GFP_KERNEL
);
556 intel_mst
->pipe
= pipe
;
557 intel_encoder
= &intel_mst
->base
;
558 intel_mst
->primary
= intel_dig_port
;
560 drm_encoder_init(dev
, &intel_encoder
->base
, &intel_dp_mst_enc_funcs
,
561 DRM_MODE_ENCODER_DPMST
, NULL
);
563 intel_encoder
->type
= INTEL_OUTPUT_DP_MST
;
564 intel_encoder
->crtc_mask
= 0x7;
565 intel_encoder
->cloneable
= 0;
567 intel_encoder
->compute_config
= intel_dp_mst_compute_config
;
568 intel_encoder
->disable
= intel_mst_disable_dp
;
569 intel_encoder
->post_disable
= intel_mst_post_disable_dp
;
570 intel_encoder
->pre_enable
= intel_mst_pre_enable_dp
;
571 intel_encoder
->enable
= intel_mst_enable_dp
;
572 intel_encoder
->get_hw_state
= intel_dp_mst_enc_get_hw_state
;
573 intel_encoder
->get_config
= intel_dp_mst_enc_get_config
;
580 intel_dp_create_fake_mst_encoders(struct intel_digital_port
*intel_dig_port
)
583 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
585 for (i
= PIPE_A
; i
<= PIPE_C
; i
++)
586 intel_dp
->mst_encoders
[i
] = intel_dp_create_fake_mst_encoder(intel_dig_port
, i
);
591 intel_dp_mst_encoder_init(struct intel_digital_port
*intel_dig_port
, int conn_base_id
)
593 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
594 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
597 intel_dp
->can_mst
= true;
598 intel_dp
->mst_mgr
.cbs
= &mst_cbs
;
600 /* create encoders */
601 intel_dp_create_fake_mst_encoders(intel_dig_port
);
602 ret
= drm_dp_mst_topology_mgr_init(&intel_dp
->mst_mgr
, dev
->dev
, &intel_dp
->aux
, 16, 3, conn_base_id
);
604 intel_dp
->can_mst
= false;
611 intel_dp_mst_encoder_cleanup(struct intel_digital_port
*intel_dig_port
)
613 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
615 if (!intel_dp
->can_mst
)
618 drm_dp_mst_topology_mgr_destroy(&intel_dp
->mst_mgr
);
619 /* encoders will get killed by normal cleanup */