Loading...
1/*
2 * Copyright © 2008 Intel Corporation
3 * 2014 Red Hat Inc.
4 *
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:
11 *
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.
15 *
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.
23 *
24 */
25
26#include <drm/drm_atomic_helper.h>
27#include <drm/drm_edid.h>
28#include <drm/drm_probe_helper.h>
29
30#include "i915_drv.h"
31#include "intel_atomic.h"
32#include "intel_audio.h"
33#include "intel_connector.h"
34#include "intel_ddi.h"
35#include "intel_display_types.h"
36#include "intel_dp.h"
37#include "intel_dp_mst.h"
38#include "intel_dpio_phy.h"
39
40static int intel_dp_mst_compute_link_config(struct intel_encoder *encoder,
41 struct intel_crtc_state *crtc_state,
42 struct drm_connector_state *conn_state,
43 struct link_config_limits *limits)
44{
45 struct drm_atomic_state *state = crtc_state->base.state;
46 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
47 struct intel_dp *intel_dp = &intel_mst->primary->dp;
48 struct intel_connector *connector =
49 to_intel_connector(conn_state->connector);
50 const struct drm_display_mode *adjusted_mode =
51 &crtc_state->base.adjusted_mode;
52 void *port = connector->port;
53 bool constant_n = drm_dp_has_quirk(&intel_dp->desc,
54 DP_DPCD_QUIRK_CONSTANT_N);
55 int bpp, slots = -EINVAL;
56
57 crtc_state->lane_count = limits->max_lane_count;
58 crtc_state->port_clock = limits->max_clock;
59
60 for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
61 crtc_state->pipe_bpp = bpp;
62
63 crtc_state->pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock,
64 crtc_state->pipe_bpp);
65
66 slots = drm_dp_atomic_find_vcpi_slots(state, &intel_dp->mst_mgr,
67 port, crtc_state->pbn);
68 if (slots == -EDEADLK)
69 return slots;
70 if (slots >= 0)
71 break;
72 }
73
74 if (slots < 0) {
75 DRM_DEBUG_KMS("failed finding vcpi slots:%d\n", slots);
76 return slots;
77 }
78
79 intel_link_compute_m_n(crtc_state->pipe_bpp,
80 crtc_state->lane_count,
81 adjusted_mode->crtc_clock,
82 crtc_state->port_clock,
83 &crtc_state->dp_m_n,
84 constant_n, crtc_state->fec_enable);
85 crtc_state->dp_m_n.tu = slots;
86
87 return 0;
88}
89
90static int intel_dp_mst_compute_config(struct intel_encoder *encoder,
91 struct intel_crtc_state *pipe_config,
92 struct drm_connector_state *conn_state)
93{
94 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
95 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
96 struct intel_dp *intel_dp = &intel_mst->primary->dp;
97 struct intel_connector *connector =
98 to_intel_connector(conn_state->connector);
99 struct intel_digital_connector_state *intel_conn_state =
100 to_intel_digital_connector_state(conn_state);
101 const struct drm_display_mode *adjusted_mode =
102 &pipe_config->base.adjusted_mode;
103 void *port = connector->port;
104 struct link_config_limits limits;
105 int ret;
106
107 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
108 return -EINVAL;
109
110 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
111 pipe_config->has_pch_encoder = false;
112
113 if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
114 pipe_config->has_audio =
115 drm_dp_mst_port_has_audio(&intel_dp->mst_mgr, port);
116 else
117 pipe_config->has_audio =
118 intel_conn_state->force_audio == HDMI_AUDIO_ON;
119
120 /*
121 * for MST we always configure max link bw - the spec doesn't
122 * seem to suggest we should do otherwise.
123 */
124 limits.min_clock =
125 limits.max_clock = intel_dp_max_link_rate(intel_dp);
126
127 limits.min_lane_count =
128 limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
129
130 limits.min_bpp = intel_dp_min_bpp(pipe_config);
131 /*
132 * FIXME: If all the streams can't fit into the link with
133 * their current pipe_bpp we should reduce pipe_bpp across
134 * the board until things start to fit. Until then we
135 * limit to <= 8bpc since that's what was hardcoded for all
136 * MST streams previously. This hack should be removed once
137 * we have the proper retry logic in place.
138 */
139 limits.max_bpp = min(pipe_config->pipe_bpp, 24);
140
141 intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
142
143 ret = intel_dp_mst_compute_link_config(encoder, pipe_config,
144 conn_state, &limits);
145 if (ret)
146 return ret;
147
148 pipe_config->limited_color_range =
149 intel_dp_limited_color_range(pipe_config, conn_state);
150
151 if (IS_GEN9_LP(dev_priv))
152 pipe_config->lane_lat_optim_mask =
153 bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
154
155 intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
156
157 return 0;
158}
159
160static int
161intel_dp_mst_atomic_check(struct drm_connector *connector,
162 struct drm_atomic_state *state)
163{
164 struct drm_connector_state *new_conn_state =
165 drm_atomic_get_new_connector_state(state, connector);
166 struct drm_connector_state *old_conn_state =
167 drm_atomic_get_old_connector_state(state, connector);
168 struct intel_connector *intel_connector =
169 to_intel_connector(connector);
170 struct drm_crtc *new_crtc = new_conn_state->crtc;
171 struct drm_crtc_state *crtc_state;
172 struct drm_dp_mst_topology_mgr *mgr;
173 int ret;
174
175 ret = intel_digital_connector_atomic_check(connector, state);
176 if (ret)
177 return ret;
178
179 if (!old_conn_state->crtc)
180 return 0;
181
182 /* We only want to free VCPI if this state disables the CRTC on this
183 * connector
184 */
185 if (new_crtc) {
186 crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
187
188 if (!crtc_state ||
189 !drm_atomic_crtc_needs_modeset(crtc_state) ||
190 crtc_state->enable)
191 return 0;
192 }
193
194 mgr = &enc_to_mst(old_conn_state->best_encoder)->primary->dp.mst_mgr;
195 ret = drm_dp_atomic_release_vcpi_slots(state, mgr,
196 intel_connector->port);
197
198 return ret;
199}
200
201static void intel_mst_disable_dp(struct intel_encoder *encoder,
202 const struct intel_crtc_state *old_crtc_state,
203 const struct drm_connector_state *old_conn_state)
204{
205 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
206 struct intel_digital_port *intel_dig_port = intel_mst->primary;
207 struct intel_dp *intel_dp = &intel_dig_port->dp;
208 struct intel_connector *connector =
209 to_intel_connector(old_conn_state->connector);
210 int ret;
211
212 DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
213
214 drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, connector->port);
215
216 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
217 if (ret) {
218 DRM_ERROR("failed to update payload %d\n", ret);
219 }
220 if (old_crtc_state->has_audio)
221 intel_audio_codec_disable(encoder,
222 old_crtc_state, old_conn_state);
223}
224
225static void intel_mst_post_disable_dp(struct intel_encoder *encoder,
226 const struct intel_crtc_state *old_crtc_state,
227 const struct drm_connector_state *old_conn_state)
228{
229 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
230 struct intel_digital_port *intel_dig_port = intel_mst->primary;
231 struct intel_dp *intel_dp = &intel_dig_port->dp;
232 struct intel_connector *connector =
233 to_intel_connector(old_conn_state->connector);
234
235 intel_ddi_disable_pipe_clock(old_crtc_state);
236
237 /* this can fail */
238 drm_dp_check_act_status(&intel_dp->mst_mgr);
239 /* and this can also fail */
240 drm_dp_update_payload_part2(&intel_dp->mst_mgr);
241
242 drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
243
244 /*
245 * Power down mst path before disabling the port, otherwise we end
246 * up getting interrupts from the sink upon detecting link loss.
247 */
248 drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
249 false);
250
251 intel_dp->active_mst_links--;
252
253 intel_mst->connector = NULL;
254 if (intel_dp->active_mst_links == 0) {
255 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
256 intel_dig_port->base.post_disable(&intel_dig_port->base,
257 old_crtc_state, NULL);
258 }
259
260 DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
261}
262
263static void intel_mst_pre_pll_enable_dp(struct intel_encoder *encoder,
264 const struct intel_crtc_state *pipe_config,
265 const struct drm_connector_state *conn_state)
266{
267 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
268 struct intel_digital_port *intel_dig_port = intel_mst->primary;
269 struct intel_dp *intel_dp = &intel_dig_port->dp;
270
271 if (intel_dp->active_mst_links == 0)
272 intel_dig_port->base.pre_pll_enable(&intel_dig_port->base,
273 pipe_config, NULL);
274}
275
276static void intel_mst_post_pll_disable_dp(struct intel_encoder *encoder,
277 const struct intel_crtc_state *old_crtc_state,
278 const struct drm_connector_state *old_conn_state)
279{
280 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
281 struct intel_digital_port *intel_dig_port = intel_mst->primary;
282 struct intel_dp *intel_dp = &intel_dig_port->dp;
283
284 if (intel_dp->active_mst_links == 0)
285 intel_dig_port->base.post_pll_disable(&intel_dig_port->base,
286 old_crtc_state,
287 old_conn_state);
288}
289
290static void intel_mst_pre_enable_dp(struct intel_encoder *encoder,
291 const struct intel_crtc_state *pipe_config,
292 const struct drm_connector_state *conn_state)
293{
294 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
295 struct intel_digital_port *intel_dig_port = intel_mst->primary;
296 struct intel_dp *intel_dp = &intel_dig_port->dp;
297 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
298 enum port port = intel_dig_port->base.port;
299 struct intel_connector *connector =
300 to_intel_connector(conn_state->connector);
301 int ret;
302 u32 temp;
303
304 /* MST encoders are bound to a crtc, not to a connector,
305 * force the mapping here for get_hw_state.
306 */
307 connector->encoder = encoder;
308 intel_mst->connector = connector;
309
310 DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
311
312 if (intel_dp->active_mst_links == 0)
313 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
314
315 drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
316
317 if (intel_dp->active_mst_links == 0)
318 intel_dig_port->base.pre_enable(&intel_dig_port->base,
319 pipe_config, NULL);
320
321 ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
322 connector->port,
323 pipe_config->pbn,
324 pipe_config->dp_m_n.tu);
325 if (!ret)
326 DRM_ERROR("failed to allocate vcpi\n");
327
328 intel_dp->active_mst_links++;
329 temp = I915_READ(DP_TP_STATUS(port));
330 I915_WRITE(DP_TP_STATUS(port), temp);
331
332 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
333
334 intel_ddi_enable_pipe_clock(pipe_config);
335}
336
337static void intel_mst_enable_dp(struct intel_encoder *encoder,
338 const struct intel_crtc_state *pipe_config,
339 const struct drm_connector_state *conn_state)
340{
341 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
342 struct intel_digital_port *intel_dig_port = intel_mst->primary;
343 struct intel_dp *intel_dp = &intel_dig_port->dp;
344 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
345 enum port port = intel_dig_port->base.port;
346
347 DRM_DEBUG_KMS("active links %d\n", intel_dp->active_mst_links);
348
349 if (intel_de_wait_for_set(dev_priv, DP_TP_STATUS(port),
350 DP_TP_STATUS_ACT_SENT, 1))
351 DRM_ERROR("Timed out waiting for ACT sent\n");
352
353 drm_dp_check_act_status(&intel_dp->mst_mgr);
354
355 drm_dp_update_payload_part2(&intel_dp->mst_mgr);
356 if (pipe_config->has_audio)
357 intel_audio_codec_enable(encoder, pipe_config, conn_state);
358}
359
360static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
361 enum pipe *pipe)
362{
363 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
364 *pipe = intel_mst->pipe;
365 if (intel_mst->connector)
366 return true;
367 return false;
368}
369
370static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
371 struct intel_crtc_state *pipe_config)
372{
373 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(&encoder->base);
374 struct intel_digital_port *intel_dig_port = intel_mst->primary;
375
376 intel_ddi_get_config(&intel_dig_port->base, pipe_config);
377}
378
379static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
380{
381 struct intel_connector *intel_connector = to_intel_connector(connector);
382 struct intel_dp *intel_dp = intel_connector->mst_port;
383 struct edid *edid;
384 int ret;
385
386 if (drm_connector_is_unregistered(connector))
387 return intel_connector_update_modes(connector, NULL);
388
389 edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
390 ret = intel_connector_update_modes(connector, edid);
391 kfree(edid);
392
393 return ret;
394}
395
396static enum drm_connector_status
397intel_dp_mst_detect(struct drm_connector *connector, bool force)
398{
399 struct intel_connector *intel_connector = to_intel_connector(connector);
400 struct intel_dp *intel_dp = intel_connector->mst_port;
401
402 if (drm_connector_is_unregistered(connector))
403 return connector_status_disconnected;
404 return drm_dp_mst_detect_port(connector, &intel_dp->mst_mgr,
405 intel_connector->port);
406}
407
408static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
409 .detect = intel_dp_mst_detect,
410 .fill_modes = drm_helper_probe_single_connector_modes,
411 .atomic_get_property = intel_digital_connector_atomic_get_property,
412 .atomic_set_property = intel_digital_connector_atomic_set_property,
413 .late_register = intel_connector_register,
414 .early_unregister = intel_connector_unregister,
415 .destroy = intel_connector_destroy,
416 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
417 .atomic_duplicate_state = intel_digital_connector_duplicate_state,
418};
419
420static int intel_dp_mst_get_modes(struct drm_connector *connector)
421{
422 return intel_dp_mst_get_ddc_modes(connector);
423}
424
425static enum drm_mode_status
426intel_dp_mst_mode_valid(struct drm_connector *connector,
427 struct drm_display_mode *mode)
428{
429 struct intel_connector *intel_connector = to_intel_connector(connector);
430 struct intel_dp *intel_dp = intel_connector->mst_port;
431 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
432 int max_rate, mode_rate, max_lanes, max_link_clock;
433
434 if (drm_connector_is_unregistered(connector))
435 return MODE_ERROR;
436
437 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
438 return MODE_NO_DBLESCAN;
439
440 max_link_clock = intel_dp_max_link_rate(intel_dp);
441 max_lanes = intel_dp_max_lane_count(intel_dp);
442
443 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
444 mode_rate = intel_dp_link_required(mode->clock, 18);
445
446 /* TODO - validate mode against available PBN for link */
447 if (mode->clock < 10000)
448 return MODE_CLOCK_LOW;
449
450 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
451 return MODE_H_ILLEGAL;
452
453 if (mode_rate > max_rate || mode->clock > max_dotclk)
454 return MODE_CLOCK_HIGH;
455
456 return MODE_OK;
457}
458
459static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
460 struct drm_connector_state *state)
461{
462 struct intel_connector *intel_connector = to_intel_connector(connector);
463 struct intel_dp *intel_dp = intel_connector->mst_port;
464 struct intel_crtc *crtc = to_intel_crtc(state->crtc);
465
466 return &intel_dp->mst_encoders[crtc->pipe]->base.base;
467}
468
469static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
470 .get_modes = intel_dp_mst_get_modes,
471 .mode_valid = intel_dp_mst_mode_valid,
472 .atomic_best_encoder = intel_mst_atomic_best_encoder,
473 .atomic_check = intel_dp_mst_atomic_check,
474};
475
476static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
477{
478 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
479
480 drm_encoder_cleanup(encoder);
481 kfree(intel_mst);
482}
483
484static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
485 .destroy = intel_dp_mst_encoder_destroy,
486};
487
488static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
489{
490 if (connector->encoder && connector->base.state->crtc) {
491 enum pipe pipe;
492 if (!connector->encoder->get_hw_state(connector->encoder, &pipe))
493 return false;
494 return true;
495 }
496 return false;
497}
498
499static struct drm_connector *intel_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, const char *pathprop)
500{
501 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
502 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
503 struct drm_device *dev = intel_dig_port->base.base.dev;
504 struct drm_i915_private *dev_priv = to_i915(dev);
505 struct intel_connector *intel_connector;
506 struct drm_connector *connector;
507 enum pipe pipe;
508 int ret;
509
510 intel_connector = intel_connector_alloc();
511 if (!intel_connector)
512 return NULL;
513
514 intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
515 intel_connector->mst_port = intel_dp;
516 intel_connector->port = port;
517 drm_dp_mst_get_port_malloc(port);
518
519 connector = &intel_connector->base;
520 ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
521 DRM_MODE_CONNECTOR_DisplayPort);
522 if (ret) {
523 intel_connector_free(intel_connector);
524 return NULL;
525 }
526
527 drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
528
529 for_each_pipe(dev_priv, pipe) {
530 struct drm_encoder *enc =
531 &intel_dp->mst_encoders[pipe]->base.base;
532
533 ret = drm_connector_attach_encoder(&intel_connector->base, enc);
534 if (ret)
535 goto err;
536 }
537
538 drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
539 drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
540
541 ret = drm_connector_set_path_property(connector, pathprop);
542 if (ret)
543 goto err;
544
545 intel_attach_force_audio_property(connector);
546 intel_attach_broadcast_rgb_property(connector);
547
548 /*
549 * Reuse the prop from the SST connector because we're
550 * not allowed to create new props after device registration.
551 */
552 connector->max_bpc_property =
553 intel_dp->attached_connector->base.max_bpc_property;
554 if (connector->max_bpc_property)
555 drm_connector_attach_max_bpc_property(connector, 6, 12);
556
557 return connector;
558
559err:
560 drm_connector_cleanup(connector);
561 return NULL;
562}
563
564static void intel_dp_register_mst_connector(struct drm_connector *connector)
565{
566 struct drm_i915_private *dev_priv = to_i915(connector->dev);
567
568 if (dev_priv->fbdev)
569 drm_fb_helper_add_one_connector(&dev_priv->fbdev->helper,
570 connector);
571
572 drm_connector_register(connector);
573}
574
575static void intel_dp_destroy_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
576 struct drm_connector *connector)
577{
578 struct drm_i915_private *dev_priv = to_i915(connector->dev);
579
580 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, connector->name);
581 drm_connector_unregister(connector);
582
583 if (dev_priv->fbdev)
584 drm_fb_helper_remove_one_connector(&dev_priv->fbdev->helper,
585 connector);
586
587 drm_connector_put(connector);
588}
589
590static const struct drm_dp_mst_topology_cbs mst_cbs = {
591 .add_connector = intel_dp_add_mst_connector,
592 .register_connector = intel_dp_register_mst_connector,
593 .destroy_connector = intel_dp_destroy_mst_connector,
594};
595
596static struct intel_dp_mst_encoder *
597intel_dp_create_fake_mst_encoder(struct intel_digital_port *intel_dig_port, enum pipe pipe)
598{
599 struct intel_dp_mst_encoder *intel_mst;
600 struct intel_encoder *intel_encoder;
601 struct drm_device *dev = intel_dig_port->base.base.dev;
602
603 intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
604
605 if (!intel_mst)
606 return NULL;
607
608 intel_mst->pipe = pipe;
609 intel_encoder = &intel_mst->base;
610 intel_mst->primary = intel_dig_port;
611
612 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
613 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
614
615 intel_encoder->type = INTEL_OUTPUT_DP_MST;
616 intel_encoder->power_domain = intel_dig_port->base.power_domain;
617 intel_encoder->port = intel_dig_port->base.port;
618 intel_encoder->crtc_mask = 0x7;
619 intel_encoder->cloneable = 0;
620
621 intel_encoder->compute_config = intel_dp_mst_compute_config;
622 intel_encoder->disable = intel_mst_disable_dp;
623 intel_encoder->post_disable = intel_mst_post_disable_dp;
624 intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
625 intel_encoder->post_pll_disable = intel_mst_post_pll_disable_dp;
626 intel_encoder->pre_enable = intel_mst_pre_enable_dp;
627 intel_encoder->enable = intel_mst_enable_dp;
628 intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
629 intel_encoder->get_config = intel_dp_mst_enc_get_config;
630
631 return intel_mst;
632
633}
634
635static bool
636intel_dp_create_fake_mst_encoders(struct intel_digital_port *intel_dig_port)
637{
638 struct intel_dp *intel_dp = &intel_dig_port->dp;
639 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
640 enum pipe pipe;
641
642 for_each_pipe(dev_priv, pipe)
643 intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(intel_dig_port, pipe);
644 return true;
645}
646
647int
648intel_dp_mst_encoder_active_links(struct intel_digital_port *intel_dig_port)
649{
650 return intel_dig_port->dp.active_mst_links;
651}
652
653int
654intel_dp_mst_encoder_init(struct intel_digital_port *intel_dig_port, int conn_base_id)
655{
656 struct intel_dp *intel_dp = &intel_dig_port->dp;
657 struct drm_device *dev = intel_dig_port->base.base.dev;
658 int ret;
659
660 intel_dp->can_mst = true;
661 intel_dp->mst_mgr.cbs = &mst_cbs;
662
663 /* create encoders */
664 intel_dp_create_fake_mst_encoders(intel_dig_port);
665 ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, dev,
666 &intel_dp->aux, 16, 3, conn_base_id);
667 if (ret) {
668 intel_dp->can_mst = false;
669 return ret;
670 }
671 return 0;
672}
673
674void
675intel_dp_mst_encoder_cleanup(struct intel_digital_port *intel_dig_port)
676{
677 struct intel_dp *intel_dp = &intel_dig_port->dp;
678
679 if (!intel_dp->can_mst)
680 return;
681
682 drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
683 /* encoders will get killed by normal cleanup */
684}
1/*
2 * Copyright © 2008 Intel Corporation
3 * 2014 Red Hat Inc.
4 *
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:
11 *
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.
15 *
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.
23 *
24 */
25
26#include <drm/drm_atomic_helper.h>
27#include <drm/drm_edid.h>
28#include <drm/drm_probe_helper.h>
29
30#include "i915_drv.h"
31#include "intel_atomic.h"
32#include "intel_audio.h"
33#include "intel_connector.h"
34#include "intel_ddi.h"
35#include "intel_display_types.h"
36#include "intel_hotplug.h"
37#include "intel_dp.h"
38#include "intel_dp_mst.h"
39#include "intel_dpio_phy.h"
40
41static int intel_dp_mst_compute_link_config(struct intel_encoder *encoder,
42 struct intel_crtc_state *crtc_state,
43 struct drm_connector_state *conn_state,
44 struct link_config_limits *limits)
45{
46 struct drm_atomic_state *state = crtc_state->uapi.state;
47 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
48 struct intel_dp *intel_dp = &intel_mst->primary->dp;
49 struct intel_connector *connector =
50 to_intel_connector(conn_state->connector);
51 struct drm_i915_private *i915 = to_i915(connector->base.dev);
52 const struct drm_display_mode *adjusted_mode =
53 &crtc_state->hw.adjusted_mode;
54 bool constant_n = drm_dp_has_quirk(&intel_dp->desc, 0,
55 DP_DPCD_QUIRK_CONSTANT_N);
56 int bpp, slots = -EINVAL;
57
58 crtc_state->lane_count = limits->max_lane_count;
59 crtc_state->port_clock = limits->max_clock;
60
61 for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
62 crtc_state->pipe_bpp = bpp;
63
64 crtc_state->pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock,
65 crtc_state->pipe_bpp,
66 false);
67
68 slots = drm_dp_atomic_find_vcpi_slots(state, &intel_dp->mst_mgr,
69 connector->port,
70 crtc_state->pbn, 0);
71 if (slots == -EDEADLK)
72 return slots;
73 if (slots >= 0)
74 break;
75 }
76
77 if (slots < 0) {
78 drm_dbg_kms(&i915->drm, "failed finding vcpi slots:%d\n",
79 slots);
80 return slots;
81 }
82
83 intel_link_compute_m_n(crtc_state->pipe_bpp,
84 crtc_state->lane_count,
85 adjusted_mode->crtc_clock,
86 crtc_state->port_clock,
87 &crtc_state->dp_m_n,
88 constant_n, crtc_state->fec_enable);
89 crtc_state->dp_m_n.tu = slots;
90
91 return 0;
92}
93
94static int intel_dp_mst_compute_config(struct intel_encoder *encoder,
95 struct intel_crtc_state *pipe_config,
96 struct drm_connector_state *conn_state)
97{
98 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
99 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
100 struct intel_dp *intel_dp = &intel_mst->primary->dp;
101 struct intel_connector *connector =
102 to_intel_connector(conn_state->connector);
103 struct intel_digital_connector_state *intel_conn_state =
104 to_intel_digital_connector_state(conn_state);
105 const struct drm_display_mode *adjusted_mode =
106 &pipe_config->hw.adjusted_mode;
107 struct link_config_limits limits;
108 int ret;
109
110 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
111 return -EINVAL;
112
113 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
114 pipe_config->has_pch_encoder = false;
115
116 if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
117 pipe_config->has_audio = connector->port->has_audio;
118 else
119 pipe_config->has_audio =
120 intel_conn_state->force_audio == HDMI_AUDIO_ON;
121
122 /*
123 * for MST we always configure max link bw - the spec doesn't
124 * seem to suggest we should do otherwise.
125 */
126 limits.min_clock =
127 limits.max_clock = intel_dp_max_link_rate(intel_dp);
128
129 limits.min_lane_count =
130 limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
131
132 limits.min_bpp = intel_dp_min_bpp(pipe_config);
133 /*
134 * FIXME: If all the streams can't fit into the link with
135 * their current pipe_bpp we should reduce pipe_bpp across
136 * the board until things start to fit. Until then we
137 * limit to <= 8bpc since that's what was hardcoded for all
138 * MST streams previously. This hack should be removed once
139 * we have the proper retry logic in place.
140 */
141 limits.max_bpp = min(pipe_config->pipe_bpp, 24);
142
143 intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
144
145 ret = intel_dp_mst_compute_link_config(encoder, pipe_config,
146 conn_state, &limits);
147 if (ret)
148 return ret;
149
150 pipe_config->limited_color_range =
151 intel_dp_limited_color_range(pipe_config, conn_state);
152
153 if (IS_GEN9_LP(dev_priv))
154 pipe_config->lane_lat_optim_mask =
155 bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
156
157 intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
158
159 return 0;
160}
161
162/*
163 * Iterate over all connectors and return a mask of
164 * all CPU transcoders streaming over the same DP link.
165 */
166static unsigned int
167intel_dp_mst_transcoder_mask(struct intel_atomic_state *state,
168 struct intel_dp *mst_port)
169{
170 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
171 const struct intel_digital_connector_state *conn_state;
172 struct intel_connector *connector;
173 u8 transcoders = 0;
174 int i;
175
176 if (INTEL_GEN(dev_priv) < 12)
177 return 0;
178
179 for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
180 const struct intel_crtc_state *crtc_state;
181 struct intel_crtc *crtc;
182
183 if (connector->mst_port != mst_port || !conn_state->base.crtc)
184 continue;
185
186 crtc = to_intel_crtc(conn_state->base.crtc);
187 crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
188
189 if (!crtc_state->hw.active)
190 continue;
191
192 transcoders |= BIT(crtc_state->cpu_transcoder);
193 }
194
195 return transcoders;
196}
197
198static int intel_dp_mst_compute_config_late(struct intel_encoder *encoder,
199 struct intel_crtc_state *crtc_state,
200 struct drm_connector_state *conn_state)
201{
202 struct intel_atomic_state *state = to_intel_atomic_state(conn_state->state);
203 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
204 struct intel_dp *intel_dp = &intel_mst->primary->dp;
205
206 /* lowest numbered transcoder will be designated master */
207 crtc_state->mst_master_transcoder =
208 ffs(intel_dp_mst_transcoder_mask(state, intel_dp)) - 1;
209
210 return 0;
211}
212
213/*
214 * If one of the connectors in a MST stream needs a modeset, mark all CRTCs
215 * that shares the same MST stream as mode changed,
216 * intel_modeset_pipe_config()+intel_crtc_check_fastset() will take care to do
217 * a fastset when possible.
218 */
219static int
220intel_dp_mst_atomic_master_trans_check(struct intel_connector *connector,
221 struct intel_atomic_state *state)
222{
223 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
224 struct drm_connector_list_iter connector_list_iter;
225 struct intel_connector *connector_iter;
226
227 if (INTEL_GEN(dev_priv) < 12)
228 return 0;
229
230 if (!intel_connector_needs_modeset(state, &connector->base))
231 return 0;
232
233 drm_connector_list_iter_begin(&dev_priv->drm, &connector_list_iter);
234 for_each_intel_connector_iter(connector_iter, &connector_list_iter) {
235 struct intel_digital_connector_state *conn_iter_state;
236 struct intel_crtc_state *crtc_state;
237 struct intel_crtc *crtc;
238 int ret;
239
240 if (connector_iter->mst_port != connector->mst_port ||
241 connector_iter == connector)
242 continue;
243
244 conn_iter_state = intel_atomic_get_digital_connector_state(state,
245 connector_iter);
246 if (IS_ERR(conn_iter_state)) {
247 drm_connector_list_iter_end(&connector_list_iter);
248 return PTR_ERR(conn_iter_state);
249 }
250
251 if (!conn_iter_state->base.crtc)
252 continue;
253
254 crtc = to_intel_crtc(conn_iter_state->base.crtc);
255 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
256 if (IS_ERR(crtc_state)) {
257 drm_connector_list_iter_end(&connector_list_iter);
258 return PTR_ERR(crtc_state);
259 }
260
261 ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
262 if (ret) {
263 drm_connector_list_iter_end(&connector_list_iter);
264 return ret;
265 }
266 crtc_state->uapi.mode_changed = true;
267 }
268 drm_connector_list_iter_end(&connector_list_iter);
269
270 return 0;
271}
272
273static int
274intel_dp_mst_atomic_check(struct drm_connector *connector,
275 struct drm_atomic_state *_state)
276{
277 struct intel_atomic_state *state = to_intel_atomic_state(_state);
278 struct drm_connector_state *new_conn_state =
279 drm_atomic_get_new_connector_state(&state->base, connector);
280 struct drm_connector_state *old_conn_state =
281 drm_atomic_get_old_connector_state(&state->base, connector);
282 struct intel_connector *intel_connector =
283 to_intel_connector(connector);
284 struct drm_crtc *new_crtc = new_conn_state->crtc;
285 struct drm_dp_mst_topology_mgr *mgr;
286 int ret;
287
288 ret = intel_digital_connector_atomic_check(connector, &state->base);
289 if (ret)
290 return ret;
291
292 ret = intel_dp_mst_atomic_master_trans_check(intel_connector, state);
293 if (ret)
294 return ret;
295
296 if (!old_conn_state->crtc)
297 return 0;
298
299 /* We only want to free VCPI if this state disables the CRTC on this
300 * connector
301 */
302 if (new_crtc) {
303 struct intel_crtc *intel_crtc = to_intel_crtc(new_crtc);
304 struct intel_crtc_state *crtc_state =
305 intel_atomic_get_new_crtc_state(state, intel_crtc);
306
307 if (!crtc_state ||
308 !drm_atomic_crtc_needs_modeset(&crtc_state->uapi) ||
309 crtc_state->uapi.enable)
310 return 0;
311 }
312
313 mgr = &enc_to_mst(to_intel_encoder(old_conn_state->best_encoder))->primary->dp.mst_mgr;
314 ret = drm_dp_atomic_release_vcpi_slots(&state->base, mgr,
315 intel_connector->port);
316
317 return ret;
318}
319
320static void clear_act_sent(struct intel_dp *intel_dp)
321{
322 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
323
324 intel_de_write(i915, intel_dp->regs.dp_tp_status,
325 DP_TP_STATUS_ACT_SENT);
326}
327
328static void wait_for_act_sent(struct intel_dp *intel_dp)
329{
330 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
331
332 if (intel_de_wait_for_set(i915, intel_dp->regs.dp_tp_status,
333 DP_TP_STATUS_ACT_SENT, 1))
334 drm_err(&i915->drm, "Timed out waiting for ACT sent\n");
335
336 drm_dp_check_act_status(&intel_dp->mst_mgr);
337}
338
339static void intel_mst_disable_dp(struct intel_atomic_state *state,
340 struct intel_encoder *encoder,
341 const struct intel_crtc_state *old_crtc_state,
342 const struct drm_connector_state *old_conn_state)
343{
344 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
345 struct intel_digital_port *dig_port = intel_mst->primary;
346 struct intel_dp *intel_dp = &dig_port->dp;
347 struct intel_connector *connector =
348 to_intel_connector(old_conn_state->connector);
349 struct drm_i915_private *i915 = to_i915(connector->base.dev);
350 int ret;
351
352 drm_dbg_kms(&i915->drm, "active links %d\n",
353 intel_dp->active_mst_links);
354
355 drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, connector->port);
356
357 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
358 if (ret) {
359 drm_dbg_kms(&i915->drm, "failed to update payload %d\n", ret);
360 }
361 if (old_crtc_state->has_audio)
362 intel_audio_codec_disable(encoder,
363 old_crtc_state, old_conn_state);
364}
365
366static void intel_mst_post_disable_dp(struct intel_atomic_state *state,
367 struct intel_encoder *encoder,
368 const struct intel_crtc_state *old_crtc_state,
369 const struct drm_connector_state *old_conn_state)
370{
371 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
372 struct intel_digital_port *dig_port = intel_mst->primary;
373 struct intel_dp *intel_dp = &dig_port->dp;
374 struct intel_connector *connector =
375 to_intel_connector(old_conn_state->connector);
376 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
377 bool last_mst_stream;
378 u32 val;
379
380 intel_dp->active_mst_links--;
381 last_mst_stream = intel_dp->active_mst_links == 0;
382 drm_WARN_ON(&dev_priv->drm,
383 INTEL_GEN(dev_priv) >= 12 && last_mst_stream &&
384 !intel_dp_mst_is_master_trans(old_crtc_state));
385
386 intel_crtc_vblank_off(old_crtc_state);
387
388 intel_disable_pipe(old_crtc_state);
389
390 drm_dp_update_payload_part2(&intel_dp->mst_mgr);
391
392 clear_act_sent(intel_dp);
393
394 val = intel_de_read(dev_priv,
395 TRANS_DDI_FUNC_CTL(old_crtc_state->cpu_transcoder));
396 val &= ~TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
397 intel_de_write(dev_priv,
398 TRANS_DDI_FUNC_CTL(old_crtc_state->cpu_transcoder),
399 val);
400
401 wait_for_act_sent(intel_dp);
402
403 drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
404
405 intel_ddi_disable_transcoder_func(old_crtc_state);
406
407 if (INTEL_GEN(dev_priv) >= 9)
408 skl_scaler_disable(old_crtc_state);
409 else
410 ilk_pfit_disable(old_crtc_state);
411
412 /*
413 * Power down mst path before disabling the port, otherwise we end
414 * up getting interrupts from the sink upon detecting link loss.
415 */
416 drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
417 false);
418
419 /*
420 * BSpec 4287: disable DIP after the transcoder is disabled and before
421 * the transcoder clock select is set to none.
422 */
423 if (last_mst_stream)
424 intel_dp_set_infoframes(&dig_port->base, false,
425 old_crtc_state, NULL);
426 /*
427 * From TGL spec: "If multi-stream slave transcoder: Configure
428 * Transcoder Clock Select to direct no clock to the transcoder"
429 *
430 * From older GENs spec: "Configure Transcoder Clock Select to direct
431 * no clock to the transcoder"
432 */
433 if (INTEL_GEN(dev_priv) < 12 || !last_mst_stream)
434 intel_ddi_disable_pipe_clock(old_crtc_state);
435
436
437 intel_mst->connector = NULL;
438 if (last_mst_stream)
439 dig_port->base.post_disable(state, &dig_port->base,
440 old_crtc_state, NULL);
441
442 drm_dbg_kms(&dev_priv->drm, "active links %d\n",
443 intel_dp->active_mst_links);
444}
445
446static void intel_mst_pre_pll_enable_dp(struct intel_atomic_state *state,
447 struct intel_encoder *encoder,
448 const struct intel_crtc_state *pipe_config,
449 const struct drm_connector_state *conn_state)
450{
451 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
452 struct intel_digital_port *dig_port = intel_mst->primary;
453 struct intel_dp *intel_dp = &dig_port->dp;
454
455 if (intel_dp->active_mst_links == 0)
456 dig_port->base.pre_pll_enable(state, &dig_port->base,
457 pipe_config, NULL);
458}
459
460static void intel_mst_pre_enable_dp(struct intel_atomic_state *state,
461 struct intel_encoder *encoder,
462 const struct intel_crtc_state *pipe_config,
463 const struct drm_connector_state *conn_state)
464{
465 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
466 struct intel_digital_port *dig_port = intel_mst->primary;
467 struct intel_dp *intel_dp = &dig_port->dp;
468 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
469 struct intel_connector *connector =
470 to_intel_connector(conn_state->connector);
471 int ret;
472 bool first_mst_stream;
473
474 /* MST encoders are bound to a crtc, not to a connector,
475 * force the mapping here for get_hw_state.
476 */
477 connector->encoder = encoder;
478 intel_mst->connector = connector;
479 first_mst_stream = intel_dp->active_mst_links == 0;
480 drm_WARN_ON(&dev_priv->drm,
481 INTEL_GEN(dev_priv) >= 12 && first_mst_stream &&
482 !intel_dp_mst_is_master_trans(pipe_config));
483
484 drm_dbg_kms(&dev_priv->drm, "active links %d\n",
485 intel_dp->active_mst_links);
486
487 if (first_mst_stream)
488 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
489
490 drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
491
492 if (first_mst_stream)
493 dig_port->base.pre_enable(state, &dig_port->base,
494 pipe_config, NULL);
495
496 ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
497 connector->port,
498 pipe_config->pbn,
499 pipe_config->dp_m_n.tu);
500 if (!ret)
501 drm_err(&dev_priv->drm, "failed to allocate vcpi\n");
502
503 intel_dp->active_mst_links++;
504
505 ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr);
506
507 /*
508 * Before Gen 12 this is not done as part of
509 * dig_port->base.pre_enable() and should be done here. For
510 * Gen 12+ the step in which this should be done is different for the
511 * first MST stream, so it's done on the DDI for the first stream and
512 * here for the following ones.
513 */
514 if (INTEL_GEN(dev_priv) < 12 || !first_mst_stream)
515 intel_ddi_enable_pipe_clock(encoder, pipe_config);
516
517 intel_ddi_set_dp_msa(pipe_config, conn_state);
518
519 intel_dp_set_m_n(pipe_config, M1_N1);
520}
521
522static void intel_mst_enable_dp(struct intel_atomic_state *state,
523 struct intel_encoder *encoder,
524 const struct intel_crtc_state *pipe_config,
525 const struct drm_connector_state *conn_state)
526{
527 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
528 struct intel_digital_port *dig_port = intel_mst->primary;
529 struct intel_dp *intel_dp = &dig_port->dp;
530 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
531 u32 val;
532
533 drm_WARN_ON(&dev_priv->drm, pipe_config->has_pch_encoder);
534
535 clear_act_sent(intel_dp);
536
537 intel_ddi_enable_transcoder_func(encoder, pipe_config);
538
539 val = intel_de_read(dev_priv,
540 TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
541 val |= TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
542 intel_de_write(dev_priv,
543 TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder),
544 val);
545
546 drm_dbg_kms(&dev_priv->drm, "active links %d\n",
547 intel_dp->active_mst_links);
548
549 wait_for_act_sent(intel_dp);
550
551 drm_dp_update_payload_part2(&intel_dp->mst_mgr);
552
553 intel_enable_pipe(pipe_config);
554
555 intel_crtc_vblank_on(pipe_config);
556
557 if (pipe_config->has_audio)
558 intel_audio_codec_enable(encoder, pipe_config, conn_state);
559}
560
561static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
562 enum pipe *pipe)
563{
564 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
565 *pipe = intel_mst->pipe;
566 if (intel_mst->connector)
567 return true;
568 return false;
569}
570
571static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
572 struct intel_crtc_state *pipe_config)
573{
574 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
575 struct intel_digital_port *dig_port = intel_mst->primary;
576
577 intel_ddi_get_config(&dig_port->base, pipe_config);
578}
579
580static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
581{
582 struct intel_connector *intel_connector = to_intel_connector(connector);
583 struct intel_dp *intel_dp = intel_connector->mst_port;
584 struct edid *edid;
585 int ret;
586
587 if (drm_connector_is_unregistered(connector))
588 return intel_connector_update_modes(connector, NULL);
589
590 edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
591 ret = intel_connector_update_modes(connector, edid);
592 kfree(edid);
593
594 return ret;
595}
596
597static int
598intel_dp_mst_connector_late_register(struct drm_connector *connector)
599{
600 struct intel_connector *intel_connector = to_intel_connector(connector);
601 int ret;
602
603 ret = drm_dp_mst_connector_late_register(connector,
604 intel_connector->port);
605 if (ret < 0)
606 return ret;
607
608 ret = intel_connector_register(connector);
609 if (ret < 0)
610 drm_dp_mst_connector_early_unregister(connector,
611 intel_connector->port);
612
613 return ret;
614}
615
616static void
617intel_dp_mst_connector_early_unregister(struct drm_connector *connector)
618{
619 struct intel_connector *intel_connector = to_intel_connector(connector);
620
621 intel_connector_unregister(connector);
622 drm_dp_mst_connector_early_unregister(connector,
623 intel_connector->port);
624}
625
626static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
627 .fill_modes = drm_helper_probe_single_connector_modes,
628 .atomic_get_property = intel_digital_connector_atomic_get_property,
629 .atomic_set_property = intel_digital_connector_atomic_set_property,
630 .late_register = intel_dp_mst_connector_late_register,
631 .early_unregister = intel_dp_mst_connector_early_unregister,
632 .destroy = intel_connector_destroy,
633 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
634 .atomic_duplicate_state = intel_digital_connector_duplicate_state,
635};
636
637static int intel_dp_mst_get_modes(struct drm_connector *connector)
638{
639 return intel_dp_mst_get_ddc_modes(connector);
640}
641
642static int
643intel_dp_mst_mode_valid_ctx(struct drm_connector *connector,
644 struct drm_display_mode *mode,
645 struct drm_modeset_acquire_ctx *ctx,
646 enum drm_mode_status *status)
647{
648 struct drm_i915_private *dev_priv = to_i915(connector->dev);
649 struct intel_connector *intel_connector = to_intel_connector(connector);
650 struct intel_dp *intel_dp = intel_connector->mst_port;
651 struct drm_dp_mst_topology_mgr *mgr = &intel_dp->mst_mgr;
652 struct drm_dp_mst_port *port = intel_connector->port;
653 const int min_bpp = 18;
654 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
655 int max_rate, mode_rate, max_lanes, max_link_clock;
656 int ret;
657
658 if (drm_connector_is_unregistered(connector)) {
659 *status = MODE_ERROR;
660 return 0;
661 }
662
663 if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
664 *status = MODE_NO_DBLESCAN;
665 return 0;
666 }
667
668 max_link_clock = intel_dp_max_link_rate(intel_dp);
669 max_lanes = intel_dp_max_lane_count(intel_dp);
670
671 max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
672 mode_rate = intel_dp_link_required(mode->clock, min_bpp);
673
674 ret = drm_modeset_lock(&mgr->base.lock, ctx);
675 if (ret)
676 return ret;
677
678 if (mode_rate > max_rate || mode->clock > max_dotclk ||
679 drm_dp_calc_pbn_mode(mode->clock, min_bpp, false) > port->full_pbn) {
680 *status = MODE_CLOCK_HIGH;
681 return 0;
682 }
683
684 if (mode->clock < 10000) {
685 *status = MODE_CLOCK_LOW;
686 return 0;
687 }
688
689 if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
690 *status = MODE_H_ILLEGAL;
691 return 0;
692 }
693
694 *status = intel_mode_valid_max_plane_size(dev_priv, mode);
695 return 0;
696}
697
698static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
699 struct drm_connector_state *state)
700{
701 struct intel_connector *intel_connector = to_intel_connector(connector);
702 struct intel_dp *intel_dp = intel_connector->mst_port;
703 struct intel_crtc *crtc = to_intel_crtc(state->crtc);
704
705 return &intel_dp->mst_encoders[crtc->pipe]->base.base;
706}
707
708static int
709intel_dp_mst_detect(struct drm_connector *connector,
710 struct drm_modeset_acquire_ctx *ctx, bool force)
711{
712 struct intel_connector *intel_connector = to_intel_connector(connector);
713 struct intel_dp *intel_dp = intel_connector->mst_port;
714
715 if (drm_connector_is_unregistered(connector))
716 return connector_status_disconnected;
717
718 return drm_dp_mst_detect_port(connector, ctx, &intel_dp->mst_mgr,
719 intel_connector->port);
720}
721
722static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
723 .get_modes = intel_dp_mst_get_modes,
724 .mode_valid_ctx = intel_dp_mst_mode_valid_ctx,
725 .atomic_best_encoder = intel_mst_atomic_best_encoder,
726 .atomic_check = intel_dp_mst_atomic_check,
727 .detect_ctx = intel_dp_mst_detect,
728};
729
730static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
731{
732 struct intel_dp_mst_encoder *intel_mst = enc_to_mst(to_intel_encoder(encoder));
733
734 drm_encoder_cleanup(encoder);
735 kfree(intel_mst);
736}
737
738static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
739 .destroy = intel_dp_mst_encoder_destroy,
740};
741
742static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
743{
744 if (intel_attached_encoder(connector) && connector->base.state->crtc) {
745 enum pipe pipe;
746 if (!intel_attached_encoder(connector)->get_hw_state(intel_attached_encoder(connector), &pipe))
747 return false;
748 return true;
749 }
750 return false;
751}
752
753static struct drm_connector *intel_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, const char *pathprop)
754{
755 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
756 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
757 struct drm_device *dev = dig_port->base.base.dev;
758 struct drm_i915_private *dev_priv = to_i915(dev);
759 struct intel_connector *intel_connector;
760 struct drm_connector *connector;
761 enum pipe pipe;
762 int ret;
763
764 intel_connector = intel_connector_alloc();
765 if (!intel_connector)
766 return NULL;
767
768 intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
769 intel_connector->mst_port = intel_dp;
770 intel_connector->port = port;
771 drm_dp_mst_get_port_malloc(port);
772
773 connector = &intel_connector->base;
774 ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
775 DRM_MODE_CONNECTOR_DisplayPort);
776 if (ret) {
777 intel_connector_free(intel_connector);
778 return NULL;
779 }
780
781 drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
782
783 for_each_pipe(dev_priv, pipe) {
784 struct drm_encoder *enc =
785 &intel_dp->mst_encoders[pipe]->base.base;
786
787 ret = drm_connector_attach_encoder(&intel_connector->base, enc);
788 if (ret)
789 goto err;
790 }
791
792 drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
793 drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
794
795 ret = drm_connector_set_path_property(connector, pathprop);
796 if (ret)
797 goto err;
798
799 intel_attach_force_audio_property(connector);
800 intel_attach_broadcast_rgb_property(connector);
801
802 /*
803 * Reuse the prop from the SST connector because we're
804 * not allowed to create new props after device registration.
805 */
806 connector->max_bpc_property =
807 intel_dp->attached_connector->base.max_bpc_property;
808 if (connector->max_bpc_property)
809 drm_connector_attach_max_bpc_property(connector, 6, 12);
810
811 return connector;
812
813err:
814 drm_connector_cleanup(connector);
815 return NULL;
816}
817
818static void
819intel_dp_mst_poll_hpd_irq(struct drm_dp_mst_topology_mgr *mgr)
820{
821 struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
822
823 intel_hpd_trigger_irq(dp_to_dig_port(intel_dp));
824}
825
826static const struct drm_dp_mst_topology_cbs mst_cbs = {
827 .add_connector = intel_dp_add_mst_connector,
828 .poll_hpd_irq = intel_dp_mst_poll_hpd_irq,
829};
830
831static struct intel_dp_mst_encoder *
832intel_dp_create_fake_mst_encoder(struct intel_digital_port *dig_port, enum pipe pipe)
833{
834 struct intel_dp_mst_encoder *intel_mst;
835 struct intel_encoder *intel_encoder;
836 struct drm_device *dev = dig_port->base.base.dev;
837
838 intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
839
840 if (!intel_mst)
841 return NULL;
842
843 intel_mst->pipe = pipe;
844 intel_encoder = &intel_mst->base;
845 intel_mst->primary = dig_port;
846
847 drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
848 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
849
850 intel_encoder->type = INTEL_OUTPUT_DP_MST;
851 intel_encoder->power_domain = dig_port->base.power_domain;
852 intel_encoder->port = dig_port->base.port;
853 intel_encoder->cloneable = 0;
854 /*
855 * This is wrong, but broken userspace uses the intersection
856 * of possible_crtcs of all the encoders of a given connector
857 * to figure out which crtcs can drive said connector. What
858 * should be used instead is the union of possible_crtcs.
859 * To keep such userspace functioning we must misconfigure
860 * this to make sure the intersection is not empty :(
861 */
862 intel_encoder->pipe_mask = ~0;
863
864 intel_encoder->compute_config = intel_dp_mst_compute_config;
865 intel_encoder->compute_config_late = intel_dp_mst_compute_config_late;
866 intel_encoder->disable = intel_mst_disable_dp;
867 intel_encoder->post_disable = intel_mst_post_disable_dp;
868 intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
869 intel_encoder->pre_enable = intel_mst_pre_enable_dp;
870 intel_encoder->enable = intel_mst_enable_dp;
871 intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
872 intel_encoder->get_config = intel_dp_mst_enc_get_config;
873
874 return intel_mst;
875
876}
877
878static bool
879intel_dp_create_fake_mst_encoders(struct intel_digital_port *dig_port)
880{
881 struct intel_dp *intel_dp = &dig_port->dp;
882 struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
883 enum pipe pipe;
884
885 for_each_pipe(dev_priv, pipe)
886 intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(dig_port, pipe);
887 return true;
888}
889
890int
891intel_dp_mst_encoder_active_links(struct intel_digital_port *dig_port)
892{
893 return dig_port->dp.active_mst_links;
894}
895
896int
897intel_dp_mst_encoder_init(struct intel_digital_port *dig_port, int conn_base_id)
898{
899 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
900 struct intel_dp *intel_dp = &dig_port->dp;
901 enum port port = dig_port->base.port;
902 int ret;
903
904 if (!HAS_DP_MST(i915) || intel_dp_is_edp(intel_dp))
905 return 0;
906
907 if (INTEL_GEN(i915) < 12 && port == PORT_A)
908 return 0;
909
910 if (INTEL_GEN(i915) < 11 && port == PORT_E)
911 return 0;
912
913 intel_dp->mst_mgr.cbs = &mst_cbs;
914
915 /* create encoders */
916 intel_dp_create_fake_mst_encoders(dig_port);
917 ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, &i915->drm,
918 &intel_dp->aux, 16, 3, conn_base_id);
919 if (ret)
920 return ret;
921
922 intel_dp->can_mst = true;
923
924 return 0;
925}
926
927void
928intel_dp_mst_encoder_cleanup(struct intel_digital_port *dig_port)
929{
930 struct intel_dp *intel_dp = &dig_port->dp;
931
932 if (!intel_dp->can_mst)
933 return;
934
935 drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
936 /* encoders will get killed by normal cleanup */
937}
938
939bool intel_dp_mst_is_master_trans(const struct intel_crtc_state *crtc_state)
940{
941 return crtc_state->mst_master_transcoder == crtc_state->cpu_transcoder;
942}
943
944bool intel_dp_mst_is_slave_trans(const struct intel_crtc_state *crtc_state)
945{
946 return crtc_state->mst_master_transcoder != INVALID_TRANSCODER &&
947 crtc_state->mst_master_transcoder != crtc_state->cpu_transcoder;
948}