1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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>
32
33static bool intel_dp_mst_compute_config(struct intel_encoder *encoder,
34 struct intel_crtc_state *pipe_config)
35{
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;
41 int bpp, i;
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;
47 int mst_pbn;
48
49 pipe_config->dp_encoder_is_mst = true;
50 pipe_config->has_pch_encoder = false;
51 pipe_config->has_dp_encoder = true;
52 bpp = 24;
53
54
55
56
57 lane_count = drm_dp_max_lane_count(intel_dp->dpcd);
58
59
60 pipe_config->lane_count = lane_count;
61
62 pipe_config->pipe_bpp = 24;
63 pipe_config->port_clock = intel_dp_max_link_rate(intel_dp);
64
65 state = pipe_config->base.state;
66
67 for_each_connector_in_state(state, drm_connector, connector_state, i) {
68 connector = to_intel_connector(drm_connector);
69
70 if (connector_state->best_encoder == &encoder->base) {
71 found = connector;
72 break;
73 }
74 }
75
76 if (!found) {
77 DRM_ERROR("can't find connector\n");
78 return false;
79 }
80
81 mst_pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock, bpp);
82
83 pipe_config->pbn = mst_pbn;
84 slots = drm_dp_find_vcpi_slots(&intel_dp->mst_mgr, mst_pbn);
85
86 intel_link_compute_m_n(bpp, lane_count,
87 adjusted_mode->crtc_clock,
88 pipe_config->port_clock,
89 &pipe_config->dp_m_n);
90
91 pipe_config->dp_m_n.tu = slots;
92
93 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
94 hsw_dp_set_ddi_pll_sel(pipe_config);
95
96 return true;
97
98}
99
100static void intel_mst_disable_dp(struct intel_encoder *encoder)
101{
102 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
103 struct intel_digital_port *intel_dig_port = intel_mst->primary;
104 struct intel_dp *intel_dp = &intel_dig_port->dp;
105 int ret;
106
107 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
108
109 drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, intel_mst->port);
110
111 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
112 if (ret) {
113 DRM_ERROR("failed to update payload %d\n", ret);
114 }
115}
116
117static void intel_mst_post_disable_dp(struct intel_encoder *encoder)
118{
119 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
120 struct intel_digital_port *intel_dig_port = intel_mst->primary;
121 struct intel_dp *intel_dp = &intel_dig_port->dp;
122
123 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
124
125
126 drm_dp_check_act_status(&intel_dp->mst_mgr);
127
128 drm_dp_update_payload_part2(&intel_dp->mst_mgr);
129
130 drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, intel_mst->port);
131
132 intel_dp->active_mst_links--;
133 intel_mst->port = NULL;
134 if (intel_dp->active_mst_links == 0) {
135 intel_dig_port->base.post_disable(&intel_dig_port->base);
136 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
137 }
138}
139
140static void intel_mst_pre_enable_dp(struct intel_encoder *encoder)
141{
142 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
143 struct intel_digital_port *intel_dig_port = intel_mst->primary;
144 struct intel_dp *intel_dp = &intel_dig_port->dp;
145 struct drm_device *dev = encoder->base.dev;
146 struct drm_i915_private *dev_priv = dev->dev_private;
147 enum port port = intel_dig_port->port;
148 int ret;
149 uint32_t temp;
150 struct intel_connector *found = NULL, *connector;
151 int slots;
152 struct drm_crtc *crtc = encoder->base.crtc;
153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
154
155 for_each_intel_connector(dev, connector) {
156 if (connector->base.state->best_encoder == &encoder->base) {
157 found = connector;
158 break;
159 }
160 }
161
162 if (!found) {
163 DRM_ERROR("can't find connector\n");
164 return;
165 }
166
167
168
169
170 found->encoder = encoder;
171
172 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
173 intel_mst->port = found->port;
174
175 if (intel_dp->active_mst_links == 0) {
176 intel_prepare_ddi_buffer(&intel_dig_port->base);
177
178 intel_ddi_clk_select(&intel_dig_port->base, intel_crtc->config);
179
180 intel_dp_set_link_params(intel_dp, intel_crtc->config);
181
182 intel_ddi_init_dp_buf_reg(&intel_dig_port->base);
183
184 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
185
186 intel_dp_start_link_train(intel_dp);
187 intel_dp_stop_link_train(intel_dp);
188 }
189
190 ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
191 intel_mst->port,
192 intel_crtc->config->pbn, &slots);
193 if (ret == false) {
194 DRM_ERROR("failed to allocate vcpi\n");
195 return;
196 }
197
198
199 intel_dp->active_mst_links++;
200 temp = I915_READ(DP_TP_STATUS(port));
201 I915_WRITE(DP_TP_STATUS(port), temp);
202
203 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
204}
205
206static void intel_mst_enable_dp(struct intel_encoder *encoder)
207{
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_device *dev = intel_dig_port->base.base.dev;
212 struct drm_i915_private *dev_priv = dev->dev_private;
213 enum port port = intel_dig_port->port;
214 int ret;
215
216 DRM_DEBUG_KMS("%d\n", intel_dp->active_mst_links);
217
218 if (wait_for((I915_READ(DP_TP_STATUS(port)) & DP_TP_STATUS_ACT_SENT),
219 1))
220 DRM_ERROR("Timed out waiting for ACT sent\n");
221
222 ret = drm_dp_check_act_status(&intel_dp->mst_mgr);
223
224 ret = drm_dp_update_payload_part2(&intel_dp->mst_mgr);
225}
226
227static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
228 enum pipe *pipe)
229{
230 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
231 *pipe = intel_mst->pipe;
232 if (intel_mst->port)
233 return true;
234 return false;
235}
236
237static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
238 struct intel_crtc_state *pipe_config)
239{
240 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
241 struct intel_digital_port *intel_dig_port = intel_mst->primary;
242 struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
243 struct drm_device *dev = encoder->base.dev;
244 struct drm_i915_private *dev_priv = dev->dev_private;
245 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
246 u32 temp, flags = 0;
247
248 pipe_config->has_dp_encoder = true;
249
250 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
251 if (temp & TRANS_DDI_PHSYNC)
252 flags |= DRM_MODE_FLAG_PHSYNC;
253 else
254 flags |= DRM_MODE_FLAG_NHSYNC;
255 if (temp & TRANS_DDI_PVSYNC)
256 flags |= DRM_MODE_FLAG_PVSYNC;
257 else
258 flags |= DRM_MODE_FLAG_NVSYNC;
259
260 switch (temp & TRANS_DDI_BPC_MASK) {
261 case TRANS_DDI_BPC_6:
262 pipe_config->pipe_bpp = 18;
263 break;
264 case TRANS_DDI_BPC_8:
265 pipe_config->pipe_bpp = 24;
266 break;
267 case TRANS_DDI_BPC_10:
268 pipe_config->pipe_bpp = 30;
269 break;
270 case TRANS_DDI_BPC_12:
271 pipe_config->pipe_bpp = 36;
272 break;
273 default:
274 break;
275 }
276 pipe_config->base.adjusted_mode.flags |= flags;
277
278 pipe_config->lane_count =
279 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
280
281 intel_dp_get_m_n(crtc, pipe_config);
282
283 intel_ddi_clock_get(&intel_dig_port->base, pipe_config);
284}
285
286static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
287{
288 struct intel_connector *intel_connector = to_intel_connector(connector);
289 struct intel_dp *intel_dp = intel_connector->mst_port;
290 struct edid *edid;
291 int ret;
292
293 edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
294 if (!edid)
295 return 0;
296
297 ret = intel_connector_update_modes(connector, edid);
298 kfree(edid);
299
300 return ret;
301}
302
303static enum drm_connector_status
304intel_dp_mst_detect(struct drm_connector *connector, bool force)
305{
306 struct intel_connector *intel_connector = to_intel_connector(connector);
307 struct intel_dp *intel_dp = intel_connector->mst_port;
308
309 return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr, intel_connector->port);
310}
311
312static int
313intel_dp_mst_set_property(struct drm_connector *connector,
314 struct drm_property *property,
315 uint64_t val)
316{
317 return 0;
318}
319
320static void
321intel_dp_mst_connector_destroy(struct drm_connector *connector)
322{
323 struct intel_connector *intel_connector = to_intel_connector(connector);
324
325 if (!IS_ERR_OR_NULL(intel_connector->edid))
326 kfree(intel_connector->edid);
327
328 drm_connector_cleanup(connector);
329 kfree(connector);
330}
331
332static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
333 .dpms = drm_atomic_helper_connector_dpms,
334 .detect = intel_dp_mst_detect,
335 .fill_modes = drm_helper_probe_single_connector_modes,
336 .set_property = intel_dp_mst_set_property,
337 .atomic_get_property = intel_connector_atomic_get_property,
338 .destroy = intel_dp_mst_connector_destroy,
339 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
340 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
341};
342
343static int intel_dp_mst_get_modes(struct drm_connector *connector)
344{
345 return intel_dp_mst_get_ddc_modes(connector);
346}
347
348static enum drm_mode_status
349intel_dp_mst_mode_valid(struct drm_connector *connector,
350 struct drm_display_mode *mode)
351{
352 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
353
354
355 if (mode->clock < 10000)
356 return MODE_CLOCK_LOW;
357
358 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
359 return MODE_H_ILLEGAL;
360
361 if (mode->clock > max_dotclk)
362 return MODE_CLOCK_HIGH;
363
364 return MODE_OK;
365}
366
367static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
368 struct drm_connector_state *state)
369{
370 struct intel_connector *intel_connector = to_intel_connector(connector);
371 struct intel_dp *intel_dp = intel_connector->mst_port;
372 struct intel_crtc *crtc = to_intel_crtc(state->crtc);
373
374 return &intel_dp->mst_encoders[crtc->pipe]->base.base;
375}
376
377static struct drm_encoder *intel_mst_best_encoder(struct drm_connector *connector)
378{
379 struct intel_connector *intel_connector = to_intel_connector(connector);
380 struct intel_dp *intel_dp = intel_connector->mst_port;
381 return &intel_dp->mst_encoders[0]->base.base;
382}
383
384static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
385 .get_modes = intel_dp_mst_get_modes,
386 .mode_valid = intel_dp_mst_mode_valid,
387 .atomic_best_encoder = intel_mst_atomic_best_encoder,
388 .best_encoder = intel_mst_best_encoder,
389};
390
391static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
392{
393 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
394
395 drm_encoder_cleanup(encoder);
396 kfree(intel_mst);
397}
398
399static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
400 .destroy = intel_dp_mst_encoder_destroy,
401};
402
403static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
404{
405 if (connector->encoder && connector->base.state->crtc) {
406 enum pipe pipe;
407 if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
408 return false;
409 return true;
410 }
411 return false;
412}
413
414static void intel_connector_add_to_fbdev(struct intel_connector *connector)
415{
416#ifdef CONFIG_DRM_FBDEV_EMULATION
417 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
418
419 if (dev_priv->fbdev)
420 drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
421 &connector->base);
422#endif
423}
424
425static void intel_connector_remove_from_fbdev(struct intel_connector *connector)
426{
427#ifdef CONFIG_DRM_FBDEV_EMULATION
428 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
429
430 if (dev_priv->fbdev)
431 drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
432 &connector->base);
433#endif
434}
435
436static struct drm_connector *intel_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, const char *pathprop)
437{
438 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
439 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
440 struct drm_device *dev = intel_dig_port->base.base.dev;
441 struct intel_connector *intel_connector;
442 struct drm_connector *connector;
443 int i;
444
445 intel_connector = intel_connector_alloc();
446 if (!intel_connector)
447 return NULL;
448
449 connector = &intel_connector->base;
450 drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs, DRM_MODE_CONNECTOR_DisplayPort);
451 drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
452
453 intel_connector->unregister = intel_connector_unregister;
454 intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
455 intel_connector->mst_port = intel_dp;
456 intel_connector->port = port;
457
458 for (i = PIPE_A; i <= PIPE_C; i++) {
459 drm_mode_connector_attach_encoder(&intel_connector->base,
460 &intel_dp->mst_encoders[i]->base.base);
461 }
462 intel_dp_add_properties(intel_dp, connector);
463
464 drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
465 drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
466
467 drm_mode_connector_set_path_property(connector, pathprop);
468 return connector;
469}
470
471static void intel_dp_register_mst_connector(struct drm_connector *connector)
472{
473 struct intel_connector *intel_connector = to_intel_connector(connector);
474 struct drm_device *dev = connector->dev;
475 drm_modeset_lock_all(dev);
476 intel_connector_add_to_fbdev(intel_connector);
477 drm_modeset_unlock_all(dev);
478 drm_connector_register(&intel_connector->base);
479}
480
481static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
482 struct drm_connector *connector)
483{
484 struct intel_connector *intel_connector = to_intel_connector(connector);
485 struct drm_device *dev = connector->dev;
486
487 intel_connector->unregister(intel_connector);
488
489
490 drm_modeset_lock_all(dev);
491 if (connector->state->crtc) {
492 struct drm_mode_set set;
493 int ret;
494
495 memset(&set, 0, sizeof(set));
496 set.crtc = connector->state->crtc,
497
498 ret = drm_atomic_helper_set_config(&set);
499
500 WARN(ret, "Disabling mst crtc failed with %i\n", ret);
501 }
502
503 intel_connector_remove_from_fbdev(intel_connector);
504 drm_connector_cleanup(connector);
505 drm_modeset_unlock_all(dev);
506
507 kfree(intel_connector);
508 DRM_DEBUG_KMS("\n");
509}
510
511static void intel_dp_mst_hotplug(struct drm_dp_mst_topology_mgr *mgr)
512{
513 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
514 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
515 struct drm_device *dev = intel_dig_port->base.base.dev;
516
517 drm_kms_helper_hotplug_event(dev);
518}
519
520static const struct drm_dp_mst_topology_cbs mst_cbs = {
521 .add_connector = intel_dp_add_mst_connector,
522 .register_connector = intel_dp_register_mst_connector,
523 .destroy_connector = intel_dp_destroy_mst_connector,
524 .hotplug = intel_dp_mst_hotplug,
525};
526
527static struct intel_dp_mst_encoder *
528intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
529{
530 struct intel_dp_mst_encoder *intel_mst;
531 struct intel_encoder *intel_encoder;
532 struct drm_device *dev = intel_dig_port->base.base.dev;
533
534 intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
535
536 if (!intel_mst)
537 return NULL;
538
539 intel_mst->pipe = pipe;
540 intel_encoder = &intel_mst->base;
541 intel_mst->primary = intel_dig_port;
542
543 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
544 DRM_MODE_ENCODER_DPMST, NULL);
545
546 intel_encoder->type = INTEL_OUTPUT_DP_MST;
547 intel_encoder->crtc_mask = 0x7;
548 intel_encoder->cloneable = 0;
549
550 intel_encoder->compute_config = intel_dp_mst_compute_config;
551 intel_encoder->disable = intel_mst_disable_dp;
552 intel_encoder->post_disable = intel_mst_post_disable_dp;
553 intel_encoder->pre_enable = intel_mst_pre_enable_dp;
554 intel_encoder->enable = intel_mst_enable_dp;
555 intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
556 intel_encoder->get_config = intel_dp_mst_enc_get_config;
557
558 return intel_mst;
559
560}
561
562static bool
563intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
564{
565 int i;
566 struct intel_dp *intel_dp = &intel_dig_port->dp;
567
568 for (i = PIPE_A; i <= PIPE_C; i++)
569 intel_dp->mst_encoders[i] = intel_dp_create_fake_mst_encoder(intel_dig_port, i);
570 return true;
571}
572
573int
574intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
575{
576 struct intel_dp *intel_dp = &intel_dig_port->dp;
577 struct drm_device *dev = intel_dig_port->base.base.dev;
578 int ret;
579
580 intel_dp->can_mst = true;
581 intel_dp->mst_mgr.cbs = &mst_cbs;
582
583
584 intel_dp_create_fake_mst_encoders(intel_dig_port);
585 ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, dev->dev, &intel_dp->aux, 16, 3, conn_base_id);
586 if (ret) {
587 intel_dp->can_mst = false;
588 return ret;
589 }
590 return 0;
591}
592
593void
594intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
595{
596 struct intel_dp *intel_dp = &intel_dig_port->dp;
597
598 if (!intel_dp->can_mst)
599 return;
600
601 drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
602
603}
604