xref: /linux/drivers/gpu/drm/i915/display/intel_dp_tunnel.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <drm/display/drm_dp_tunnel.h>
7 #include <drm/drm_print.h>
8 
9 #include "intel_atomic.h"
10 #include "intel_display_core.h"
11 #include "intel_display_limits.h"
12 #include "intel_display_types.h"
13 #include "intel_dp.h"
14 #include "intel_dp_link_caps.h"
15 #include "intel_dp_link_training.h"
16 #include "intel_dp_mst.h"
17 #include "intel_dp_tunnel.h"
18 #include "intel_link_bw.h"
19 
20 struct intel_dp_tunnel_inherited_state {
21 	struct drm_dp_tunnel_ref ref[I915_MAX_PIPES];
22 };
23 
24 /**
25  * intel_dp_tunnel_disconnect - Disconnect a DP tunnel from a port
26  * @intel_dp: DP port object the tunnel is connected to
27  *
28  * Disconnect a DP tunnel from @intel_dp, destroying any related state. This
29  * should be called after detecting a sink-disconnect event from the port.
30  */
31 void intel_dp_tunnel_disconnect(struct intel_dp *intel_dp)
32 {
33 	drm_dp_tunnel_destroy(intel_dp->tunnel);
34 	intel_dp->tunnel = NULL;
35 }
36 
37 /**
38  * intel_dp_tunnel_destroy - Destroy a DP tunnel
39  * @intel_dp: DP port object the tunnel is connected to
40  *
41  * Destroy a DP tunnel connected to @intel_dp, after disabling the BW
42  * allocation mode on the tunnel. This should be called while destroying the
43  * port.
44  */
45 void intel_dp_tunnel_destroy(struct intel_dp *intel_dp)
46 {
47 	if (intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
48 		drm_dp_tunnel_disable_bw_alloc(intel_dp->tunnel);
49 
50 	intel_dp_tunnel_disconnect(intel_dp);
51 }
52 
53 static int kbytes_to_mbits(int kbytes)
54 {
55 	return DIV_ROUND_UP(kbytes * 8, 1000);
56 }
57 
58 static int get_current_link_bw(struct intel_dp *intel_dp)
59 {
60 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
61 	struct intel_dp_link_config max_bw_config;
62 
63 	intel_dp_link_caps_get_max_bw_config(link_caps, &max_bw_config);
64 
65 	return intel_dp_max_link_data_rate(intel_dp, max_bw_config.rate,
66 						     max_bw_config.lane_count);
67 }
68 
69 static int __update_tunnel_state(struct intel_dp *intel_dp, bool force_sink_update)
70 {
71 	struct intel_display *display = to_intel_display(intel_dp);
72 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
73 	int ret;
74 
75 	ret = drm_dp_tunnel_update_state(intel_dp->tunnel);
76 	if (ret < 0) {
77 		drm_dbg_kms(display->drm,
78 			    "[DPTUN %s][ENCODER:%d:%s] State update failed (err %pe)\n",
79 			    drm_dp_tunnel_name(intel_dp->tunnel),
80 			    encoder->base.base.id, encoder->base.name,
81 			    ERR_PTR(ret));
82 
83 		return ret;
84 	}
85 
86 	if (!force_sink_update &&
87 	    (ret == 0 || !drm_dp_tunnel_bw_alloc_is_enabled(intel_dp->tunnel)))
88 		return 0;
89 
90 	intel_dp_update_sink_caps(intel_dp);
91 
92 	return 0;
93 }
94 
95 static bool has_tunnel_bw_changed(struct intel_dp *intel_dp, int old_bw)
96 {
97 	struct intel_display *display = to_intel_display(intel_dp);
98 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
99 	int new_bw;
100 
101 	new_bw = get_current_link_bw(intel_dp);
102 
103 	/* Suppress the notification if the mode list can't change due to bw. */
104 	if (old_bw == new_bw)
105 		return false;
106 
107 	drm_dbg_kms(display->drm,
108 		    "[DPTUN %s][ENCODER:%d:%s] Notify users about BW change: %d -> %d\n",
109 		    drm_dp_tunnel_name(intel_dp->tunnel),
110 		    encoder->base.base.id, encoder->base.name,
111 		    kbytes_to_mbits(old_bw), kbytes_to_mbits(new_bw));
112 
113 	return true;
114 }
115 
116 /*
117  * Returns:
118  * - 0 in case of success - if there wasn't any change in the tunnel state
119  *   requiring a user notification
120  * - 1 in case of success - if there was a change in the tunnel state
121  *   requiring a user notification
122  * - Negative error code if updating the tunnel state failed
123  */
124 static int update_tunnel_state(struct intel_dp *intel_dp)
125 {
126 	int old_bw;
127 	int err;
128 
129 	old_bw = get_current_link_bw(intel_dp);
130 
131 	err = __update_tunnel_state(intel_dp, false);
132 	if (err)
133 		return err;
134 
135 	return has_tunnel_bw_changed(intel_dp, old_bw) ? 1 : 0;
136 }
137 
138 /*
139  * Allocate the BW for a tunnel on a DP connector/port if the connector/port
140  * was already active when detecting the tunnel. The allocated BW must be
141  * freed by the next atomic modeset, storing the BW in the
142  * intel_atomic_state::inherited_dp_tunnels, and calling
143  * intel_dp_tunnel_atomic_free_bw().
144  */
145 static int allocate_initial_tunnel_bw_for_pipes(struct intel_dp *intel_dp, u8 pipe_mask)
146 {
147 	struct intel_display *display = to_intel_display(intel_dp);
148 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
149 	struct intel_crtc *crtc;
150 	int tunnel_bw = 0;
151 	int err;
152 
153 	for_each_intel_crtc_in_pipe_mask(display, crtc, pipe_mask) {
154 		const struct intel_crtc_state *crtc_state =
155 			to_intel_crtc_state(crtc->base.state);
156 		int stream_bw = intel_dp_config_required_rate(crtc_state);
157 
158 		tunnel_bw += stream_bw;
159 
160 		drm_dbg_kms(display->drm,
161 			    "[DPTUN %s][ENCODER:%d:%s][CRTC:%d:%s] Initial BW for stream %d: %d/%d Mb/s\n",
162 			    drm_dp_tunnel_name(intel_dp->tunnel),
163 			    encoder->base.base.id, encoder->base.name,
164 			    crtc->base.base.id, crtc->base.name,
165 			    crtc->pipe,
166 			    kbytes_to_mbits(stream_bw), kbytes_to_mbits(tunnel_bw));
167 	}
168 
169 	err = drm_dp_tunnel_alloc_bw(intel_dp->tunnel, tunnel_bw);
170 	if (err) {
171 		drm_dbg_kms(display->drm,
172 			    "[DPTUN %s][ENCODER:%d:%s] Initial BW allocation failed (err %pe)\n",
173 			    drm_dp_tunnel_name(intel_dp->tunnel),
174 			    encoder->base.base.id, encoder->base.name,
175 			    ERR_PTR(err));
176 	}
177 
178 	return err;
179 }
180 
181 static int allocate_initial_tunnel_bw(struct intel_dp *intel_dp,
182 				      struct drm_modeset_acquire_ctx *ctx)
183 {
184 	u8 pipe_mask;
185 	int err;
186 
187 	err = intel_dp_get_active_pipes(intel_dp, ctx, &pipe_mask);
188 	if (err)
189 		return err;
190 
191 	return allocate_initial_tunnel_bw_for_pipes(intel_dp, pipe_mask);
192 }
193 
194 /*
195  * Returns:
196  * - 0 in case of success - after any tunnel detected and added to @intel_dp
197  * - 1 in case of success - after a tunnel detected and added to @intel_dp,
198  *   where the link BW via the tunnel changed in a way requiring a user
199  *   notification
200  * - Negative error code if the tunnel detection failed
201  */
202 static int detect_new_tunnel(struct intel_dp *intel_dp, struct drm_modeset_acquire_ctx *ctx)
203 {
204 	struct intel_display *display = to_intel_display(intel_dp);
205 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
206 	struct drm_dp_tunnel *tunnel;
207 	int old_bw;
208 	int ret;
209 
210 	old_bw = get_current_link_bw(intel_dp);
211 
212 	tunnel = drm_dp_tunnel_detect(display->dp_tunnel_mgr,
213 				      &intel_dp->aux);
214 	if (IS_ERR(tunnel))
215 		return PTR_ERR(tunnel);
216 
217 	intel_dp->tunnel = tunnel;
218 
219 	ret = drm_dp_tunnel_enable_bw_alloc(intel_dp->tunnel);
220 	if (ret) {
221 		if (ret == -EOPNOTSUPP)
222 			return 0;
223 
224 		drm_dbg_kms(display->drm,
225 			    "[DPTUN %s][ENCODER:%d:%s] Failed to enable BW allocation mode (ret %pe)\n",
226 			    drm_dp_tunnel_name(intel_dp->tunnel),
227 			    encoder->base.base.id, encoder->base.name,
228 			    ERR_PTR(ret));
229 
230 		/* Keep the tunnel with BWA disabled */
231 		return 0;
232 	}
233 
234 	ret = allocate_initial_tunnel_bw(intel_dp, ctx);
235 	if (ret < 0) {
236 		intel_dp_tunnel_destroy(intel_dp);
237 
238 		return ret;
239 	}
240 
241 	ret = __update_tunnel_state(intel_dp, true);
242 	if (ret)
243 		return ret;
244 
245 	return has_tunnel_bw_changed(intel_dp, old_bw) ? 1 : 0;
246 }
247 
248 /**
249  * intel_dp_tunnel_detect - Detect a DP tunnel on a port
250  * @intel_dp: DP port object
251  * @ctx: lock context acquired by the connector detection handler
252  *
253  * Detect a DP tunnel on the @intel_dp port, enabling the BW allocation mode
254  * on it if supported and allocating the BW required on an already active port.
255  * The BW allocated this way must be freed by the next atomic modeset calling
256  * intel_dp_tunnel_atomic_free_bw().
257  *
258  * If @intel_dp has already a tunnel detected on it, update the tunnel's state
259  * wrt. its support for BW allocation mode and the available BW via the
260  * tunnel. If the tunnel's state change requires this - for instance the
261  * tunnel's group ID has changed - the tunnel will be dropped and recreated.
262  *
263  * Returns:
264  * - 0 in case of success - after any tunnel detected and added to @intel_dp
265  * - 1 in case the link BW via the new or an already existing tunnel has changed
266  *   in a way that requires notifying user space
267  * - Negative error code, if creating a new tunnel or updating the tunnel
268  *   state failed
269  */
270 int intel_dp_tunnel_detect(struct intel_dp *intel_dp, struct drm_modeset_acquire_ctx *ctx)
271 {
272 	int ret;
273 
274 	if (intel_dp_is_edp(intel_dp))
275 		return 0;
276 
277 	if (intel_dp->tunnel) {
278 		ret = update_tunnel_state(intel_dp);
279 		if (ret >= 0)
280 			return ret;
281 
282 		/* Try to recreate the tunnel after an update error. */
283 		intel_dp_tunnel_destroy(intel_dp);
284 	}
285 
286 	return detect_new_tunnel(intel_dp, ctx);
287 }
288 
289 /**
290  * intel_dp_tunnel_bw_alloc_is_enabled - Query the BW allocation support on a tunnel
291  * @intel_dp: DP port object
292  *
293  * Query whether a DP tunnel is connected on @intel_dp and the tunnel supports
294  * the BW allocation mode.
295  *
296  * Returns %true if the BW allocation mode is supported on @intel_dp.
297  */
298 bool intel_dp_tunnel_bw_alloc_is_enabled(struct intel_dp *intel_dp)
299 {
300 	return drm_dp_tunnel_bw_alloc_is_enabled(intel_dp->tunnel);
301 }
302 
303 /**
304  * intel_dp_tunnel_pr_optimization_supported - Query the PR BW optimization support
305  * @intel_dp: DP port object
306  *
307  * Query whether a DP tunnel supports the PR BW optimization.
308  *
309  * Returns %true if the BW allocation mode is supported on @intel_dp.
310  */
311 bool intel_dp_tunnel_pr_optimization_supported(struct intel_dp *intel_dp)
312 {
313 	struct intel_display *display = to_intel_display(intel_dp);
314 
315 	if (DISPLAY_VER(display) < 35)
316 		return false;
317 
318 	return drm_dp_tunnel_pr_optimization_supported(intel_dp->tunnel);
319 }
320 
321 /**
322  * intel_dp_tunnel_suspend - Suspend a DP tunnel connected on a port
323  * @intel_dp: DP port object
324  *
325  * Suspend a DP tunnel on @intel_dp with BW allocation mode enabled on it.
326  */
327 void intel_dp_tunnel_suspend(struct intel_dp *intel_dp)
328 {
329 	struct intel_display *display = to_intel_display(intel_dp);
330 	struct intel_connector *connector = intel_dp->attached_connector;
331 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
332 
333 	if (!intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
334 		return;
335 
336 	drm_dbg_kms(display->drm,
337 		    "[DPTUN %s][CONNECTOR:%d:%s][ENCODER:%d:%s] Suspend\n",
338 		    drm_dp_tunnel_name(intel_dp->tunnel),
339 		    connector->base.base.id, connector->base.name,
340 		    encoder->base.base.id, encoder->base.name);
341 
342 	drm_dp_tunnel_disable_bw_alloc(intel_dp->tunnel);
343 
344 	intel_dp->tunnel_suspended = true;
345 }
346 
347 /**
348  * intel_dp_tunnel_resume - Resume a DP tunnel connected on a port
349  * @intel_dp: DP port object
350  * @crtc_state: CRTC state
351  * @dpcd_updated: the DPCD DPRX capabilities got updated during resume
352  *
353  * Resume a DP tunnel on @intel_dp with BW allocation mode enabled on it.
354  */
355 void intel_dp_tunnel_resume(struct intel_dp *intel_dp,
356 			    const struct intel_crtc_state *crtc_state,
357 			    bool dpcd_updated)
358 {
359 	struct intel_display *display = to_intel_display(intel_dp);
360 	struct intel_connector *connector = intel_dp->attached_connector;
361 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
362 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
363 	u8 pipe_mask;
364 	int err = 0;
365 
366 	if (!intel_dp->tunnel_suspended)
367 		return;
368 
369 	intel_dp->tunnel_suspended = false;
370 
371 	drm_dbg_kms(display->drm,
372 		    "[DPTUN %s][CONNECTOR:%d:%s][ENCODER:%d:%s] Resume\n",
373 		    drm_dp_tunnel_name(intel_dp->tunnel),
374 		    connector->base.base.id, connector->base.name,
375 		    encoder->base.base.id, encoder->base.name);
376 
377 	/*
378 	 * The TBT Connection Manager requires the GFX driver to read out
379 	 * the sink's DPRX caps to be able to service any BW requests later.
380 	 * During resume overriding the caps in @intel_dp cached before
381 	 * suspend must be avoided, so do here only a dummy read, unless the
382 	 * capabilities were updated already during resume.
383 	 */
384 	if (!dpcd_updated) {
385 		err = intel_dp_read_dprx_caps(intel_dp, dpcd);
386 
387 		if (err) {
388 			drm_dp_tunnel_set_io_error(intel_dp->tunnel);
389 			goto out_err;
390 		}
391 	}
392 
393 	err = drm_dp_tunnel_enable_bw_alloc(intel_dp->tunnel);
394 	if (err)
395 		goto out_err;
396 
397 	pipe_mask = 0;
398 	if (crtc_state) {
399 		struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
400 
401 		/* TODO: Add support for MST */
402 		pipe_mask |= BIT(crtc->pipe);
403 	}
404 
405 	err = allocate_initial_tunnel_bw_for_pipes(intel_dp, pipe_mask);
406 	if (err < 0)
407 		goto out_err;
408 
409 	return;
410 
411 out_err:
412 	drm_dbg_kms(display->drm,
413 		    "[DPTUN %s][CONNECTOR:%d:%s][ENCODER:%d:%s] Tunnel can't be resumed, will drop and reject it (err %pe)\n",
414 		    drm_dp_tunnel_name(intel_dp->tunnel),
415 		    connector->base.base.id, connector->base.name,
416 		    encoder->base.base.id, encoder->base.name,
417 		    ERR_PTR(err));
418 }
419 
420 static struct drm_dp_tunnel *
421 get_inherited_tunnel(struct intel_atomic_state *state, struct intel_crtc *crtc)
422 {
423 	if (!state->inherited_dp_tunnels)
424 		return NULL;
425 
426 	return state->inherited_dp_tunnels->ref[crtc->pipe].tunnel;
427 }
428 
429 static int
430 add_inherited_tunnel(struct intel_atomic_state *state,
431 		     struct drm_dp_tunnel *tunnel,
432 		     struct intel_crtc *crtc)
433 {
434 	struct intel_display *display = to_intel_display(state);
435 	struct drm_dp_tunnel *old_tunnel;
436 
437 	old_tunnel = get_inherited_tunnel(state, crtc);
438 	if (old_tunnel) {
439 		drm_WARN_ON(display->drm, old_tunnel != tunnel);
440 		return 0;
441 	}
442 
443 	if (!state->inherited_dp_tunnels) {
444 		state->inherited_dp_tunnels = kzalloc_obj(*state->inherited_dp_tunnels);
445 		if (!state->inherited_dp_tunnels)
446 			return -ENOMEM;
447 	}
448 
449 	drm_dp_tunnel_ref_get(tunnel, &state->inherited_dp_tunnels->ref[crtc->pipe]);
450 
451 	return 0;
452 }
453 
454 static int check_inherited_tunnel_state(struct intel_atomic_state *state,
455 					struct intel_dp *intel_dp,
456 					const struct intel_digital_connector_state *old_conn_state)
457 {
458 	struct intel_display *display = to_intel_display(state);
459 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
460 	struct intel_connector *connector =
461 		to_intel_connector(old_conn_state->base.connector);
462 	struct intel_crtc *old_crtc;
463 	const struct intel_crtc_state *old_crtc_state;
464 
465 	/*
466 	 * If a BWA tunnel gets detected only after the corresponding
467 	 * connector got enabled already without a BWA tunnel, or a different
468 	 * BWA tunnel (which was removed meanwhile) the old CRTC state won't
469 	 * contain the state of the current tunnel. This tunnel still has a
470 	 * reserved BW, which needs to be released, add the state for such
471 	 * inherited tunnels separately only to this atomic state.
472 	 */
473 	if (!intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
474 		return 0;
475 
476 	if (!old_conn_state->base.crtc)
477 		return 0;
478 
479 	old_crtc = to_intel_crtc(old_conn_state->base.crtc);
480 	old_crtc_state = intel_atomic_get_old_crtc_state(state, old_crtc);
481 
482 	if (!old_crtc_state->hw.active ||
483 	    old_crtc_state->dp_tunnel_ref.tunnel == intel_dp->tunnel)
484 		return 0;
485 
486 	drm_dbg_kms(display->drm,
487 		    "[DPTUN %s][CONNECTOR:%d:%s][ENCODER:%d:%s][CRTC:%d:%s] Adding state for inherited tunnel %p\n",
488 		    drm_dp_tunnel_name(intel_dp->tunnel),
489 		    connector->base.base.id, connector->base.name,
490 		    encoder->base.base.id, encoder->base.name,
491 		    old_crtc->base.base.id, old_crtc->base.name,
492 		    intel_dp->tunnel);
493 
494 	return add_inherited_tunnel(state, intel_dp->tunnel, old_crtc);
495 }
496 
497 /**
498  * intel_dp_tunnel_atomic_cleanup_inherited_state - Free any inherited DP tunnel state
499  * @state: Atomic state
500  *
501  * Free the inherited DP tunnel state in @state.
502  */
503 void intel_dp_tunnel_atomic_cleanup_inherited_state(struct intel_atomic_state *state)
504 {
505 	struct intel_display *display = to_intel_display(state);
506 	enum pipe pipe;
507 
508 	if (!state->inherited_dp_tunnels)
509 		return;
510 
511 	for_each_pipe(display, pipe)
512 		if (state->inherited_dp_tunnels->ref[pipe].tunnel)
513 			drm_dp_tunnel_ref_put(&state->inherited_dp_tunnels->ref[pipe]);
514 
515 	kfree(state->inherited_dp_tunnels);
516 	state->inherited_dp_tunnels = NULL;
517 }
518 
519 static int intel_dp_tunnel_atomic_add_group_state(struct intel_atomic_state *state,
520 						  struct drm_dp_tunnel *tunnel)
521 {
522 	struct intel_display *display = to_intel_display(state);
523 	u32 pipe_mask;
524 	int err;
525 
526 	err = drm_dp_tunnel_atomic_get_group_streams_in_state(&state->base,
527 							      tunnel, &pipe_mask);
528 	if (err)
529 		return err;
530 
531 	drm_WARN_ON(display->drm, pipe_mask & ~((1 << I915_MAX_PIPES) - 1));
532 
533 	return intel_modeset_pipes_in_mask_early(state, "DPTUN", pipe_mask);
534 }
535 
536 /**
537  * intel_dp_tunnel_atomic_add_state_for_crtc - Add CRTC specific DP tunnel state
538  * @state: Atomic state
539  * @crtc: CRTC to add the tunnel state for
540  *
541  * Add the DP tunnel state for @crtc if the CRTC (aka DP tunnel stream) is enabled
542  * via a DP tunnel.
543  *
544  * Return 0 in case of success, a negative error code otherwise.
545  */
546 int intel_dp_tunnel_atomic_add_state_for_crtc(struct intel_atomic_state *state,
547 					      struct intel_crtc *crtc)
548 {
549 	const struct intel_crtc_state *new_crtc_state =
550 		intel_atomic_get_new_crtc_state(state, crtc);
551 	const struct drm_dp_tunnel_state *tunnel_state;
552 	struct drm_dp_tunnel *tunnel = new_crtc_state->dp_tunnel_ref.tunnel;
553 
554 	if (!tunnel)
555 		return 0;
556 
557 	tunnel_state = drm_dp_tunnel_atomic_get_state(&state->base, tunnel);
558 	if (IS_ERR(tunnel_state))
559 		return PTR_ERR(tunnel_state);
560 
561 	return 0;
562 }
563 
564 static int check_group_state(struct intel_atomic_state *state,
565 			     struct intel_dp *intel_dp,
566 			     struct intel_connector *connector,
567 			     struct intel_crtc *crtc)
568 {
569 	struct intel_display *display = to_intel_display(state);
570 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
571 	const struct intel_crtc_state *crtc_state =
572 		intel_atomic_get_new_crtc_state(state, crtc);
573 
574 	if (!crtc_state->dp_tunnel_ref.tunnel)
575 		return 0;
576 
577 	drm_dbg_kms(display->drm,
578 		    "[DPTUN %s][CONNECTOR:%d:%s][ENCODER:%d:%s][CRTC:%d:%s] Adding group state for tunnel %p\n",
579 		    drm_dp_tunnel_name(intel_dp->tunnel),
580 		    connector->base.base.id, connector->base.name,
581 		    encoder->base.base.id, encoder->base.name,
582 		    crtc->base.base.id, crtc->base.name,
583 		    crtc_state->dp_tunnel_ref.tunnel);
584 
585 	return intel_dp_tunnel_atomic_add_group_state(state, crtc_state->dp_tunnel_ref.tunnel);
586 }
587 
588 /**
589  * intel_dp_tunnel_atomic_check_state - Check a connector's DP tunnel specific state
590  * @state: Atomic state
591  * @intel_dp: DP port object
592  * @connector: connector using @intel_dp
593  *
594  * Check and add the DP tunnel atomic state for @intel_dp/@connector to
595  * @state, if there is a DP tunnel detected on @intel_dp with BW allocation
596  * mode enabled on it, or if @intel_dp/@connector was previously enabled via a
597  * DP tunnel.
598  *
599  * Returns 0 in case of success, or a negative error code otherwise.
600  */
601 int intel_dp_tunnel_atomic_check_state(struct intel_atomic_state *state,
602 				       struct intel_dp *intel_dp,
603 				       struct intel_connector *connector)
604 {
605 	const struct intel_digital_connector_state *old_conn_state =
606 		intel_atomic_get_old_connector_state(state, connector);
607 	const struct intel_digital_connector_state *new_conn_state =
608 		intel_atomic_get_new_connector_state(state, connector);
609 	int err;
610 
611 	if (old_conn_state->base.crtc) {
612 		err = check_group_state(state, intel_dp, connector,
613 					to_intel_crtc(old_conn_state->base.crtc));
614 		if (err)
615 			return err;
616 	}
617 
618 	if (new_conn_state->base.crtc &&
619 	    new_conn_state->base.crtc != old_conn_state->base.crtc) {
620 		err = check_group_state(state, intel_dp, connector,
621 					to_intel_crtc(new_conn_state->base.crtc));
622 		if (err)
623 			return err;
624 	}
625 
626 	return check_inherited_tunnel_state(state, intel_dp, old_conn_state);
627 }
628 
629 /**
630  * intel_dp_tunnel_atomic_compute_stream_bw - Compute the BW required by a DP tunnel stream
631  * @state: Atomic state
632  * @intel_dp: DP object
633  * @connector: connector using @intel_dp
634  * @crtc_state: state of CRTC of the given DP tunnel stream
635  *
636  * Compute the required BW of CRTC (aka DP tunnel stream), storing this BW to
637  * the DP tunnel state containing the stream in @state. Before re-calculating a
638  * BW requirement in the crtc_state state the old BW requirement computed by this
639  * function must be cleared by calling intel_dp_tunnel_atomic_clear_stream_bw().
640  *
641  * Returns 0 in case of success, a negative error code otherwise.
642  */
643 int intel_dp_tunnel_atomic_compute_stream_bw(struct intel_atomic_state *state,
644 					     struct intel_dp *intel_dp,
645 					     const struct intel_connector *connector,
646 					     struct intel_crtc_state *crtc_state)
647 {
648 	struct intel_display *display = to_intel_display(state);
649 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
650 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
651 	int required_rate = intel_dp_config_required_rate(crtc_state);
652 	int ret;
653 
654 	if (!intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
655 		return 0;
656 
657 	drm_dbg_kms(display->drm,
658 		    "[DPTUN %s][CONNECTOR:%d:%s][ENCODER:%d:%s][CRTC:%d:%s] Stream %d required BW %d Mb/s\n",
659 		    drm_dp_tunnel_name(intel_dp->tunnel),
660 		    connector->base.base.id, connector->base.name,
661 		    encoder->base.base.id, encoder->base.name,
662 		    crtc->base.base.id, crtc->base.name,
663 		    crtc->pipe,
664 		    kbytes_to_mbits(required_rate));
665 
666 	ret = drm_dp_tunnel_atomic_set_stream_bw(&state->base, intel_dp->tunnel,
667 						 crtc->pipe, required_rate);
668 	if (ret < 0)
669 		return ret;
670 
671 	drm_dp_tunnel_ref_get(intel_dp->tunnel,
672 			      &crtc_state->dp_tunnel_ref);
673 
674 	return 0;
675 }
676 
677 /**
678  * intel_dp_tunnel_atomic_clear_stream_bw - Clear any DP tunnel stream BW requirement
679  * @state: Atomic state
680  * @crtc_state: state of CRTC of the given DP tunnel stream
681  *
682  * Clear any DP tunnel stream BW requirement set by
683  * intel_dp_tunnel_atomic_compute_stream_bw().
684  *
685  * Returns 0 in case of success, a negative error code otherwise.
686  */
687 int intel_dp_tunnel_atomic_clear_stream_bw(struct intel_atomic_state *state,
688 					   struct intel_crtc_state *crtc_state)
689 {
690 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
691 	int err;
692 
693 	if (!crtc_state->dp_tunnel_ref.tunnel)
694 		return 0;
695 
696 	err = drm_dp_tunnel_atomic_set_stream_bw(&state->base,
697 						 crtc_state->dp_tunnel_ref.tunnel,
698 						 crtc->pipe, 0);
699 	if (err)
700 		return err;
701 
702 	drm_dp_tunnel_ref_put(&crtc_state->dp_tunnel_ref);
703 
704 	return 0;
705 }
706 
707 /**
708  * intel_dp_tunnel_atomic_check_link - Check the DP tunnel atomic state
709  * @state: intel atomic state
710  * @limits: link BW limits
711  *
712  * Check the link configuration for all DP tunnels in @state. If the
713  * configuration is invalid @limits will be updated if possible to
714  * reduce the total BW, after which the configuration for all CRTCs in
715  * @state must be recomputed with the updated @limits.
716  *
717  * Returns:
718  *   - 0 if the configuration is valid
719  *   - %-EAGAIN, if the configuration is invalid and @limits got updated
720  *     with fallback values with which the configuration of all CRTCs in
721  *     @state must be recomputed
722  *   - Other negative error, if the configuration is invalid without a
723  *     fallback possibility, or the check failed for another reason
724  */
725 int intel_dp_tunnel_atomic_check_link(struct intel_atomic_state *state,
726 				      struct intel_link_bw_limits *limits)
727 {
728 	u32 failed_stream_mask;
729 	int err;
730 
731 	err = drm_dp_tunnel_atomic_check_stream_bws(&state->base,
732 						    &failed_stream_mask);
733 	if (err != -ENOSPC)
734 		return err;
735 
736 	err = intel_link_bw_reduce_bpp(state, limits,
737 				       failed_stream_mask, "DP tunnel link BW");
738 
739 	return err ? : -EAGAIN;
740 }
741 
742 static void atomic_decrease_bw(struct intel_atomic_state *state)
743 {
744 	struct intel_crtc *crtc;
745 	const struct intel_crtc_state *old_crtc_state;
746 	const struct intel_crtc_state *new_crtc_state;
747 
748 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state) {
749 		const struct drm_dp_tunnel_state *new_tunnel_state;
750 		struct drm_dp_tunnel *tunnel;
751 		int old_bw;
752 		int new_bw;
753 
754 		if (!intel_crtc_needs_modeset(new_crtc_state))
755 			continue;
756 
757 		tunnel = get_inherited_tunnel(state, crtc);
758 		if (!tunnel)
759 			tunnel = old_crtc_state->dp_tunnel_ref.tunnel;
760 
761 		if (!tunnel)
762 			continue;
763 
764 		old_bw = drm_dp_tunnel_get_allocated_bw(tunnel);
765 
766 		new_tunnel_state = drm_dp_tunnel_atomic_get_new_state(&state->base, tunnel);
767 		new_bw = drm_dp_tunnel_atomic_get_required_bw(new_tunnel_state);
768 
769 		if (new_bw >= old_bw)
770 			continue;
771 
772 		drm_dp_tunnel_alloc_bw(tunnel, new_bw);
773 	}
774 }
775 
776 static void queue_retry_work(struct intel_atomic_state *state,
777 			     struct drm_dp_tunnel *tunnel,
778 			     const struct intel_crtc_state *crtc_state)
779 {
780 	struct intel_display *display = to_intel_display(state);
781 	struct intel_encoder *encoder;
782 
783 	encoder = intel_get_crtc_new_encoder(state, crtc_state);
784 
785 	if (!intel_digital_port_connected(encoder))
786 		return;
787 
788 	drm_dbg_kms(display->drm,
789 		    "[DPTUN %s][ENCODER:%d:%s] BW allocation failed on a connected sink\n",
790 		    drm_dp_tunnel_name(tunnel),
791 		    encoder->base.base.id,
792 		    encoder->base.name);
793 
794 	intel_dp_queue_modeset_retry_for_link(state, encoder, crtc_state);
795 }
796 
797 static void atomic_increase_bw(struct intel_atomic_state *state)
798 {
799 	struct intel_crtc *crtc;
800 	const struct intel_crtc_state *crtc_state;
801 
802 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state) {
803 		struct drm_dp_tunnel_state *tunnel_state;
804 		struct drm_dp_tunnel *tunnel = crtc_state->dp_tunnel_ref.tunnel;
805 		int bw;
806 
807 		if (!intel_crtc_needs_modeset(crtc_state))
808 			continue;
809 
810 		if (!tunnel)
811 			continue;
812 
813 		tunnel_state = drm_dp_tunnel_atomic_get_new_state(&state->base, tunnel);
814 
815 		bw = drm_dp_tunnel_atomic_get_required_bw(tunnel_state);
816 
817 		if (drm_dp_tunnel_alloc_bw(tunnel, bw) != 0)
818 			queue_retry_work(state, tunnel, crtc_state);
819 	}
820 }
821 
822 /**
823  * intel_dp_tunnel_atomic_alloc_bw - Allocate the BW for all modeset tunnels
824  * @state: Atomic state
825  *
826  * Allocate the required BW for all tunnels in @state.
827  */
828 void intel_dp_tunnel_atomic_alloc_bw(struct intel_atomic_state *state)
829 {
830 	atomic_decrease_bw(state);
831 	atomic_increase_bw(state);
832 }
833 
834 static u8 lane_count_mask(int lane_count)
835 {
836 	return BIT(ilog2(lane_count));
837 }
838 
839 void intel_dp_tunnel_uhbr_lanes_wa_apply(struct intel_dp *intel_dp)
840 {
841 	struct intel_connector *connector = intel_dp->attached_connector;
842 	struct intel_dp_link_caps_order order =
843 		intel_dp_link_caps_connector_compute_order(connector);
844 	struct intel_dp_link_caps *link_caps = intel_dp->link.caps;
845 	struct intel_dp_link_config link_config;
846 	struct intel_dp_link_caps_iter iter;
847 
848 	if (!intel_dp->disabled_uhbr_lane_mask)
849 		return;
850 
851 	intel_dp_link_caps_iter_start(&iter, link_caps, order, INTEL_DP_LINK_CAPS_FILTER_ALL);
852 	for_each_dp_link_config(&iter, &link_config) {
853 		if (drm_dp_is_uhbr_rate(link_config.rate) &&
854 		    lane_count_mask(link_config.lane_count) & intel_dp->disabled_uhbr_lane_mask)
855 			intel_dp_link_caps_disable_config(link_caps, &link_config);
856 	}
857 	intel_dp_link_caps_iter_end(&iter);
858 }
859 
860 bool intel_dp_tunnel_uhbr_lanes_wa_setup(struct intel_dp *intel_dp)
861 {
862 	u8 old_mask = intel_dp->disabled_uhbr_lane_mask;
863 
864 	if (!intel_dp_tunnel_bw_alloc_is_enabled(intel_dp) ||
865 	    drm_dp_tunnel_128b132b_lane0_mapping_supported(intel_dp->tunnel))
866 		intel_dp->disabled_uhbr_lane_mask = 0;
867 	else
868 		/* TODO: Add support for keeping 2 lanes enabled as well. */
869 		intel_dp->disabled_uhbr_lane_mask = lane_count_mask(1) | lane_count_mask(2);
870 
871 	return intel_dp->disabled_uhbr_lane_mask != old_mask;
872 }
873 
874 void intel_dp_tunnel_uhbr_lanes_wa_reset(struct intel_dp *intel_dp)
875 {
876 	intel_dp->disabled_uhbr_lane_mask = 0;
877 }
878 
879 /**
880  * intel_dp_tunnel_mgr_init - Initialize the DP tunnel manager
881  * @display: display device
882  *
883  * Initialize the DP tunnel manager. The tunnel manager will support the
884  * detection/management of DP tunnels on all DP connectors, so the function
885  * must be called after all these connectors have been registered already.
886  *
887  * Return 0 in case of success, a negative error code otherwise.
888  */
889 int intel_dp_tunnel_mgr_init(struct intel_display *display)
890 {
891 	struct drm_dp_tunnel_mgr *tunnel_mgr;
892 	struct drm_connector_list_iter connector_list_iter;
893 	struct intel_connector *connector;
894 	int dp_connectors = 0;
895 
896 	drm_connector_list_iter_begin(display->drm, &connector_list_iter);
897 	for_each_intel_connector_iter(connector, &connector_list_iter) {
898 		if (connector->base.connector_type != DRM_MODE_CONNECTOR_DisplayPort)
899 			continue;
900 
901 		dp_connectors++;
902 	}
903 	drm_connector_list_iter_end(&connector_list_iter);
904 
905 	tunnel_mgr = drm_dp_tunnel_mgr_create(display->drm, dp_connectors);
906 	if (IS_ERR(tunnel_mgr))
907 		return PTR_ERR(tunnel_mgr);
908 
909 	display->dp_tunnel_mgr = tunnel_mgr;
910 
911 	return 0;
912 }
913 
914 /**
915  * intel_dp_tunnel_mgr_cleanup - Clean up the DP tunnel manager state
916  * @display: display device
917  *
918  * Clean up the DP tunnel manager state.
919  */
920 void intel_dp_tunnel_mgr_cleanup(struct intel_display *display)
921 {
922 	drm_dp_tunnel_mgr_destroy(display->dp_tunnel_mgr);
923 	display->dp_tunnel_mgr = NULL;
924 }
925