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