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 intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
37 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
38 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
39 struct drm_atomic_state
*state
;
41 int lane_count
, slots
, rate
;
42 struct drm_display_mode
*adjusted_mode
= &pipe_config
->base
.adjusted_mode
;
43 struct drm_connector
*drm_connector
;
44 struct intel_connector
*connector
, *found
= NULL
;
45 struct drm_connector_state
*connector_state
;
48 pipe_config
->dp_encoder_is_mst
= true;
49 pipe_config
->has_pch_encoder
= false;
50 pipe_config
->has_dp_encoder
= true;
53 * for MST we always configure max link bw - the spec doesn't
54 * seem to suggest we should do otherwise.
56 lane_count
= drm_dp_max_lane_count(intel_dp
->dpcd
);
58 rate
= intel_dp_max_link_rate(intel_dp
);
60 if (intel_dp
->num_sink_rates
) {
61 intel_dp
->link_bw
= 0;
62 intel_dp
->rate_select
= intel_dp_rate_select(intel_dp
, rate
);
64 intel_dp
->link_bw
= drm_dp_link_rate_to_bw_code(rate
);
65 intel_dp
->rate_select
= 0;
68 intel_dp
->lane_count
= lane_count
;
70 pipe_config
->pipe_bpp
= 24;
71 pipe_config
->port_clock
= rate
;
73 state
= pipe_config
->base
.state
;
75 for_each_connector_in_state(state
, drm_connector
, connector_state
, i
) {
76 connector
= to_intel_connector(drm_connector
);
78 if (connector_state
->best_encoder
== &encoder
->base
) {
85 DRM_ERROR("can't find connector\n");
89 mst_pbn
= drm_dp_calc_pbn_mode(adjusted_mode
->clock
, bpp
);
91 pipe_config
->pbn
= mst_pbn
;
92 slots
= drm_dp_find_vcpi_slots(&intel_dp
->mst_mgr
, mst_pbn
);
94 intel_link_compute_m_n(bpp
, lane_count
,
95 adjusted_mode
->crtc_clock
,
96 pipe_config
->port_clock
,
97 &pipe_config
->dp_m_n
);
99 pipe_config
->dp_m_n
.tu
= slots
;
104 static void intel_mst_disable_dp(struct intel_encoder
*encoder
)
106 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
107 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
108 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
111 DRM_DEBUG_KMS("%d\n", intel_dp
->active_mst_links
);
113 drm_dp_mst_reset_vcpi_slots(&intel_dp
->mst_mgr
, intel_mst
->port
);
115 ret
= drm_dp_update_payload_part1(&intel_dp
->mst_mgr
);
117 DRM_ERROR("failed to update payload %d\n", ret
);
121 static void intel_mst_post_disable_dp(struct intel_encoder
*encoder
)
123 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
124 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
125 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
127 DRM_DEBUG_KMS("%d\n", intel_dp
->active_mst_links
);
130 drm_dp_check_act_status(&intel_dp
->mst_mgr
);
131 /* and this can also fail */
132 drm_dp_update_payload_part2(&intel_dp
->mst_mgr
);
134 drm_dp_mst_deallocate_vcpi(&intel_dp
->mst_mgr
, intel_mst
->port
);
136 intel_dp
->active_mst_links
--;
137 intel_mst
->port
= NULL
;
138 if (intel_dp
->active_mst_links
== 0) {
139 intel_dig_port
->base
.post_disable(&intel_dig_port
->base
);
140 intel_dp_sink_dpms(intel_dp
, DRM_MODE_DPMS_OFF
);
144 static void intel_mst_pre_enable_dp(struct intel_encoder
*encoder
)
146 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
147 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
148 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
149 struct drm_device
*dev
= encoder
->base
.dev
;
150 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
151 enum port port
= intel_dig_port
->port
;
154 struct intel_connector
*found
= NULL
, *connector
;
156 struct drm_crtc
*crtc
= encoder
->base
.crtc
;
157 struct intel_crtc
*intel_crtc
= to_intel_crtc(crtc
);
159 for_each_intel_connector(dev
, connector
) {
160 if (connector
->base
.state
->best_encoder
== &encoder
->base
) {
167 DRM_ERROR("can't find connector\n");
171 DRM_DEBUG_KMS("%d\n", intel_dp
->active_mst_links
);
172 intel_mst
->port
= found
->port
;
174 if (intel_dp
->active_mst_links
== 0) {
175 enum port port
= intel_ddi_get_encoder_port(encoder
);
177 /* FIXME: add support for SKL */
178 if (INTEL_INFO(dev
)->gen
< 9)
179 I915_WRITE(PORT_CLK_SEL(port
),
180 intel_crtc
->config
->ddi_pll_sel
);
182 intel_ddi_init_dp_buf_reg(&intel_dig_port
->base
);
184 intel_dp_sink_dpms(intel_dp
, DRM_MODE_DPMS_ON
);
187 intel_dp_start_link_train(intel_dp
);
188 intel_dp_complete_link_train(intel_dp
);
189 intel_dp_stop_link_train(intel_dp
);
192 ret
= drm_dp_mst_allocate_vcpi(&intel_dp
->mst_mgr
,
194 intel_crtc
->config
->pbn
, &slots
);
196 DRM_ERROR("failed to allocate vcpi\n");
201 intel_dp
->active_mst_links
++;
202 temp
= I915_READ(DP_TP_STATUS(port
));
203 I915_WRITE(DP_TP_STATUS(port
), temp
);
205 ret
= drm_dp_update_payload_part1(&intel_dp
->mst_mgr
);
208 static void intel_mst_enable_dp(struct intel_encoder
*encoder
)
210 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
211 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
212 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
213 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
214 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
215 enum port port
= intel_dig_port
->port
;
218 DRM_DEBUG_KMS("%d\n", intel_dp
->active_mst_links
);
220 if (wait_for((I915_READ(DP_TP_STATUS(port
)) & DP_TP_STATUS_ACT_SENT
),
222 DRM_ERROR("Timed out waiting for ACT sent\n");
224 ret
= drm_dp_check_act_status(&intel_dp
->mst_mgr
);
226 ret
= drm_dp_update_payload_part2(&intel_dp
->mst_mgr
);
229 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder
*encoder
,
232 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
233 *pipe
= intel_mst
->pipe
;
239 static void intel_dp_mst_enc_get_config(struct intel_encoder
*encoder
,
240 struct intel_crtc_state
*pipe_config
)
242 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(&encoder
->base
);
243 struct intel_digital_port
*intel_dig_port
= intel_mst
->primary
;
244 struct intel_crtc
*crtc
= to_intel_crtc(encoder
->base
.crtc
);
245 struct drm_device
*dev
= encoder
->base
.dev
;
246 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
247 enum transcoder cpu_transcoder
= pipe_config
->cpu_transcoder
;
250 pipe_config
->has_dp_encoder
= true;
252 temp
= I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder
));
253 if (temp
& TRANS_DDI_PHSYNC
)
254 flags
|= DRM_MODE_FLAG_PHSYNC
;
256 flags
|= DRM_MODE_FLAG_NHSYNC
;
257 if (temp
& TRANS_DDI_PVSYNC
)
258 flags
|= DRM_MODE_FLAG_PVSYNC
;
260 flags
|= DRM_MODE_FLAG_NVSYNC
;
262 switch (temp
& TRANS_DDI_BPC_MASK
) {
263 case TRANS_DDI_BPC_6
:
264 pipe_config
->pipe_bpp
= 18;
266 case TRANS_DDI_BPC_8
:
267 pipe_config
->pipe_bpp
= 24;
269 case TRANS_DDI_BPC_10
:
270 pipe_config
->pipe_bpp
= 30;
272 case TRANS_DDI_BPC_12
:
273 pipe_config
->pipe_bpp
= 36;
278 pipe_config
->base
.adjusted_mode
.flags
|= flags
;
279 intel_dp_get_m_n(crtc
, pipe_config
);
281 intel_ddi_clock_get(&intel_dig_port
->base
, pipe_config
);
284 static int intel_dp_mst_get_ddc_modes(struct drm_connector
*connector
)
286 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
287 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
291 edid
= drm_dp_mst_get_edid(connector
, &intel_dp
->mst_mgr
, intel_connector
->port
);
295 ret
= intel_connector_update_modes(connector
, edid
);
301 static enum drm_connector_status
302 intel_dp_mst_detect(struct drm_connector
*connector
, bool force
)
304 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
305 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
307 return drm_dp_mst_detect_port(connector
, &intel_dp
->mst_mgr
, intel_connector
->port
);
311 intel_dp_mst_set_property(struct drm_connector
*connector
,
312 struct drm_property
*property
,
319 intel_dp_mst_connector_destroy(struct drm_connector
*connector
)
321 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
323 if (!IS_ERR_OR_NULL(intel_connector
->edid
))
324 kfree(intel_connector
->edid
);
326 drm_connector_cleanup(connector
);
330 static const struct drm_connector_funcs intel_dp_mst_connector_funcs
= {
331 .dpms
= intel_connector_dpms
,
332 .detect
= intel_dp_mst_detect
,
333 .fill_modes
= drm_helper_probe_single_connector_modes
,
334 .set_property
= intel_dp_mst_set_property
,
335 .atomic_get_property
= intel_connector_atomic_get_property
,
336 .destroy
= intel_dp_mst_connector_destroy
,
337 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
338 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
341 static int intel_dp_mst_get_modes(struct drm_connector
*connector
)
343 return intel_dp_mst_get_ddc_modes(connector
);
346 static enum drm_mode_status
347 intel_dp_mst_mode_valid(struct drm_connector
*connector
,
348 struct drm_display_mode
*mode
)
350 /* TODO - validate mode against available PBN for link */
351 if (mode
->clock
< 10000)
352 return MODE_CLOCK_LOW
;
354 if (mode
->flags
& DRM_MODE_FLAG_DBLCLK
)
355 return MODE_H_ILLEGAL
;
360 static struct drm_encoder
*intel_mst_atomic_best_encoder(struct drm_connector
*connector
,
361 struct drm_connector_state
*state
)
363 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
364 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
365 struct intel_crtc
*crtc
= to_intel_crtc(state
->crtc
);
367 return &intel_dp
->mst_encoders
[crtc
->pipe
]->base
.base
;
370 static struct drm_encoder
*intel_mst_best_encoder(struct drm_connector
*connector
)
372 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
373 struct intel_dp
*intel_dp
= intel_connector
->mst_port
;
374 return &intel_dp
->mst_encoders
[0]->base
.base
;
377 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs
= {
378 .get_modes
= intel_dp_mst_get_modes
,
379 .mode_valid
= intel_dp_mst_mode_valid
,
380 .atomic_best_encoder
= intel_mst_atomic_best_encoder
,
381 .best_encoder
= intel_mst_best_encoder
,
384 static void intel_dp_mst_encoder_destroy(struct drm_encoder
*encoder
)
386 struct intel_dp_mst_encoder
*intel_mst
= enc_to_mst(encoder
);
388 drm_encoder_cleanup(encoder
);
392 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs
= {
393 .destroy
= intel_dp_mst_encoder_destroy
,
396 static bool intel_dp_mst_get_hw_state(struct intel_connector
*connector
)
398 if (connector
->encoder
) {
400 if (!connector
->encoder
->get_hw_state(connector
->encoder
, &pipe
))
407 static void intel_connector_add_to_fbdev(struct intel_connector
*connector
)
409 #ifdef CONFIG_DRM_I915_FBDEV
410 struct drm_i915_private
*dev_priv
= to_i915(connector
->base
.dev
);
411 drm_fb_helper_add_one_connector(&dev_priv
->fbdev
->helper
, &connector
->base
);
415 static void intel_connector_remove_from_fbdev(struct intel_connector
*connector
)
417 #ifdef CONFIG_DRM_I915_FBDEV
418 struct drm_i915_private
*dev_priv
= to_i915(connector
->base
.dev
);
419 drm_fb_helper_remove_one_connector(&dev_priv
->fbdev
->helper
, &connector
->base
);
423 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
)
425 struct intel_dp
*intel_dp
= container_of(mgr
, struct intel_dp
, mst_mgr
);
426 struct intel_digital_port
*intel_dig_port
= dp_to_dig_port(intel_dp
);
427 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
428 struct intel_connector
*intel_connector
;
429 struct drm_connector
*connector
;
432 intel_connector
= intel_connector_alloc();
433 if (!intel_connector
)
436 connector
= &intel_connector
->base
;
437 drm_connector_init(dev
, connector
, &intel_dp_mst_connector_funcs
, DRM_MODE_CONNECTOR_DisplayPort
);
438 drm_connector_helper_add(connector
, &intel_dp_mst_connector_helper_funcs
);
440 intel_connector
->unregister
= intel_connector_unregister
;
441 intel_connector
->get_hw_state
= intel_dp_mst_get_hw_state
;
442 intel_connector
->mst_port
= intel_dp
;
443 intel_connector
->port
= port
;
445 for (i
= PIPE_A
; i
<= PIPE_C
; i
++) {
446 drm_mode_connector_attach_encoder(&intel_connector
->base
,
447 &intel_dp
->mst_encoders
[i
]->base
.base
);
449 intel_dp_add_properties(intel_dp
, connector
);
451 drm_object_attach_property(&connector
->base
, dev
->mode_config
.path_property
, 0);
452 drm_object_attach_property(&connector
->base
, dev
->mode_config
.tile_property
, 0);
454 drm_mode_connector_set_path_property(connector
, pathprop
);
455 drm_reinit_primary_mode_group(dev
);
456 mutex_lock(&dev
->mode_config
.mutex
);
457 intel_connector_add_to_fbdev(intel_connector
);
458 mutex_unlock(&dev
->mode_config
.mutex
);
459 drm_connector_register(&intel_connector
->base
);
463 static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr
*mgr
,
464 struct drm_connector
*connector
)
466 struct intel_connector
*intel_connector
= to_intel_connector(connector
);
467 struct drm_device
*dev
= connector
->dev
;
468 /* need to nuke the connector */
469 mutex_lock(&dev
->mode_config
.mutex
);
470 intel_connector_dpms(connector
, DRM_MODE_DPMS_OFF
);
471 mutex_unlock(&dev
->mode_config
.mutex
);
473 intel_connector
->unregister(intel_connector
);
475 mutex_lock(&dev
->mode_config
.mutex
);
476 intel_connector_remove_from_fbdev(intel_connector
);
477 drm_connector_cleanup(connector
);
478 mutex_unlock(&dev
->mode_config
.mutex
);
480 drm_reinit_primary_mode_group(dev
);
482 kfree(intel_connector
);
486 static void intel_dp_mst_hotplug(struct drm_dp_mst_topology_mgr
*mgr
)
488 struct intel_dp
*intel_dp
= container_of(mgr
, struct intel_dp
, mst_mgr
);
489 struct intel_digital_port
*intel_dig_port
= dp_to_dig_port(intel_dp
);
490 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
492 drm_kms_helper_hotplug_event(dev
);
495 static struct drm_dp_mst_topology_cbs mst_cbs
= {
496 .add_connector
= intel_dp_add_mst_connector
,
497 .destroy_connector
= intel_dp_destroy_mst_connector
,
498 .hotplug
= intel_dp_mst_hotplug
,
501 static struct intel_dp_mst_encoder
*
502 intel_dp_create_fake_mst_encoder(struct intel_digital_port
*intel_dig_port
, enum pipe pipe
)
504 struct intel_dp_mst_encoder
*intel_mst
;
505 struct intel_encoder
*intel_encoder
;
506 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
508 intel_mst
= kzalloc(sizeof(*intel_mst
), GFP_KERNEL
);
513 intel_mst
->pipe
= pipe
;
514 intel_encoder
= &intel_mst
->base
;
515 intel_mst
->primary
= intel_dig_port
;
517 drm_encoder_init(dev
, &intel_encoder
->base
, &intel_dp_mst_enc_funcs
,
518 DRM_MODE_ENCODER_DPMST
);
520 intel_encoder
->type
= INTEL_OUTPUT_DP_MST
;
521 intel_encoder
->crtc_mask
= 0x7;
522 intel_encoder
->cloneable
= 0;
524 intel_encoder
->compute_config
= intel_dp_mst_compute_config
;
525 intel_encoder
->disable
= intel_mst_disable_dp
;
526 intel_encoder
->post_disable
= intel_mst_post_disable_dp
;
527 intel_encoder
->pre_enable
= intel_mst_pre_enable_dp
;
528 intel_encoder
->enable
= intel_mst_enable_dp
;
529 intel_encoder
->get_hw_state
= intel_dp_mst_enc_get_hw_state
;
530 intel_encoder
->get_config
= intel_dp_mst_enc_get_config
;
537 intel_dp_create_fake_mst_encoders(struct intel_digital_port
*intel_dig_port
)
540 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
542 for (i
= PIPE_A
; i
<= PIPE_C
; i
++)
543 intel_dp
->mst_encoders
[i
] = intel_dp_create_fake_mst_encoder(intel_dig_port
, i
);
548 intel_dp_mst_encoder_init(struct intel_digital_port
*intel_dig_port
, int conn_base_id
)
550 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
551 struct drm_device
*dev
= intel_dig_port
->base
.base
.dev
;
554 intel_dp
->can_mst
= true;
555 intel_dp
->mst_mgr
.cbs
= &mst_cbs
;
557 /* create encoders */
558 intel_dp_create_fake_mst_encoders(intel_dig_port
);
559 ret
= drm_dp_mst_topology_mgr_init(&intel_dp
->mst_mgr
, dev
->dev
, &intel_dp
->aux
, 16, 3, conn_base_id
);
561 intel_dp
->can_mst
= false;
568 intel_dp_mst_encoder_cleanup(struct intel_digital_port
*intel_dig_port
)
570 struct intel_dp
*intel_dp
= &intel_dig_port
->dp
;
572 if (!intel_dp
->can_mst
)
575 drm_dp_mst_topology_mgr_destroy(&intel_dp
->mst_mgr
);
576 /* encoders will get killed by normal cleanup */