Skip to contentSkip to conversation

drm/i915: Add Display Port tunnel BW allocation support

PATCH Superseded Imre Deak 5 participants 61 messages Jan 23, 2024 – Feb 8, 2024 Page 1 of 2

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Imre Deak

author
#0 lore raw

cover drm/i915: Add Display Port tunnel BW allocation support

Add support for detecting DP tunnels on (Thunderbolt) display links and enabling the Bandwidth Allocation mode on the link. This helps in enabling the maximum resolution in any scenario on displays sharing the BW on such links.

Kudos to all Cc'd for advices, co-development and testing.

Cc Mika Westerberg <[email protected]>
Cc Ville Syrjälä <[email protected]>
Cc Saranya Gopal <[email protected]>
Cc Rajaram Regupathy <[email protected]>
Cc Gil Fine <[email protected]>
Cc Naama Shachar <[email protected]>
Cc Pengfei Xu <[email protected]>

Imre Deak (19):
drm/dp: Add drm_dp_max_dprx_data_rate()
drm/dp: Add support for DP tunneling
drm/i915/dp: Add support to notify MST connectors to retry modesets drm/i915/dp: Use drm_dp_max_dprx_data_rate()
drm/i915/dp: Factor out intel_dp_config_required_rate() drm/i915/dp: Export intel_dp_max_common_rate/lane_count() drm/i915/dp: Factor out intel_dp_update_sink_caps() drm/i915/dp: Factor out intel_dp_read_dprx_caps() drm/i915/dp: Add intel_dp_max_link_data_rate() drm/i915/dp: Add way to get active pipes with syncing commits drm/i915/dp: Add support for DP tunnel BW allocation drm/i915/dp: Add DP tunnel atomic state and check BW limit drm/i915/dp: Account for tunnel BW limit in
intel_dp_max_link_data_rate()
drm/i915/dp: Compute DP tunel BW during encoder state computation drm/i915/dp: Allocate/free DP tunnel BW in the encoder enable/disable
hooks
drm/i915/dp: Handle DP tunnel IRQs
drm/i915/dp: Call intel_dp_sync_state() always for DDI DP encoders drm/i915/dp: Suspend/resume DP tunnels
drm/i915/dp: Enable DP tunnel BW allocation mode

 drivers/gpu/drm/display/Kconfig               |   17 +
 drivers/gpu/drm/display/Makefile              |    2 +
 drivers/gpu/drm/display/drm_dp_helper.c       |   58 +
 drivers/gpu/drm/display/drm_dp_tunnel.c       | 1715 +++++++++++++++++
 drivers/gpu/drm/i915/Kconfig                  |   13 +
 drivers/gpu/drm/i915/Kconfig.debug            |    1 +
 drivers/gpu/drm/i915/Makefile                 |    3 +
 drivers/gpu/drm/i915/display/g4x_dp.c         |   28 +
 drivers/gpu/drm/i915/display/intel_atomic.c   |   10 +
 drivers/gpu/drm/i915/display/intel_ddi.c      |    9 +-
 drivers/gpu/drm/i915/display/intel_display.c  |   26 +-
 .../gpu/drm/i915/display/intel_display_core.h |    1 +
 .../drm/i915/display/intel_display_driver.c   |   20 +-
 .../drm/i915/display/intel_display_types.h    |    9 +
 drivers/gpu/drm/i915/display/intel_dp.c       |  309 ++-
 drivers/gpu/drm/i915/display/intel_dp.h       |   21 +-
 .../drm/i915/display/intel_dp_link_training.c |   33 +-
 .../drm/i915/display/intel_dp_link_training.h |    1 +
 drivers/gpu/drm/i915/display/intel_dp_mst.c   |   18 +-
 .../gpu/drm/i915/display/intel_dp_tunnel.c    |  642 ++++++
 .../gpu/drm/i915/display/intel_dp_tunnel.h    |  131 ++
 drivers/gpu/drm/i915/display/intel_link_bw.c  |    5 +
 drivers/gpu/drm/i915/display/intel_tc.c       |    4 +-
 include/drm/display/drm_dp.h                  |   61 +
 include/drm/display/drm_dp_helper.h           |    2 +
 include/drm/display/drm_dp_tunnel.h           |  270 +++
 26 files changed, 3292 insertions(+), 117 deletions(-)
 create mode 100644 drivers/gpu/drm/display/drm_dp_tunnel.c
 create mode 100644 drivers/gpu/drm/i915/display/intel_dp_tunnel.c
 create mode 100644 drivers/gpu/drm/i915/display/intel_dp_tunnel.h
 create mode 100644 include/drm/display/drm_dp_tunnel.h
Signature · 1 line
2.39.2
author
#1 lore raw

PATCH 1/19 drm/dp: Add drm_dp_max_dprx_data_rate()

Copy intel_dp_max_data_rate() to DRM core. It will be needed by a follow-up DP tunnel patch, checking the maximum rate the DPRX (sink) supports. Accordingly use the drm_dp_max_dprx_data_rate() name for clarity. This patchset will also switch calling the new DRM function in i915 instead of intel_dp_max_data_rate().

---
 drivers/gpu/drm/display/drm_dp_helper.c | 58 +++++++++++++++++++++++++
 include/drm/display/drm_dp_helper.h     |  2 +
 2 files changed, 60 insertions(+)
drivers/gpu/drm/display/drm_dp_helper.c +58 -0
index b1ca3a1100dab..24911243d4d3a 100644
@@ -4058,3 +4058,61 @@ int drm_dp_bw_channel_coding_efficiency(bool is_uhbr)
 		return 800000;
 }
 EXPORT_SYMBOL(drm_dp_bw_channel_coding_efficiency);
+
+/*
+ * Given a link rate and lanes, get the data bandwidth.
+ *
+ * Data bandwidth is the actual payload rate, which depends on the data
+ * bandwidth efficiency and the link rate.
+ *
+ * For 8b/10b channel encoding, SST and non-FEC, the data bandwidth efficiency
+ * is 80%. For example, for a 1.62 Gbps link, 1.62*10^9 bps * 0.80 * (1/8) =
+ * 162000 kBps. With 8-bit symbols, we have 162000 kHz symbol clock. Just by
+ * coincidence, the port clock in kHz matches the data bandwidth in kBps, and
+ * they equal the link bit rate in Gbps multiplied by 100000. (Note that this no
+ * longer holds for data bandwidth as soon as FEC or MST is taken into account!)
+ *
+ * For 128b/132b channel encoding, the data bandwidth efficiency is 96.71%. For
+ * example, for a 10 Gbps link, 10*10^9 bps * 0.9671 * (1/8) = 1208875
+ * kBps. With 32-bit symbols, we have 312500 kHz symbol clock. The value 1000000
+ * does not match the symbol clock, the port clock (not even if you think in
+ * terms of a byte clock), nor the data bandwidth. It only matches the link bit
+ * rate in units of 10000 bps.
+ *
+ * Note that protocol layers above the DPRX link level considered here can
+ * further limit the maximum data rate. Such layers are the MST topology (with
+ * limits on the link between the source and first branch device as well as on
+ * the whole MST path until the DPRX link) and (Thunderbolt) DP tunnels -
+ * which in turn can encapsulate an MST link with its own limit - with each
+ * SST or MST encapsulated tunnel sharing the BW of a tunnel group.
+ *
+ * TODO: Add support for querying the max data rate with the above limits as
+ * well.
+ *
+ * Returns the maximum data rate in kBps units.
+ */
+int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes)
+{
+	int ch_coding_efficiency =
+		drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_rate));
+	int max_link_rate_kbps = max_link_rate * 10;
+
+	/*
+	 * UHBR rates always use 128b/132b channel encoding, and have
+	 * 97.71% data bandwidth efficiency. Consider max_link_rate the
+	 * link bit rate in units of 10000 bps.
+	 */
+	/*
+	 * Lower than UHBR rates always use 8b/10b channel encoding, and have
+	 * 80% data bandwidth efficiency for SST non-FEC. However, this turns
+	 * out to be a nop by coincidence:
+	 *
+	 *	int max_link_rate_kbps = max_link_rate * 10;
+	 *	max_link_rate_kbps = DIV_ROUND_DOWN_ULL(max_link_rate_kbps * 8, 10);
+	 *	max_link_rate = max_link_rate_kbps / 8;
+	 */
+	return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate_kbps * max_lanes,
+					      ch_coding_efficiency),
+				  1000000 * 8);
+}
+EXPORT_SYMBOL(drm_dp_max_dprx_data_rate);
include/drm/display/drm_dp_helper.h +2 -0
index 863b2e7add29e..454ae7517419a 100644
@@ -813,4 +813,6 @@ int drm_dp_bw_overhead(int lane_count, int hactive,
 		       int bpp_x16, unsigned long flags);
 int drm_dp_bw_channel_coding_efficiency(bool is_uhbr);
 
+int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes);
+
 #endif /* _DRM_DP_HELPER_H_ */
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH 01/19] drm/dp: Add drm_dp_max_dprx_data_rate()

Quoted 48 lines · Imre Deak

Copy intel_dp_max_data_rate() to DRM core. It will be needed by a follow-up DP tunnel patch, checking the maximum rate the DPRX (sink) supports. Accordingly use the drm_dp_max_dprx_data_rate() name for clarity. This patchset will also switch calling the new DRM function in i915 instead of intel_dp_max_data_rate().

---
 drivers/gpu/drm/display/drm_dp_helper.c | 58 +++++++++++++++++++++++++
 include/drm/display/drm_dp_helper.h     |  2 +
 2 files changed, 60 insertions(+)
drivers/gpu/drm/display/drm_dp_helper.c +28 -0
index b1ca3a1100dab..24911243d4d3a 100644
@@ -4058,3 +4058,61 @@ int drm_dp_bw_channel_coding_efficiency(bool is_uhbr)
 		return 800000;
 }
 EXPORT_SYMBOL(drm_dp_bw_channel_coding_efficiency);
+
+/*
+ * Given a link rate and lanes, get the data bandwidth.
+ *
+ * Data bandwidth is the actual payload rate, which depends on the data
+ * bandwidth efficiency and the link rate.
+ *
+ * For 8b/10b channel encoding, SST and non-FEC, the data bandwidth efficiency
+ * is 80%. For example, for a 1.62 Gbps link, 1.62*10^9 bps * 0.80 * (1/8) =
+ * 162000 kBps. With 8-bit symbols, we have 162000 kHz symbol clock. Just by
+ * coincidence, the port clock in kHz matches the data bandwidth in kBps, and
+ * they equal the link bit rate in Gbps multiplied by 100000. (Note that this no
+ * longer holds for data bandwidth as soon as FEC or MST is taken into account!)
+ *
+ * For 128b/132b channel encoding, the data bandwidth efficiency is 96.71%. For
+ * example, for a 10 Gbps link, 10*10^9 bps * 0.9671 * (1/8) = 1208875
+ * kBps. With 32-bit symbols, we have 312500 kHz symbol clock. The value 1000000
+ * does not match the symbol clock, the port clock (not even if you think in
+ * terms of a byte clock), nor the data bandwidth. It only matches the link bit
+ * rate in units of 10000 bps.
+ *
+ * Note that protocol layers above the DPRX link level considered here can
+ * further limit the maximum data rate. Such layers are the MST topology (with
+ * limits on the link between the source and first branch device as well as on
+ * the whole MST path until the DPRX link) and (Thunderbolt) DP tunnels -
+ * which in turn can encapsulate an MST link with its own limit - with each
+ * SST or MST encapsulated tunnel sharing the BW of a tunnel group.
+ *

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool is_uhbr) · line 4098

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_helper.c +10 -0
+ * TODO: Add support for querying the max data rate with the above limits as
+ * well.
+ *
+ * Returns the maximum data rate in kBps units.
+ */
+int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes)
+{
+	int ch_coding_efficiency =
+		drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_rate));
+	int max_link_rate_kbps = max_link_rate * 10;

That x10 value seems rather pointless.

Quoted 5 lines
drivers/gpu/drm/display/drm_dp_helper.c +5 -0
+
+	/*
+	 * UHBR rates always use 128b/132b channel encoding, and have
+	 * 97.71% data bandwidth efficiency. Consider max_link_rate the
+	 * link bit rate in units of 10000 bps.

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool is_uhbr) · line 4113

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_helper.c +10 -0
+	 */
+	/*
+	 * Lower than UHBR rates always use 8b/10b channel encoding, and have
+	 * 80% data bandwidth efficiency for SST non-FEC. However, this turns
+	 * out to be a nop by coincidence:
+	 *
+	 *	int max_link_rate_kbps = max_link_rate * 10;
+	 *	max_link_rate_kbps = DIV_ROUND_DOWN_ULL(max_link_rate_kbps * 8, 10);
+	 *	max_link_rate = max_link_rate_kbps / 8;
+	 */

Not sure why we are repeating the nuts and bolts detils in the comments so much? Doesn't drm_dp_bw_channel_coding_efficiency() explain all this already?

Quoted 18 lines
drivers/gpu/drm/display/drm_dp_helper.c +5 -0
+	return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate_kbps * max_lanes,
+					      ch_coding_efficiency),
+				  1000000 * 8);
+}
+EXPORT_SYMBOL(drm_dp_max_dprx_data_rate);
include/drm/display/drm_dp_helper.h +2 -0
index 863b2e7add29e..454ae7517419a 100644
@@ -813,4 +813,6 @@ int drm_dp_bw_overhead(int lane_count, int hactive,
 		       int bpp_x16, unsigned long flags);
 int drm_dp_bw_channel_coding_efficiency(bool is_uhbr);
 
+int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes);
+
 #endif /* _DRM_DP_HELPER_H_ */
Signature · 1 line
2.39.2
Signature · 2 lines
Ville Syrjälä
Intel
author
#3 lore raw

Re: [PATCH 01/19] drm/dp: Add drm_dp_max_dprx_data_rate()

Quoted 53 lines · Ville Syrjälä
Quoted 52 lines · Imre Deak

Copy intel_dp_max_data_rate() to DRM core. It will be needed by a follow-up DP tunnel patch, checking the maximum rate the DPRX (sink) supports. Accordingly use the drm_dp_max_dprx_data_rate() name for clarity. This patchset will also switch calling the new DRM function in i915 instead of intel_dp_max_data_rate().

---
 drivers/gpu/drm/display/drm_dp_helper.c | 58 +++++++++++++++++++++++++
 include/drm/display/drm_dp_helper.h     |  2 +
 2 files changed, 60 insertions(+)
drivers/gpu/drm/display/drm_dp_helper.c +32 -0
index b1ca3a1100dab..24911243d4d3a 100644
@@ -4058,3 +4058,61 @@ int drm_dp_bw_channel_coding_efficiency(bool is_uhbr)
 		return 800000;
 }
 EXPORT_SYMBOL(drm_dp_bw_channel_coding_efficiency);
+
+/*
+ * Given a link rate and lanes, get the data bandwidth.
+ *
+ * Data bandwidth is the actual payload rate, which depends on the data
+ * bandwidth efficiency and the link rate.
+ *
+ * For 8b/10b channel encoding, SST and non-FEC, the data bandwidth efficiency
+ * is 80%. For example, for a 1.62 Gbps link, 1.62*10^9 bps * 0.80 * (1/8) =
+ * 162000 kBps. With 8-bit symbols, we have 162000 kHz symbol clock. Just by
+ * coincidence, the port clock in kHz matches the data bandwidth in kBps, and
+ * they equal the link bit rate in Gbps multiplied by 100000. (Note that this no
+ * longer holds for data bandwidth as soon as FEC or MST is taken into account!)
+ *
+ * For 128b/132b channel encoding, the data bandwidth efficiency is 96.71%. For
+ * example, for a 10 Gbps link, 10*10^9 bps * 0.9671 * (1/8) = 1208875
+ * kBps. With 32-bit symbols, we have 312500 kHz symbol clock. The value 1000000
+ * does not match the symbol clock, the port clock (not even if you think in
+ * terms of a byte clock), nor the data bandwidth. It only matches the link bit
+ * rate in units of 10000 bps.
+ *
+ * Note that protocol layers above the DPRX link level considered here can
+ * further limit the maximum data rate. Such layers are the MST topology (with
+ * limits on the link between the source and first branch device as well as on
+ * the whole MST path until the DPRX link) and (Thunderbolt) DP tunnels -
+ * which in turn can encapsulate an MST link with its own limit - with each
+ * SST or MST encapsulated tunnel sharing the BW of a tunnel group.
+ *
+ * TODO: Add support for querying the max data rate with the above limits as
+ * well.
+ *
+ * Returns the maximum data rate in kBps units.
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool is_uhbr) · line 4098

Quoted 6 lines
drivers/gpu/drm/display/drm_dp_helper.c +6 -0
+ */
+int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes)
+{
+	int ch_coding_efficiency =
+		drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_rate));
+	int max_link_rate_kbps = max_link_rate * 10;

That x10 value seems rather pointless.

I suppose the point was to make the units clearer, but it could be clarified instead in max_link_rates' documentation, which is missing atm.

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/display/drm_dp_helper.c +11 -0
+
+	/*
+	 * UHBR rates always use 128b/132b channel encoding, and have
+	 * 97.71% data bandwidth efficiency. Consider max_link_rate the
+	 * link bit rate in units of 10000 bps.
+	 */
+	/*
+	 * Lower than UHBR rates always use 8b/10b channel encoding, and have
+	 * 80% data bandwidth efficiency for SST non-FEC. However, this turns
+	 * out to be a nop by coincidence:
+	 *
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool is_uhbr) · line 4113

Quoted 4 lines
drivers/gpu/drm/display/drm_dp_helper.c +4 -0
+	 *	int max_link_rate_kbps = max_link_rate * 10;
+	 *	max_link_rate_kbps = DIV_ROUND_DOWN_ULL(max_link_rate_kbps * 8, 10);
+	 *	max_link_rate = max_link_rate_kbps / 8;
+	 */

Not sure why we are repeating the nuts and bolts detils in the comments so much? Doesn't drm_dp_bw_channel_coding_efficiency() explain all this already?

I simply copied the function, but yes in this context there is duplication, thanks for reading through all that. Will consolidate both the above and the bigger comment before the function with the existing docs here.

Quoted 23 lines
Quoted 19 lines
drivers/gpu/drm/display/drm_dp_helper.c +5 -0
+	return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate_kbps * max_lanes,
+					      ch_coding_efficiency),
+				  1000000 * 8);
+}
+EXPORT_SYMBOL(drm_dp_max_dprx_data_rate);
include/drm/display/drm_dp_helper.h +2 -0
index 863b2e7add29e..454ae7517419a 100644
@@ -813,4 +813,6 @@ int drm_dp_bw_overhead(int lane_count, int hactive,
 		       int bpp_x16, unsigned long flags);
 int drm_dp_bw_channel_coding_efficiency(bool is_uhbr);
 
+int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes);
+
 #endif /* _DRM_DP_HELPER_H_ */
Signature · 1 line
2.39.2
Signature · 2 lines
Ville Syrjälä
Intel
Reviewed-by
#4 lore raw

RE: [PATCH 01/19] drm/dp: Add drm_dp_max_dprx_data_rate()

Quoted 119 lines

-----Original Message-----
From: dri-devel <[email protected]> On Behalf Of Imre Deak
Sent: Friday, January 26, 2024 6:58 PM
To: Ville Syrjälä <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH 01/19] drm/dp: Add drm_dp_max_dprx_data_rate()

Quoted 10 lines · Ville Syrjälä
Quoted 9 lines · Imre Deak

Copy intel_dp_max_data_rate() to DRM core. It will be needed by a follow-up DP tunnel patch, checking the maximum rate the DPRX (sink) supports. Accordingly use the drm_dp_max_dprx_data_rate() name for clarity. This patchset will also switch calling the new DRM function in i915 instead of intel_dp_max_data_rate().

---
 drivers/gpu/drm/display/drm_dp_helper.c | 58

+++++++++++++++++++++++++

Quoted 9 lines

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool

Quoted 9 lines
 include/drm/display/drm_dp_helper.h     |  2 +
 2 files changed, 60 insertions(+)
drivers/gpu/drm/display/drm_dp_helper.c +0 -0
index b1ca3a1100dab..24911243d4d3a 100644
@@ -4058,3 +4058,61 @@ int drm_dp_bw_channel_coding_efficiency(bool

is_uhbr)

Quoted 49 lines
Quoted 49 lines
drivers/gpu/drm/display/drm_dp_helper.c +46 -0
 		return 800000;
 }
 EXPORT_SYMBOL(drm_dp_bw_channel_coding_efficiency);
+
+/*
+ * Given a link rate and lanes, get the data bandwidth.
+ *
+ * Data bandwidth is the actual payload rate, which depends on the
+data
+ * bandwidth efficiency and the link rate.
+ *
+ * For 8b/10b channel encoding, SST and non-FEC, the data bandwidth
+efficiency
+ * is 80%. For example, for a 1.62 Gbps link, 1.62*10^9 bps * 0.80
+* (1/8) =
+ * 162000 kBps. With 8-bit symbols, we have 162000 kHz symbol
+clock. Just by
+ * coincidence, the port clock in kHz matches the data bandwidth in
+kBps, and
+ * they equal the link bit rate in Gbps multiplied by 100000. (Note
+that this no
+ * longer holds for data bandwidth as soon as FEC or MST is taken
+into account!)
+ *
+ * For 128b/132b channel encoding, the data bandwidth efficiency is
+96.71%. For
+ * example, for a 10 Gbps link, 10*10^9 bps * 0.9671 * (1/8) =
+1208875
+ * kBps. With 32-bit symbols, we have 312500 kHz symbol clock. The
+value 1000000
+ * does not match the symbol clock, the port clock (not even if you
+think in
+ * terms of a byte clock), nor the data bandwidth. It only matches
+the link bit
+ * rate in units of 10000 bps.
+ *
+ * Note that protocol layers above the DPRX link level considered
+here can
+ * further limit the maximum data rate. Such layers are the MST
+topology (with
+ * limits on the link between the source and first branch device as
+well as on
+ * the whole MST path until the DPRX link) and (Thunderbolt) DP
+tunnels -
+ * which in turn can encapsulate an MST link with its own limit -
+with each
+ * SST or MST encapsulated tunnel sharing the BW of a tunnel group.
+ *
+ * TODO: Add support for querying the max data rate with the above
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool is_uhbr) · line 4099

Quoted 8 lines
drivers/gpu/drm/display/drm_dp_helper.c +8 -0
+limits as
+ * well.
+ *
+ * Returns the maximum data rate in kBps units.
+ */
+int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes) {
+	int ch_coding_efficiency =
+

drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_
rate));

Quoted 3 lines

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool is_uhbr) · line 4098

Quoted 1 line
drivers/gpu/drm/display/drm_dp_helper.c +1 -0
+	int max_link_rate_kbps = max_link_rate * 10;

That x10 value seems rather pointless.

I suppose the point was to make the units clearer, but it could be clarified instead in max_link_rates' documentation, which is missing atm.

Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/display/drm_dp_helper.c +5 -0
+
+	/*
+	 * UHBR rates always use 128b/132b channel encoding, and have
+	 * 97.71% data bandwidth efficiency. Consider max_link_rate the
+	 * link bit rate in units of 10000 bps.
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool is_uhbr)

Quoted 8 lines
drivers/gpu/drm/display/drm_dp_helper.c +8 -0
+	 */
+	/*
+	 * Lower than UHBR rates always use 8b/10b channel encoding, and have
+	 * 80% data bandwidth efficiency for SST non-FEC. However, this turns
+	 * out to be a nop by coincidence:
+	 *
+	 *	int max_link_rate_kbps = max_link_rate * 10;
+	 *	max_link_rate_kbps =

DIV_ROUND_DOWN_ULL(max_link_rate_kbps * 8, 10);

Quoted 6 lines

drivers/gpu/drm/display/drm_dp_helper.c int drm_dp_bw_channel_coding_efficiency(bool is_uhbr) · line 4113

Quoted 2 lines
drivers/gpu/drm/display/drm_dp_helper.c +2 -0
+	 *	max_link_rate = max_link_rate_kbps / 8;
+	 */

Not sure why we are repeating the nuts and bolts detils in the comments so much? Doesn't drm_dp_bw_channel_coding_efficiency() explain all this already?

Quoted 3 lines

I simply copied the function, but yes in this context there is duplication, thanks for reading through all that. Will consolidate both the above and the bigger comment before the function with the existing docs here.

Changes look good to me. With above addressed:

Quoted 25 lines
Quoted 2 lines

drivers/gpu/drm/display/drm_dp_helper.c

Quoted 2 lines
drivers/gpu/drm/display/drm_dp_helper.c +1 -0
+	return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate_kbps *

max_lanes,

Quoted 22 lines
Quoted 17 lines
drivers/gpu/drm/display/drm_dp_helper.c +4 -0
+					      ch_coding_efficiency),
+				  1000000 * 8);
+}
+EXPORT_SYMBOL(drm_dp_max_dprx_data_rate);
include/drm/display/drm_dp_helper.h +2 -1
index 863b2e7add29e..454ae7517419a 100644
@@ -813,4 +813,6 @@ int drm_dp_bw_overhead(int lane_count, int hactive,
 		       int bpp_x16, unsigned long flags);  int
 drm_dp_bw_channel_coding_efficiency(bool is_uhbr);
 
+int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes);
+
 #endif /* _DRM_DP_HELPER_H_ */
--
Quoted 1 line

2.39.2

--
Ville Syrjälä
Intel

author
#5 lore raw

PATCH 2/19 drm/dp: Add support for DP tunneling

In reply to Imre Deak

Add support for Display Port DP tunneling. For now this includes the support for Bandwidth Allocation Mode, leaving adding Panel Replay support for later.

BWA allows using displays that share the same (Thunderbolt) link with their maximum resolution. Atm, this may not be possible due to the coarse granularity of partitioning the link BW among the displays on the
link: the BW allocation policy is in a SW/FW/HW component on the link (on Thunderbolt it's the SW or FW Connection Manager), independent of the driver. This policy will set the DPRX maximum rate and lane count DPCD registers the GFX driver will see (0x00000, 0x00001, 0x02200, 0x02201) based on the available link BW.

The granularity of the current BW allocation policy is course, based on the required link rate in the 1.62Gbs..8.1Gbps range and it may prevent using higher resolutions all together: the display connected first will get a share of the link BW which corresponds to its full DPRX capability (regardless of the actual mode it uses). A subsequent display connected will only get the remaining BW, which could be well below its full capability.

BWA solves the above course granularity (reducing it to a 250Mbs..1Gps range) and first-come/first-served issues by letting the driver request the BW for each display on a link which reflects the actual modes the displays use.

This patch adds the DRM core helper functions, while a follow-up change in the patchset takes them into use in the i915 driver.

---
 drivers/gpu/drm/display/Kconfig         |   17 +
 drivers/gpu/drm/display/Makefile        |    2 +
 drivers/gpu/drm/display/drm_dp_tunnel.c | 1715 +++++++++++++++++++++++
 include/drm/display/drm_dp.h            |   60 +
 include/drm/display/drm_dp_tunnel.h     |  270 ++++
 5 files changed, 2064 insertions(+)
 create mode 100644 drivers/gpu/drm/display/drm_dp_tunnel.c
 create mode 100644 include/drm/display/drm_dp_tunnel.h
drivers/gpu/drm/display/Kconfig +17 -0
index 09712b88a5b83..b024a84b94c1c 100644
@@ -17,6 +17,23 @@ config DRM_DISPLAY_DP_HELPER
 	help
 	  DRM display helpers for DisplayPort.
 
+config DRM_DISPLAY_DP_TUNNEL
+	bool
+	select DRM_DISPLAY_DP_HELPER
+	help
+	  Enable support for DisplayPort tunnels.
+
+config DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	bool "Enable debugging the DP tunnel state"
+	depends on REF_TRACKER
+	depends on DRM_DISPLAY_DP_TUNNEL
+	depends on DEBUG_KERNEL
+	depends on EXPERT
+	help
+	  Enables debugging the DP tunnel manager's status.
+
+	  If in doubt, say "N".
+
 config DRM_DISPLAY_HDCP_HELPER
 	bool
 	depends on DRM_DISPLAY_HELPER
drivers/gpu/drm/display/Makefile +2 -0
index 17ac4a1006a80..7ca61333c6696 100644
@@ -8,6 +8,8 @@ drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_HELPER) += \
 	drm_dp_helper.o \
 	drm_dp_mst_topology.o \
 	drm_dsc_helper.o
+drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_TUNNEL) += \
+	drm_dp_tunnel.o
 drm_display_helper-$(CONFIG_DRM_DISPLAY_HDCP_HELPER) += drm_hdcp_helper.o
 drm_display_helper-$(CONFIG_DRM_DISPLAY_HDMI_HELPER) += \
 	drm_hdmi_helper.o \
drivers/gpu/drm/display/drm_dp_tunnel.c new file +1715 -0

1,715 lines not on this page · raw message

include/drm/display/drm_dp.h +60 -0

66 lines not on this page · raw message

include/drm/display/drm_dp_tunnel.h new file +270 -0

270 lines not on this page · raw message

Signature · 1 line
2.39.2
#6 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 270 lines · Imre Deak

Add support for Display Port DP tunneling. For now this includes the support for Bandwidth Allocation Mode, leaving adding Panel Replay support for later.

BWA allows using displays that share the same (Thunderbolt) link with their maximum resolution. Atm, this may not be possible due to the coarse granularity of partitioning the link BW among the displays on the
link: the BW allocation policy is in a SW/FW/HW component on the link (on Thunderbolt it's the SW or FW Connection Manager), independent of the driver. This policy will set the DPRX maximum rate and lane count DPCD registers the GFX driver will see (0x00000, 0x00001, 0x02200, 0x02201) based on the available link BW.

The granularity of the current BW allocation policy is course, based on
the required link rate in the 1.62Gbs..8.1Gbps range and it may prevent
using higher resolutions all together: the display connected first will
get a share of the link BW which corresponds to its full DPRX capability
(regardless of the actual mode it uses). A subsequent display connected
will only get the remaining BW, which could be well below its full capability.

BWA solves the above course granularity (reducing it to a 250Mbs..1Gps
range) and first-come/first-served issues by letting the driver request
the BW for each display on a link which reflects the actual modes the displays use.

This patch adds the DRM core helper functions, while a follow-up change
in the patchset takes them into use in the i915 driver.

---
 drivers/gpu/drm/display/Kconfig         |   17 + drivers/gpu/drm/display/Makefile        |    2 + drivers/gpu/drm/display/drm_dp_tunnel.c | 1715
+++++++++++++++++++++++
 include/drm/display/drm_dp.h            |   60 + include/drm/display/drm_dp_tunnel.h     |  270 ++++

 5 files changed, 2064 insertions(+)

 create mode 100644 drivers/gpu/drm/display/drm_dp_tunnel.c create mode 100644 include/drm/display/drm_dp_tunnel.h

drivers/gpu/drm/display/Kconfig +0 -0
index 09712b88a5b83..b024a84b94c1c 100644
@@ -17,6 +17,23 @@ config DRM_DISPLAY_DP_HELPER

        help
          DRM display helpers for DisplayPort.

drivers/gpu/drm/display/Kconfig +17 -0
+config DRM_DISPLAY_DP_TUNNEL
+       bool
+       select DRM_DISPLAY_DP_HELPER
+       help
+         Enable support for DisplayPort tunnels.
+
+config DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       bool "Enable debugging the DP tunnel state"
+       depends on REF_TRACKER
+       depends on DRM_DISPLAY_DP_TUNNEL
+       depends on DEBUG_KERNEL
+       depends on EXPERT
+       help
+         Enables debugging the DP tunnel manager's status.
+
+         If in doubt, say "N".
+

 config DRM_DISPLAY_HDCP_HELPER
        bool
        depends on DRM_DISPLAY_HELPER

drivers/gpu/drm/display/Makefile +0 -0
index 17ac4a1006a80..7ca61333c6696 100644
@@ -8,6 +8,8 @@ drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_HELPER) +=
 \
         drm_dp_helper.o \
         drm_dp_mst_topology.o \

        drm_dsc_helper.o +drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_TUNNEL) += \
+       drm_dp_tunnel.o drm_display_helper-$(CONFIG_DRM_DISPLAY_HDCP_HELPER) += drm_hdcp_helper.o drm_display_helper-$(CONFIG_DRM_DISPLAY_HDMI_HELPER) += \
        drm_hdmi_helper.o \

drivers/gpu/drm/display/drm_dp_tunnel.c new file +21 -0
new file mode 100644
index 0000000000000..58f6330db7d9d
@@ -0,0 +1,1715 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/ref_tracker.h>
+#include <linux/types.h>
+
+#include <drm/drm_atomic_state_helper.h>
+
+#include <drm/drm_atomic.h>
+#include <drm/drm_print.h>
+#include <drm/display/drm_dp.h>
+#include <drm/display/drm_dp_helper.h>
+#include <drm/display/drm_dp_tunnel.h>
+
+#define to_group(__private_obj) \
+       container_of(__private_obj, struct drm_dp_tunnel_group, base)
+
+#define to_group_state(__private_state) \
+       container_of(__private_state, struct

drm_dp_tunnel_group_state, base)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+
+#define is_dp_tunnel_private_obj(__obj) \
+       ((__obj)->funcs == &tunnel_group_funcs)
+
+#define for_each_new_group_in_state(__state, __new_group_state, __i)

\
+       for ((__i) = 0; \
+            (__i) < (__state)->num_private_objs; \
+            (__i)++) \
+               for_each_if ((__state)->private_objs[__i].ptr && \
+                            is_dp_tunnel_private_obj((__state)-

Quoted 1 line

private_objs[__i].ptr) && \

+                            ((__new_group_state) = \
+                               to_group_state((__state)-

Quoted 1 line

private_objs[__i].new_state), 1))

drivers/gpu/drm/display/drm_dp_tunnel.c new file +2 -0
+
+#define for_each_old_group_in_state(__state, __old_group_state, __i)

\
+       for ((__i) = 0; \
+            (__i) < (__state)->num_private_objs; \
+            (__i)++) \
+               for_each_if ((__state)->private_objs[__i].ptr && \
+                            is_dp_tunnel_private_obj((__state)-

Quoted 1 line

private_objs[__i].ptr) && \

+                            ((__old_group_state) = \
+                               to_group_state((__state)-

Quoted 1 line

private_objs[__i].old_state), 1))

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+#define for_each_tunnel_in_group(__group, __tunnel) \
+       list_for_each_entry(__tunnel, &(__group)->tunnels, node)
+
+#define for_each_tunnel_state(__group_state, __tunnel_state) \
+       list_for_each_entry(__tunnel_state, &(__group_state)-
Quoted 1 line

tunnel_states, node)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +2 -0
+
+#define for_each_tunnel_state_safe(__group_state, __tunnel_state,

__tunnel_state_tmp) \
+       list_for_each_entry_safe(__tunnel_state, __tunnel_state_tmp, \
+                                &(__group_state)->tunnel_states, node)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+
+#define kbytes_to_mbits(__kbytes) \
+       DIV_ROUND_UP((__kbytes) * 8, 1000)
+
+#define DPTUN_BW_ARG(__bw) ((__bw) < 0 ? (__bw) :

kbytes_to_mbits(__bw))

drivers/gpu/drm/display/drm_dp_tunnel.c new file +14 -0
+
+#define __tun_prn(__tunnel, __level, __type, __fmt, ...) \
+       drm_##__level##__type((__tunnel)->group->mgr->dev, \
+                             "[DPTUN %s][%s] " __fmt, \
+                             drm_dp_tunnel_name(__tunnel), \
+                             (__tunnel)->aux->name, ## \
+                             __VA_ARGS__)
+
+#define tun_dbg(__tunnel, __fmt, ...) \
+       __tun_prn(__tunnel, dbg, _kms, __fmt, ## __VA_ARGS__)
+
+#define tun_dbg_stat(__tunnel, __err, __fmt, ...) do { \
+       if (__err) \
+               __tun_prn(__tunnel, dbg, _kms, __fmt " (Failed, err:

%pe)\n", \
+                         ## __VA_ARGS__, ERR_PTR(__err)); \
+       else \
+               __tun_prn(__tunnel, dbg, _kms, __fmt " (Ok)\n", \
+                         ## __VA_ARGS__); \

drivers/gpu/drm/display/drm_dp_tunnel.c new file +24 -0
+} while (0)
+
+#define tun_dbg_atomic(__tunnel, __fmt, ...) \
+       __tun_prn(__tunnel, dbg, _atomic, __fmt, ## __VA_ARGS__)
+
+#define tun_grp_dbg(__group, __fmt, ...) \
+       drm_dbg_kms((__group)->mgr->dev, \
+                   "[DPTUN %s] " __fmt, \
+                   drm_dp_tunnel_group_name(__group), ## \
+                   __VA_ARGS__)
+
+#define DP_TUNNELING_BASE DP_TUNNELING_OUI
+
+#define __DPTUN_REG_RANGE(start, size) \
+       GENMASK_ULL(start + size - 1, start)
+
+#define DPTUN_REG_RANGE(addr, size) \
+       __DPTUN_REG_RANGE((addr) - DP_TUNNELING_BASE, size)
+
+#define DPTUN_REG(addr) DPTUN_REG_RANGE(addr, 1)
+
+#define DPTUN_INFO_REG_MASK ( \
+       DPTUN_REG_RANGE(DP_TUNNELING_OUI, DP_TUNNELING_OUI_BYTES) | \
+       DPTUN_REG_RANGE(DP_TUNNELING_DEV_ID,

DP_TUNNELING_DEV_ID_BYTES) | \
+       DPTUN_REG(DP_TUNNELING_HW_REV) | \

+       DPTUN_REG(DP_TUNNELING_SW_REV_MAJOR) | \
+       DPTUN_REG(DP_TUNNELING_SW_REV_MINOR) | \
+       DPTUN_REG(DP_TUNNELING_CAPABILITIES) | \

+       DPTUN_REG(DP_IN_ADAPTER_INFO) | \
+       DPTUN_REG(DP_USB4_DRIVER_ID) | \
+       DPTUN_REG(DP_USB4_DRIVER_BW_CAPABILITY) | \
+       DPTUN_REG(DP_IN_ADAPTER_TUNNEL_INFORMATION) | \
+       DPTUN_REG(DP_BW_GRANULARITY) | \
+       DPTUN_REG(DP_ESTIMATED_BW) | \
+       DPTUN_REG(DP_ALLOCATED_BW) | \
+       DPTUN_REG(DP_TUNNELING_MAX_LINK_RATE) | \
+       DPTUN_REG(DP_TUNNELING_MAX_LANE_COUNT) | \
+       DPTUN_REG(DP_DPTX_BW_ALLOCATION_MODE_CONTROL))

drivers/gpu/drm/display/drm_dp_tunnel.c new file +41 -0
+
+static const DECLARE_BITMAP(dptun_info_regs, 64) = {
+       DPTUN_INFO_REG_MASK & -1UL,
+#if BITS_PER_LONG == 32
+       DPTUN_INFO_REG_MASK >> 32,
+#endif
+};
+
+struct drm_dp_tunnel_regs {
+       u8 buf[HWEIGHT64(DPTUN_INFO_REG_MASK)];
+};
+
+struct drm_dp_tunnel_group;
+
+struct drm_dp_tunnel {
+       struct drm_dp_tunnel_group *group;
+
+       struct list_head node;
+
+       struct kref kref;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       struct ref_tracker *tracker;
+#endif
+       struct drm_dp_aux *aux;
+       char name[8];
+
+       int bw_granularity;
+       int estimated_bw;
+       int allocated_bw;
+
+       int max_dprx_rate;
+       u8 max_dprx_lane_count;
+
+       u8 adapter_id;
+
+       bool bw_alloc_supported:1;
+       bool bw_alloc_enabled:1;
+       bool has_io_error:1;
+       bool destroyed:1;
+};
+

drivers/gpu/drm/display/drm_dp_tunnel.c line 164

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+struct drm_dp_tunnel_group_state;
+
+struct drm_dp_tunnel_state {
+       struct drm_dp_tunnel_group_state *group_state;
+
+       struct drm_dp_tunnel_ref tunnel_ref;
+
+       struct list_head node;
+
+       u32 stream_mask;

I'm wondering if drm_dp_tunnel_state can really contain several streams and what kind of scenario this would be? From i915 point of view I would understand that several pipes are routed to DP tunnel. Is it bigjoiner case?

BR,

Jouni Högander

Quoted 2,122 lines

+       int *stream_bw;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+};
+
+struct drm_dp_tunnel_group_state {
+       struct drm_private_state base;
+
+       struct list_head tunnel_states;
+};
+
+struct drm_dp_tunnel_group {
+       struct drm_private_obj base;
+       struct drm_dp_tunnel_mgr *mgr;
+
+       struct list_head tunnels;
+
+       int available_bw;       /* available BW including the

allocated_bw of all tunnels */
+       int drv_group_id;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +23 -0
+
+       char name[8];
+
+       bool active:1;
+};
+
+struct drm_dp_tunnel_mgr {
+       struct drm_device *dev;
+
+       int group_count;
+       struct drm_dp_tunnel_group *groups;
+       wait_queue_head_t bw_req_queue;
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       struct ref_tracker_dir ref_tracker;
+#endif
+};
+
+static int next_reg_area(int *offset)
+{
+       *offset = find_next_bit(dptun_info_regs, 64, *offset);
+
+       return find_next_zero_bit(dptun_info_regs, 64, *offset + 1) -

*offset;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+}
+
+#define tunnel_reg_ptr(__regs, __address) ({ \
+       WARN_ON(!test_bit((__address) - DP_TUNNELING_BASE,

dptun_info_regs)); \
+       &(__regs)->buf[bitmap_weight(dptun_info_regs, (__address) - DP_TUNNELING_BASE)]; \

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+})
+
+static int read_tunnel_regs(struct drm_dp_aux *aux, struct

drm_dp_tunnel_regs *regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+{
+       int offset = 0;
+       int len;
+
+       while ((len = next_reg_area(&offset))) {
+               int address = DP_TUNNELING_BASE + offset;
+
+               if (drm_dp_dpcd_read(aux, address,

tunnel_reg_ptr(regs, address), len) < 0)
+                       return -EIO;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+               offset += len;
+       }
+
+       return 0;
+}
+
+static u8 tunnel_reg(const struct drm_dp_tunnel_regs *regs, int

address)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+{
+       return *tunnel_reg_ptr(regs, address);
+}
+
+static int tunnel_reg_drv_group_id(const struct drm_dp_tunnel_regs

*regs)
+{
+       int drv_id = tunnel_reg(regs, DP_USB4_DRIVER_ID) & DP_USB4_DRIVER_ID_MASK;
+       int group_id = tunnel_reg(regs, DP_IN_ADAPTER_TUNNEL_INFORMATION) & DP_GROUP_ID_MASK;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+
+       if (!group_id)
+               return 0;
+
+       return (drv_id << DP_GROUP_ID_BITS) | group_id;
+}
+
+/* Return granularity in kB/s units */
+static int tunnel_reg_bw_granularity(const struct drm_dp_tunnel_regs

*regs)
+{
+       int gr = tunnel_reg(regs, DP_BW_GRANULARITY) & DP_BW_GRANULARITY_MASK;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+       WARN_ON(gr > 2);
+
+       return (250000 << gr) / 8;
+}
+
+static int tunnel_reg_max_dprx_rate(const struct drm_dp_tunnel_regs

*regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+{
+       u8 bw_code = tunnel_reg(regs, DP_TUNNELING_MAX_LINK_RATE);
+
+       return drm_dp_bw_code_to_link_rate(bw_code);
+}
+
+static int tunnel_reg_max_dprx_lane_count(const struct

drm_dp_tunnel_regs *regs)
+{
+       u8 lane_count = tunnel_reg(regs, DP_TUNNELING_MAX_LANE_COUNT) &
+                       DP_TUNNELING_MAX_LANE_COUNT_MASK;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+
+       return lane_count;
+}
+
+static bool tunnel_reg_bw_alloc_supported(const struct

drm_dp_tunnel_regs *regs)
+{
+       u8 cap_mask = DP_TUNNELING_SUPPORT | DP_IN_BW_ALLOCATION_MODE_SUPPORT;
+
+       if ((tunnel_reg(regs, DP_TUNNELING_CAPABILITIES) & cap_mask) != cap_mask)
+               return false;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+       return tunnel_reg(regs, DP_USB4_DRIVER_BW_CAPABILITY) &
+              DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT;
+}
+
+static bool tunnel_reg_bw_alloc_enabled(const struct

drm_dp_tunnel_regs *regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +22 -0
+{
+       return tunnel_reg(regs, DP_DPTX_BW_ALLOCATION_MODE_CONTROL) &
+               DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE;
+}
+
+static int tunnel_group_drv_id(int drv_group_id)
+{
+       return drv_group_id >> DP_GROUP_ID_BITS;
+}
+
+static int tunnel_group_id(int drv_group_id)
+{
+       return drv_group_id & DP_GROUP_ID_MASK;
+}
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+       return tunnel->name;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_name);
+
+static const char *drm_dp_tunnel_group_name(const struct

drm_dp_tunnel_group *group)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+       return group->name;
+}
+
+static struct drm_dp_tunnel_group *
+lookup_or_alloc_group(struct drm_dp_tunnel_mgr *mgr, int

drv_group_id)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+{
+       struct drm_dp_tunnel_group *group = NULL;
+       int i;
+
+       for (i = 0; i < mgr->group_count; i++) {
+               /*
+                * A tunnel group with 0 group ID shouldn't have more

than one
+                * tunnels.
+                */
+               if (tunnel_group_id(drv_group_id) &&
+                   mgr->groups[i].drv_group_id == drv_group_id) +                       return &mgr->groups[i];

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+               if (!group && !mgr->groups[i].active)
+                       group = &mgr->groups[i];
+       }
+
+       if (!group) {
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: Can't allocate more tunnel

groups\n");
+               return NULL;
+       }

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+       group->drv_group_id = drv_group_id;
+       group->active = true;
+
+       snprintf(group->name, sizeof(group->name), "%d:%d:*",
+                tunnel_group_drv_id(drv_group_id) & ((1 <<

DP_GROUP_ID_BITS) - 1),
+                tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +27 -0
+
+       return group;
+}
+
+static void free_group(struct drm_dp_tunnel_group *group)
+{
+       struct drm_dp_tunnel_mgr *mgr = group->mgr;
+
+       if (drm_WARN_ON(mgr->dev, !list_empty(&group->tunnels)))
+               return;
+
+       group->drv_group_id = 0;
+       group->available_bw = -1;
+       group->active = false;
+}
+
+static struct drm_dp_tunnel *
+tunnel_get(struct drm_dp_tunnel *tunnel)
+{
+       kref_get(&tunnel->kref);
+
+       return tunnel;
+}
+
+static void free_tunnel(struct kref *kref)
+{
+       struct drm_dp_tunnel *tunnel = container_of(kref,

typeof(*tunnel), kref);
+       struct drm_dp_tunnel_group *group = tunnel->group;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +114 -0
+
+       list_del(&tunnel->node);
+       if (list_empty(&group->tunnels))
+               free_group(group);
+
+       kfree(tunnel);
+}
+
+static void tunnel_put(struct drm_dp_tunnel *tunnel)
+{
+       kref_put(&tunnel->kref, free_tunnel);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void track_tunnel_ref(struct drm_dp_tunnel *tunnel,
+                            struct ref_tracker **tracker)
+{
+       ref_tracker_alloc(&tunnel->group->mgr->ref_tracker,
+                         tracker, GFP_KERNEL);
+}
+
+static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
+                              struct ref_tracker **tracker)
+{
+       ref_tracker_free(&tunnel->group->mgr->ref_tracker,
+                        tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+       track_tunnel_ref(tunnel, NULL);
+
+       return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);
+
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel)
+{
+       tunnel_put(tunnel);
+       untrack_tunnel_ref(tunnel, NULL);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put_untracked);
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel,
+                   struct ref_tracker **tracker)
+{
+       track_tunnel_ref(tunnel, tracker);
+
+       return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get);
+
+void drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel,
+                        struct ref_tracker **tracker)
+{
+       untrack_tunnel_ref(tunnel, tracker);
+       tunnel_put(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put);
+#else
+#define track_tunnel_ref(tunnel, tracker) do {} while (0)
+#define untrack_tunnel_ref(tunnel, tracker) do {} while (0)
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+       return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);
+
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel)
+{
+       tunnel_put(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put_untracked);
+#endif
+
+static bool add_tunnel_to_group(struct drm_dp_tunnel_mgr *mgr,
+                               int drv_group_id,
+                               struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_group *group =
+               lookup_or_alloc_group(mgr, drv_group_id);
+
+       if (!group)
+               return false;
+
+       tunnel->group = group;
+       list_add(&tunnel->node, &group->tunnels);
+
+       return true;
+}
+
+static struct drm_dp_tunnel *
+create_tunnel(struct drm_dp_tunnel_mgr *mgr,
+             struct drm_dp_aux *aux,
+             const struct drm_dp_tunnel_regs *regs)
+{
+       int drv_group_id = tunnel_reg_drv_group_id(regs);
+       struct drm_dp_tunnel *tunnel;
+
+       tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
+       if (!tunnel)
+               return NULL;
+
+       INIT_LIST_HEAD(&tunnel->node);
+
+       kref_init(&tunnel->kref);
+
+       tunnel->aux = aux;
+
+       tunnel->adapter_id = tunnel_reg(regs, DP_IN_ADAPTER_INFO) &

DP_IN_ADAPTER_NUMBER_MASK;
+
+       snprintf(tunnel->name, sizeof(tunnel->name), "%d:%d:%d", +                tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
+                tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1),
+                tunnel->adapter_id & ((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+       tunnel->bw_granularity = tunnel_reg_bw_granularity(regs);
+       tunnel->allocated_bw = tunnel_reg(regs, DP_ALLOCATED_BW) *
+                              tunnel->bw_granularity;
+
+       tunnel->bw_alloc_supported =

tunnel_reg_bw_alloc_supported(regs);
+       tunnel->bw_alloc_enabled = tunnel_reg_bw_alloc_enabled(regs);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +34 -0
+
+       if (!add_tunnel_to_group(mgr, drv_group_id, tunnel)) {
+               kfree(tunnel);
+
+               return NULL;
+       }
+
+       track_tunnel_ref(tunnel, &tunnel->tracker);
+
+       return tunnel;
+}
+
+static void destroy_tunnel(struct drm_dp_tunnel *tunnel)
+{
+       untrack_tunnel_ref(tunnel, &tunnel->tracker);
+       tunnel_put(tunnel);
+}
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel)
+{
+       tunnel->has_io_error = true;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_set_io_error);
+
+static char yes_no_chr(int val)
+{
+       return val ? 'Y' : 'N';
+}
+
+#define SKIP_DPRX_CAPS_CHECK           BIT(0)
+#define ALLOW_ALLOCATED_BW_CHANGE      BIT(1)
+
+static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr,
+                                 const struct drm_dp_tunnel_regs

*regs,
+                                 unsigned int flags)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+{
+       int drv_group_id = tunnel_reg_drv_group_id(regs);
+       bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK);
+       bool ret = true;
+
+       if (!tunnel_reg_bw_alloc_supported(regs)) {
+               if (tunnel_group_id(drv_group_id)) {
+                       drm_dbg_kms(mgr->dev,
+                                   "DPTUN: A non-zero group ID is

only allowed with BWA support\n");
+                       ret = false;
+               }
+
+               if (tunnel_reg(regs, DP_ALLOCATED_BW)) { +                       drm_dbg_kms(mgr->dev,
+                                   "DPTUN: BW is allocated without BWA support\n");
+                       ret = false;
+               }

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+               return ret;
+       }
+
+       if (!tunnel_group_id(drv_group_id)) {
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: BWA support requires a non-zero

group ID\n");
+               ret = false;
+       }
+
+       if (check_dprx && hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) {
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: Invalid DPRX lane count: %d\n", +                           tunnel_reg_max_dprx_lane_count(regs));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+
+               ret = false;
+       }
+
+       if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) {
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: DPRX rate is 0\n");
+
+               ret = false;
+       }
+
+       if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs,

DP_ESTIMATED_BW)) {
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: Allocated BW %d > estimated BW %d Mb/s\n",
+                           DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) *
+
tunnel_reg_bw_granularity(regs)),
+                           DPTUN_BW_ARG(tunnel_reg(regs, DP_ESTIMATED_BW) *
+
tunnel_reg_bw_granularity(regs)));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+               ret = false;
+       }
+
+       return ret;
+}
+
+static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel

*tunnel,
+                                         const struct drm_dp_tunnel_regs *regs,
+                                         unsigned int flags)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+{
+       int new_drv_group_id = tunnel_reg_drv_group_id(regs);
+       bool ret = true;
+
+       if (tunnel->bw_alloc_supported !=

tunnel_reg_bw_alloc_supported(regs)) {
+               tun_dbg(tunnel,
+                       "BW alloc support has changed %c -> %c\n", +                       yes_no_chr(tunnel->bw_alloc_supported), +                       yes_no_chr(tunnel_reg_bw_alloc_supported(regs )));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+               ret = false;
+       }
+
+       if (tunnel->group->drv_group_id != new_drv_group_id) {
+               tun_dbg(tunnel,
+                       "Driver/group ID has changed %d:%d:* ->

%d:%d:*\n",
+                       tunnel_group_drv_id(tunnel->group-

Quoted 1 line

drv_group_id),

+                       tunnel_group_id(tunnel->group->drv_group_id), +                       tunnel_group_drv_id(new_drv_group_id), +                       tunnel_group_id(new_drv_group_id));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+               ret = false;
+       }
+
+       if (!tunnel->bw_alloc_supported)
+               return ret;
+
+       if (tunnel->bw_granularity !=

tunnel_reg_bw_granularity(regs)) {
+               tun_dbg(tunnel,
+                       "BW granularity has changed: %d -> %d Mb/s\n",
+                       DPTUN_BW_ARG(tunnel->bw_granularity), +                       DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs)) );

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+               ret = false;
+       }
+
+       /*
+        * On some devices at least the BW alloc mode enabled status

is always
+        * reported as 0, so skip checking that here. +        */
+
+       if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) && +           tunnel->allocated_bw !=
+           tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel-

Quoted 1 line

bw_granularity) {

+               tun_dbg(tunnel,
+                       "Allocated BW has changed: %d -> %d Mb/s\n", +                       DPTUN_BW_ARG(tunnel->allocated_bw), +                       DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +31 -0
+
+               ret = false;
+       }
+
+       return ret;
+}
+
+static int
+read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel,
+                           struct drm_dp_tunnel_regs *regs,
+                           unsigned int flags)
+{
+       int err;
+
+       err = read_tunnel_regs(tunnel->aux, regs);
+       if (err < 0) {
+               drm_dp_tunnel_set_io_error(tunnel);
+
+               return err;
+       }
+
+       if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags))
+               return -EINVAL;
+
+       if (!tunnel_info_changes_are_valid(tunnel, regs, flags))
+               return -EINVAL;
+
+       return 0;
+}
+
+static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const

struct drm_dp_tunnel_regs *regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+{
+       bool changed = false;
+
+       if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate)

{
+               tunnel->max_dprx_rate = tunnel_reg_max_dprx_rate(regs);
+               changed = true;
+       }
+
+       if (tunnel_reg_max_dprx_lane_count(regs) != tunnel-

Quoted 1 line

max_dprx_lane_count) {

+               tunnel->max_dprx_lane_count =
tunnel_reg_max_dprx_lane_count(regs);
+               changed = true;
+       }

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+
+       return changed;
+}
+
+static int dev_id_len(const u8 *dev_id, int max_len)
+{
+       while (max_len && dev_id[max_len - 1] == '\0')
+               max_len--;
+
+       return max_len;
+}
+
+static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel)
+{
+       int bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate,
+                                          tunnel-
Quoted 1 line

max_dprx_lane_count);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+       return min(roundup(bw, tunnel->bw_granularity),
+                  MAX_DP_REQUEST_BW * tunnel->bw_granularity);
+}
+
+static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel)
+{
+       return min(get_max_dprx_bw(tunnel), tunnel->group-
Quoted 1 line

available_bw);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+}
+
+/**
+ * drm_dp_tunnel_detect - Detect DP tunnel on the link
+ * @mgr: Tunnel manager
+ * @aux: DP AUX on which the tunnel will be detected
+ *
+ * Detect if there is any DP tunnel on the link and add it to the

tunnel
+ * group's tunnel list.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +17 -0
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+                      struct drm_dp_aux *aux)
+{
+       struct drm_dp_tunnel_regs regs;
+       struct drm_dp_tunnel *tunnel;
+       int err;
+
+       err = read_tunnel_regs(aux, &regs);
+       if (err)
+               return ERR_PTR(err);
+
+       if (!(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+             DP_TUNNELING_SUPPORT))
+               return ERR_PTR(-ENODEV);
+
+       /* The DPRX caps are valid only after enabling BW alloc mode.

*/
+       if (!tunnel_regs_are_valid(mgr, &regs, SKIP_DPRX_CAPS_CHECK)) +               return ERR_PTR(-EINVAL);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+       tunnel = create_tunnel(mgr, aux, &regs);
+       if (!tunnel)
+               return ERR_PTR(-ENOMEM);
+
+       tun_dbg(tunnel,
+               "OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%c

BWA-Sup:%c BWA-En:%c\n",
+               DP_TUNNELING_OUI_BYTES,
+                       tunnel_reg_ptr(&regs, DP_TUNNELING_OUI), +               dev_id_len(tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES),
+                       tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID), +               (tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MAJOR_MASK) >>
+                       DP_TUNNELING_HW_REV_MAJOR_SHIFT, +               (tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MINOR_MASK) >>
+                       DP_TUNNELING_HW_REV_MINOR_SHIFT, +               tunnel_reg(&regs, DP_TUNNELING_SW_REV_MAJOR), +               tunnel_reg(&regs, DP_TUNNELING_SW_REV_MINOR), +               yes_no_chr(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+                          DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT), +               yes_no_chr(tunnel->bw_alloc_supported), +               yes_no_chr(tunnel->bw_alloc_enabled));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +51 -0
+
+       return tunnel;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_detect);
+
+/**
+ * drm_dp_tunnel_destroy - Destroy tunnel object
+ * @tunnel: Tunnel object
+ *
+ * Remove the tunnel from the tunnel topology and destroy it.
+ */
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+       if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed))
+               return -ENODEV;
+
+       tun_dbg(tunnel, "destroying\n");
+
+       tunnel->destroyed = true;
+       destroy_tunnel(tunnel);
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_destroy);
+
+static int check_tunnel(const struct drm_dp_tunnel *tunnel)
+{
+       if (tunnel->destroyed)
+               return -ENODEV;
+
+       if (tunnel->has_io_error)
+               return -EIO;
+
+       return 0;
+}
+
+static int group_allocated_bw(struct drm_dp_tunnel_group *group)
+{
+       struct drm_dp_tunnel *tunnel;
+       int group_allocated_bw = 0;
+
+       for_each_tunnel_in_group(group, tunnel) {
+               if (check_tunnel(tunnel) == 0 &&
+                   tunnel->bw_alloc_enabled)
+                       group_allocated_bw += tunnel->allocated_bw;
+       }
+
+       return group_allocated_bw;
+}
+
+static int calc_group_available_bw(const struct drm_dp_tunnel

*tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+{
+       return group_allocated_bw(tunnel->group) -
+              tunnel->allocated_bw +
+              tunnel->estimated_bw;
+}
+
+static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
+                                    const struct drm_dp_tunnel_regs

*regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+       struct drm_dp_tunnel *tunnel_iter;
+       int group_available_bw;
+       bool changed;
+
+       tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) *

tunnel->bw_granularity;
+
+       if (calc_group_available_bw(tunnel) == tunnel->group-

Quoted 1 line

available_bw)

+               return 0;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+
+       for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
+               int err;
+
+               if (tunnel_iter == tunnel)
+                       continue;
+
+               if (check_tunnel(tunnel_iter) != 0 ||
+                   !tunnel_iter->bw_alloc_enabled)
+                       continue;
+
+               err = drm_dp_dpcd_probe(tunnel_iter->aux,

DP_DPCD_REV);
+               if (err) {
+                       tun_dbg(tunnel_iter,
+                               "Probe failed, assume disconnected (err %pe)\n",
+                               ERR_PTR(err));
+                       drm_dp_tunnel_set_io_error(tunnel_iter); +               }
+       }

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+
+       group_available_bw = calc_group_available_bw(tunnel);
+
+       tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
+               DPTUN_BW_ARG(tunnel->group->available_bw),
+               DPTUN_BW_ARG(group_available_bw));
+
+       changed = tunnel->group->available_bw != group_available_bw;
+
+       tunnel->group->available_bw = group_available_bw;
+
+       return changed ? 1 : 0;
+}
+
+static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool

enable)
+{
+       u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ;
+       u8 val;
+
+       if (drm_dp_dpcd_readb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)
+               goto out_err;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+       if (enable)
+               val |= mask;
+       else
+               val &= ~mask;
+
+       if (drm_dp_dpcd_writeb(tunnel->aux,

DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)
+               goto out_err;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +13 -0
+
+       tunnel->bw_alloc_enabled = enable;
+
+       return 0;
+
+out_err:
+       drm_dp_tunnel_set_io_error(tunnel);
+
+       return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_enable_bw_alloc: Enable DP tunnel BW allocation

mode
+ * @tunnel: Tunnel object
+ *
+ * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +36 -0
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_regs regs;
+       int err = check_tunnel(tunnel);
+
+       if (err)
+               return err;
+
+       if (!tunnel->bw_alloc_supported)
+               return -EOPNOTSUPP;
+
+       if (!tunnel_group_id(tunnel->group->drv_group_id))
+               return -EINVAL;
+
+       err = set_bw_alloc_mode(tunnel, true);
+       if (err)
+               goto out;
+
+       err = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+       if (err) {
+               set_bw_alloc_mode(tunnel, false);
+
+               goto out;
+       }
+
+       if (!tunnel->max_dprx_rate)
+               update_dprx_caps(tunnel, &regs);
+
+       if (tunnel->group->available_bw == -1) {
+               err = update_group_available_bw(tunnel, &regs);
+               if (err > 0)
+                       err = 0;
+       }
+out:
+       tun_dbg_stat(tunnel, err,
+                    "Enabling BW alloc mode: DPRX:%dx%d Group

alloc:%d/%d Mb/s",
+                    tunnel->max_dprx_rate / 100, tunnel-

Quoted 1 line

max_dprx_lane_count,

+                    DPTUN_BW_ARG(group_allocated_bw(tunnel->group)), +                    DPTUN_BW_ARG(tunnel->group->available_bw));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+       return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
+
+/**
+ * drm_dp_tunnel_disable_bw_alloc: Disable DP tunnel BW allocation

mode
+ * @tunnel: Tunnel object
+ *
+ * Disable the DP tunnel BW allocation mode on @tunnel.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+       int err = check_tunnel(tunnel);
+
+       if (err)
+               return err;
+
+       err = set_bw_alloc_mode(tunnel, false);
+
+       tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
+
+       return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
+
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel

*tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+       return tunnel->bw_alloc_enabled;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
+
+static int bw_req_complete(struct drm_dp_aux *aux, bool

*status_changed)
+{
+       u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
+       u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+       u8 val;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +32 -0
+
+       if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+               return -EIO;
+
+       *status_changed = val & status_change_mask;
+
+       val &= bw_req_mask;
+
+       if (!val)
+               return -EAGAIN;
+
+       if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, val) < 0)
+               return -EIO;
+
+       return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
+}
+
+static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+       struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
+       int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
+       unsigned long wait_expires;
+       DEFINE_WAIT(wait);
+       int err;
+
+       /* Atomic check should prevent the following. */
+       if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
+               err = -EINVAL;
+               goto out;
+       }
+
+       if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW,

request_bw) < 0) {
+               err = -EIO;
+               goto out;
+       }

drivers/gpu/drm/display/drm_dp_tunnel.c new file +14 -0
+
+       wait_expires = jiffies + msecs_to_jiffies(3000);
+
+       for (;;) {
+               bool status_changed;
+
+               err = bw_req_complete(tunnel->aux, &status_changed);
+               if (err != -EAGAIN)
+                       break;
+
+               if (status_changed) {
+                       struct drm_dp_tunnel_regs regs;
+
+                       err = read_and_verify_tunnel_regs(tunnel,

&regs,
+
ALLOW_ALLOCATED_BW_CHANGE);
+                       if (err)
+                               break;
+               }

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+               if (time_after(jiffies, wait_expires)) {
+                       err = -ETIMEDOUT;
+                       break;
+               }
+
+               prepare_to_wait(&mgr->bw_req_queue, &wait,

TASK_UNINTERRUPTIBLE);
+               schedule_timeout(msecs_to_jiffies(200)); +       };

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+
+       finish_wait(&mgr->bw_req_queue, &wait);
+
+       if (err)
+               goto out;
+
+       tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
+
+out:
+       tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel:

Group alloc:%d/%d Mb/s",
+                    DPTUN_BW_ARG(request_bw * tunnel-

Quoted 1 line

bw_granularity),

+                    DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)), +                    DPTUN_BW_ARG(group_allocated_bw(tunnel->group)), +                    DPTUN_BW_ARG(tunnel->group->available_bw));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +19 -0
+
+       if (err == -EIO)
+               drm_dp_tunnel_set_io_error(tunnel);
+
+       return err;
+}
+
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+       int err = check_tunnel(tunnel);
+
+       if (err)
+               return err;
+
+       return allocate_tunnel_bw(tunnel, bw);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
+
+static int check_and_clear_status_change(struct drm_dp_tunnel

*tunnel)
+{
+       u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+       u8 val;
+
+       if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0)
+               goto out_err;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+
+       val &= mask;
+
+       if (val) {
+               if (drm_dp_dpcd_writeb(tunnel->aux,

DP_TUNNELING_STATUS, val) < 0)
+                       goto out_err;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+
+               return 1;
+       }
+
+       if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
+               return 0;
+
+       /*
+        * Check for estimated BW changes explicitly to account for

lost
+        * BW change notifications.
+        */
+       if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) <
0)
+               goto out_err;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +14 -0
+
+       if (val * tunnel->bw_granularity != tunnel->estimated_bw)
+               return 1;
+
+       return 0;
+
+out_err:
+       drm_dp_tunnel_set_io_error(tunnel);
+
+       return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_update_state: Update DP tunnel SW state with the HW

state
+ * @tunnel: Tunnel object
+ *
+ * Update the SW state of @tunnel with the HW state.
+ *
+ * Returns 0 if the state has not changed, 1 if it has changed and got updated
+ * successfully and a negative error code otherwise.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +30 -0
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_regs regs;
+       bool changed = false;
+       int ret = check_tunnel(tunnel);
+
+       if (ret < 0)
+               return ret;
+
+       ret = check_and_clear_status_change(tunnel);
+       if (ret < 0)
+               goto out;
+
+       if (!ret)
+               return 0;
+
+       ret = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+       if (ret)
+               goto out;
+
+       if (update_dprx_caps(tunnel, &regs))
+               changed = true;
+
+       ret = update_group_available_bw(tunnel, &regs);
+       if (ret == 1)
+               changed = true;
+
+out:
+       tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
+                    "State update: Changed:%c DPRX:%dx%d Tunnel

alloc:%d/%d Group alloc:%d/%d Mb/s",
+                    yes_no_chr(changed),
+                    tunnel->max_dprx_rate / 100, tunnel-

Quoted 1 line

max_dprx_lane_count,

+                    DPTUN_BW_ARG(tunnel->allocated_bw),
+                    DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)), +                    DPTUN_BW_ARG(group_allocated_bw(tunnel->group)), +                    DPTUN_BW_ARG(tunnel->group->available_bw));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+
+       if (ret < 0)
+               return ret;
+
+       if (changed)
+               return 1;
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_update_state);
+
+/*
+ * Returns 0 if no re-probe is needed, 1 if a re-probe is needed,
+ * a negative error code otherwise.
+ */
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct

drm_dp_aux *aux)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+{
+       u8 val;
+
+       if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+               return -EIO;
+
+       if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
+               wake_up_all(&mgr->bw_req_queue);
+
+       if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED |

DP_ESTIMATED_BW_CHANGED))
+               return 1;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
+
+/**
+ * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the

tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum link rate of the DPRX connected
+ * to @tunnel. Note that this rate will not be limited by the BW limit of the
+ * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
+ * registers.
+ *
+ * Returns the maximum link rate in 10 kbit/s units.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+       return tunnel->max_dprx_rate;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
+
+/**
+ * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count

of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum lane count of the DPRX connected
+ * to @tunnel. Note that this lane count will not be limited by the BW limit of
+ * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
+ * registers.
+ *
+ * Returns the maximum lane count.
+ */
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+{
+       return tunnel->max_dprx_lane_count;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
+
+/**
+ * drm_dp_tunnel_available_bw - Query the estimated total available

BW of the tunnel
+ * @tunnel: Tunnel object
+ *
+ * This function is used to query the estimated total available BW of the
+ * tunnel. This includes the currently allocated and free BW for all the
+ * tunnels in @tunnel's group. The available BW is valid only after the BW
+ * allocation mode has been enabled for the tunnel and its state got updated
+ * calling drm_dp_tunnel_update_state().
+ *
+ * Returns the @tunnel group's estimated total available bandwidth in kB/s
+ * units, or -1 if the available BW isn't valid (the BW allocation mode is
+ * not enabled or the tunnel's state hasn't been updated).
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+       return tunnel->group->available_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
+
+static struct drm_dp_tunnel_group_state *
+drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
+                                    const struct drm_dp_tunnel

*tunnel)
+{
+       return (struct drm_dp_tunnel_group_state *)
+               drm_atomic_get_private_obj_state(state, +                                                &tunnel->group-

Quoted 1 line

base);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+}
+
+static struct drm_dp_tunnel_state *
+add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+                struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       tun_dbg_atomic(tunnel,
+                      "Adding state for tunnel %p to group state

%p\n",
+                      tunnel, group_state);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+
+       tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
+       if (!tunnel_state)
+               return NULL;
+
+       tunnel_state->group_state = group_state;
+
+       drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
+
+       INIT_LIST_HEAD(&tunnel_state->node);
+       list_add(&tunnel_state->node, &group_state->tunnel_states);
+
+       return tunnel_state;
+}
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state

*tunnel_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+{
+       tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
+                      "Clearing state for tunnel %p\n",
+                      tunnel_state->tunnel_ref.tunnel);
+
+       list_del(&tunnel_state->node);
+
+       kfree(tunnel_state->stream_bw);
+       drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
+
+       kfree(tunnel_state);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_clear_state);
+
+static void clear_tunnel_group_state(struct

drm_dp_tunnel_group_state *group_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+{
+       struct drm_dp_tunnel_state *tunnel_state;
+       struct drm_dp_tunnel_state *tunnel_state_tmp;
+
+       for_each_tunnel_state_safe(group_state, tunnel_state,

tunnel_state_tmp)
+               drm_dp_tunnel_atomic_clear_state(tunnel_state);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +17 -0
+}
+
+static struct drm_dp_tunnel_state *
+get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+                const struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       for_each_tunnel_state(group_state, tunnel_state)
+               if (tunnel_state->tunnel_ref.tunnel == tunnel)
+                       return tunnel_state;
+
+       return NULL;
+}
+
+static struct drm_dp_tunnel_state *
+get_or_add_tunnel_state(struct drm_dp_tunnel_group_state

*group_state,
+                       struct drm_dp_tunnel *tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +14 -0
+{
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       tunnel_state = get_tunnel_state(group_state, tunnel);
+       if (tunnel_state)
+               return tunnel_state;
+
+       return add_tunnel_state(group_state, tunnel);
+}
+
+static struct drm_private_state *
+tunnel_group_duplicate_state(struct drm_private_obj *obj)
+{
+       struct drm_dp_tunnel_group_state *group_state =

to_group_state(obj->state);
+       struct drm_dp_tunnel_state *tunnel_state;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+       group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+       if (!group_state)
+               return NULL;
+
+       INIT_LIST_HEAD(&group_state->tunnel_states);
+
+       __drm_atomic_helper_private_obj_duplicate_state(obj,

&group_state->base);
+
+       for_each_tunnel_state(to_group_state(obj->state), tunnel_state) {
+               struct drm_dp_tunnel_state *new_tunnel_state; +
+               new_tunnel_state = get_or_add_tunnel_state(group_state,
+
tunnel_state->tunnel_ref.tunnel);
+               if (!new_tunnel_state)
+                       goto out_free_state;
+
+               new_tunnel_state->stream_mask = tunnel_state-

Quoted 1 line

stream_mask;

+               new_tunnel_state->stream_bw = kmemdup(tunnel_state-

Quoted 1 line

stream_bw,

+
sizeof(*tunnel_state->stream_bw) *
+                                                       hweight32(tun nel_state->stream_mask),
+                                                     GFP_KERNEL);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+
+               if (!new_tunnel_state->stream_bw)
+                       goto out_free_state;
+       }
+
+       return &group_state->base;
+
+out_free_state:
+       clear_tunnel_group_state(group_state);
+       kfree(group_state);
+
+       return NULL;
+}
+
+static void tunnel_group_destroy_state(struct drm_private_obj *obj,

struct drm_private_state *state)
+{
+       struct drm_dp_tunnel_group_state *group_state = to_group_state(state);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +32 -0
+
+       clear_tunnel_group_state(group_state);
+       kfree(group_state);
+}
+
+static const struct drm_private_state_funcs tunnel_group_funcs = {
+       .atomic_duplicate_state = tunnel_group_duplicate_state,
+       .atomic_destroy_state = tunnel_group_destroy_state,
+};
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+                              struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_group_state *group_state =
+               drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       if (IS_ERR(group_state))
+               return ERR_CAST(group_state);
+
+       tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
+       if (!tunnel_state)
+               return ERR_PTR(-ENOMEM);
+
+       return tunnel_state;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+                                  const struct drm_dp_tunnel

*tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+       struct drm_dp_tunnel_group_state *new_group_state;
+       int i;
+
+       for_each_new_group_in_state(state, new_group_state, i)
+               if (to_group(new_group_state->base.obj) == tunnel-
Quoted 1 line

group)

+                       return get_tunnel_state(new_group_state, tunnel);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+       return NULL;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
+
+static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct

drm_dp_tunnel_group *group)
+{
+       struct drm_dp_tunnel_group_state *group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+
+       if (!group_state)
+               return false;
+
+       INIT_LIST_HEAD(&group_state->tunnel_states);
+
+       group->mgr = mgr;
+       group->available_bw = -1;
+       INIT_LIST_HEAD(&group->tunnels);
+
+       drm_atomic_private_obj_init(mgr->dev, &group->base,

&group_state->base,
+                                   &tunnel_group_funcs);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+
+       return true;
+}
+
+static void cleanup_group(struct drm_dp_tunnel_group *group)
+{
+       drm_atomic_private_obj_fini(&group->base);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void check_unique_stream_ids(const struct

drm_dp_tunnel_group_state *group_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+{
+       const struct drm_dp_tunnel_state *tunnel_state;
+       u32 stream_mask = 0;
+
+       for_each_tunnel_state(group_state, tunnel_state) {
+               drm_WARN(to_group(group_state->base.obj)->mgr->dev,
+                        tunnel_state->stream_mask & stream_mask,
+                        "[DPTUN %s]: conflicting stream IDs %x (IDs

in other tunnels %x)\n",
+                        tunnel_state->tunnel_ref.tunnel->name, +                        tunnel_state->stream_mask,
+                        stream_mask);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+               stream_mask |= tunnel_state->stream_mask;
+       }
+}
+#else
+static void check_unique_stream_ids(const struct

drm_dp_tunnel_group_state *group_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+{
+}
+#endif
+
+static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
+{
+       return hweight32(stream_mask & (BIT(stream_id) - 1));
+}
+
+static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
+                          unsigned long old_mask, unsigned long

new_mask)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+{
+       unsigned long move_mask = old_mask & new_mask;
+       int *new_bws = NULL;
+       int id;
+
+       WARN_ON(!new_mask);
+
+       if (old_mask == new_mask)
+               return 0;
+
+       new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws),

GFP_KERNEL);
+       if (!new_bws)
+               return -ENOMEM;
+
+       for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask)) +               new_bws[stream_id_to_idx(new_mask, id)] = +                       tunnel_state-

Quoted 1 line

stream_bw[stream_id_to_idx(old_mask, id)];

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+
+       kfree(tunnel_state->stream_bw);
+       tunnel_state->stream_bw = new_bws;
+       tunnel_state->stream_mask = new_mask;
+
+       return 0;
+}
+
+static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+                        u8 stream_id, int bw)
+{
+       int err;
+
+       err = resize_bw_array(tunnel_state,
+                             tunnel_state->stream_mask,
+                             tunnel_state->stream_mask |

BIT(stream_id));
+       if (err)
+               return err;
+
+       tunnel_state->stream_bw[stream_id_to_idx(tunnel_state-

Quoted 1 line

stream_mask, stream_id)] = bw;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+
+       return 0;
+}
+
+static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+                          u8 stream_id)
+{
+       if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
+               drm_dp_tunnel_atomic_clear_state(tunnel_state);
+               return 0;
+       }
+
+       return resize_bw_array(tunnel_state,
+                              tunnel_state->stream_mask,
+                              tunnel_state->stream_mask &

~BIT(stream_id));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+}
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state

*state,
+                                        struct drm_dp_tunnel *tunnel,
+                                        u8 stream_id, int bw)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+{
+       struct drm_dp_tunnel_group_state *new_group_state =
+               drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+       struct drm_dp_tunnel_state *tunnel_state;
+       int err;
+
+       if (drm_WARN_ON(tunnel->group->mgr->dev,
+                       stream_id > BITS_PER_TYPE(tunnel_state-
Quoted 1 line

stream_mask)))

+               return -EINVAL;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+       tun_dbg(tunnel,
+               "Setting %d Mb/s for stream %d\n",
+               DPTUN_BW_ARG(bw), stream_id);
+
+       if (bw == 0) {
+               tunnel_state = get_tunnel_state(new_group_state,

tunnel);
+               if (!tunnel_state)
+                       return 0;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+
+               return clear_stream_bw(tunnel_state, stream_id);
+       }
+
+       tunnel_state = get_or_add_tunnel_state(new_group_state,

tunnel);
+       if (drm_WARN_ON(state->dev, !tunnel_state)) +               return -EINVAL;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+
+       err = set_stream_bw(tunnel_state, stream_id, bw);
+       if (err)
+               return err;
+
+       check_unique_stream_ids(new_group_state);
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct

drm_dp_tunnel_state *tunnel_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+{
+       int tunnel_bw = 0;
+       int i;
+
+       for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
+               tunnel_bw += tunnel_state->stream_bw[i];
+
+       return tunnel_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_tunnel_bw);
+
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct

drm_atomic_state *state,
+                                                   const struct drm_dp_tunnel *tunnel,
+                                                   u32 *stream_mask)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +18 -0
+{
+       struct drm_dp_tunnel_group_state *group_state =
+               drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       if (IS_ERR(group_state))
+               return PTR_ERR(group_state);
+
+       *stream_mask = 0;
+       for_each_tunnel_state(group_state, tunnel_state)
+               *stream_mask |= tunnel_state->stream_mask;
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
+
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state

*new_group_state,
+                                   u32 *failed_stream_mask) +{
+       struct drm_dp_tunnel_group *group = to_group(new_group_state-

Quoted 1 line

base.obj);

+       struct drm_dp_tunnel_state *new_tunnel_state;
+       u32 group_stream_mask = 0;
+       int group_bw = 0;
+
+       for_each_tunnel_state(new_group_state, new_tunnel_state) { +               struct drm_dp_tunnel *tunnel = new_tunnel_state-

Quoted 1 line

tunnel_ref.tunnel;

+               int max_dprx_bw = get_max_dprx_bw(tunnel); +               int tunnel_bw = drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);
+
+               tun_dbg(tunnel,
+                       "%sRequired %d/%d Mb/s total for tunnel.\n", +                       tunnel_bw > max_dprx_bw ? "Not enough BW: " :
"",
+                       DPTUN_BW_ARG(tunnel_bw),
+                       DPTUN_BW_ARG(max_dprx_bw));
+
+               if (tunnel_bw > max_dprx_bw) {
+                       *failed_stream_mask = new_tunnel_state-

Quoted 1 line

stream_mask;

+                       return -ENOSPC;
+               }
+
+               group_bw += min(roundup(tunnel_bw, tunnel-

Quoted 1 line

bw_granularity),

+                               max_dprx_bw);
+               group_stream_mask |= new_tunnel_state->stream_mask; +       }
+
+       tun_grp_dbg(group,
+                   "%sRequired %d/%d Mb/s total for tunnel group.\n",
+                   group_bw > group->available_bw ? "Not enough BW:
" : "",
+                   DPTUN_BW_ARG(group_bw),
+                   DPTUN_BW_ARG(group->available_bw));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+
+       if (group_bw > group->available_bw) {
+               *failed_stream_mask = group_stream_mask;
+               return -ENOSPC;
+       }
+
+       return 0;
+}
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state

*state,
+                                         u32 *failed_stream_mask)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+{
+       struct drm_dp_tunnel_group_state *new_group_state;
+       int i;
+
+       for_each_new_group_in_state(state, new_group_state, i) {
+               int ret;
+
+               ret =

drm_dp_tunnel_atomic_check_group_bw(new_group_state, +
failed_stream_mask);
+               if (ret)
+                       return ret;
+       }

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
+
+static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
+{
+       int i;
+
+       for (i = 0; i < mgr->group_count; i++) {
+               cleanup_group(&mgr->groups[i]);
+               drm_WARN_ON(mgr->dev, !list_empty(&mgr-
Quoted 1 line

groups[i].tunnels));

+       }

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       ref_tracker_dir_exit(&mgr->ref_tracker);
+#endif
+
+       kfree(mgr->groups);
+       kfree(mgr);
+}
+
+/**
+ * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
+ * @i915: i915 driver object
+ *
+ * Creates a DP tunnel manager.
+ *
+ * Returns a pointer to the tunnel manager if created successfully

or NULL in
+ * case of an error.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +2 -0
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int

max_group_count)
+{
+       struct drm_dp_tunnel_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
+       int i;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+       if (!mgr)
+               return NULL;
+
+       mgr->dev = dev;
+       init_waitqueue_head(&mgr->bw_req_queue);
+
+       mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups),

GFP_KERNEL);
+       if (!mgr->groups) {
+               kfree(mgr);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +33 -0
+
+               return NULL;
+       }
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
+#endif
+
+       for (i = 0; i < max_group_count; i++) {
+               if (!init_group(mgr, &mgr->groups[i])) {
+                       destroy_mgr(mgr);
+
+                       return NULL;
+               }
+
+               mgr->group_count++;
+       }
+
+       return mgr;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
+
+/**
+ * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
+ * @mgr: Tunnel manager object
+ *
+ * Destroy the tunnel manager.
+ */
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
+{
+       destroy_mgr(mgr);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);
include/drm/display/drm_dp.h +0 -0
index 281afff6ee4e5..8bfd5d007be8d 100644
@@ -1382,6 +1382,66 @@
  #define DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET     0x69494
  #define DP_HDCP_2_2_REG_DBG_OFFSET             0x69518

include/drm/display/drm_dp.h +10 -0
+/* DP-tunneling */
+#define DP_TUNNELING_OUI                               0xe0000
+#define  DP_TUNNELING_OUI_BYTES                                3
+
+#define DP_TUNNELING_DEV_ID                            0xe0003
+#define  DP_TUNNELING_DEV_ID_BYTES                     6
+
+#define DP_TUNNELING_HW_REV                            0xe0009
+#define  DP_TUNNELING_HW_REV_MAJOR_SHIFT               4
+#define  DP_TUNNELING_HW_REV_MAJOR_MASK                        (0xf

<< DP_TUNNELING_HW_REV_MAJOR_SHIFT)

include/drm/display/drm_dp.h +2 -0
+#define  DP_TUNNELING_HW_REV_MINOR_SHIFT               0
+#define  DP_TUNNELING_HW_REV_MINOR_MASK                        (0xf

<< DP_TUNNELING_HW_REV_MINOR_SHIFT)

include/drm/display/drm_dp.h +12 -0
+
+#define DP_TUNNELING_SW_REV_MAJOR                      0xe000a
+#define DP_TUNNELING_SW_REV_MINOR                      0xe000b
+
+#define DP_TUNNELING_CAPABILITIES                      0xe000d
+#define  DP_IN_BW_ALLOCATION_MODE_SUPPORT              (1 << 7)
+#define  DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT          (1 << 6)
+#define  DP_TUNNELING_SUPPORT                          (1 << 0)
+
+#define DP_IN_ADAPTER_INFO                             0xe000e
+#define  DP_IN_ADAPTER_NUMBER_BITS                     7
+#define  DP_IN_ADAPTER_NUMBER_MASK                     ((1 <<

DP_IN_ADAPTER_NUMBER_BITS) - 1)

include/drm/display/drm_dp.h +4 -0
+
+#define DP_USB4_DRIVER_ID                              0xe000f
+#define  DP_USB4_DRIVER_ID_BITS                                4
+#define  DP_USB4_DRIVER_ID_MASK                                ((1

<< DP_USB4_DRIVER_ID_BITS) - 1)

include/drm/display/drm_dp.h +7 -0
+
+#define DP_USB4_DRIVER_BW_CAPABILITY                   0xe0020
+#define  DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT     (1 << 7)
+
+#define DP_IN_ADAPTER_TUNNEL_INFORMATION               0xe0021
+#define  DP_GROUP_ID_BITS                              3
+#define  DP_GROUP_ID_MASK                              ((1 <<

DP_GROUP_ID_BITS) - 1)

include/drm/display/drm_dp.h +5 -0
+
+#define DP_BW_GRANULARITY                              0xe0022
+#define  DP_BW_GRANULARITY_MASK                                0x3
+
+#define

DP_ESTIMATED_BW                                        0xe0023 +#define
DP_ALLOCATED_BW                                        0xe0024

include/drm/display/drm_dp.h +19 -0
+
+#define DP_TUNNELING_STATUS                            0xe0025
+#define  DP_BW_ALLOCATION_CAPABILITY_CHANGED           (1 << 3)
+#define  DP_ESTIMATED_BW_CHANGED                       (1 << 2)
+#define  DP_BW_REQUEST_SUCCEEDED                       (1 << 1)
+#define  DP_BW_REQUEST_FAILED                          (1 << 0)
+
+#define DP_TUNNELING_MAX_LINK_RATE                     0xe0028
+
+#define DP_TUNNELING_MAX_LANE_COUNT                    0xe0029
+#define  DP_TUNNELING_MAX_LANE_COUNT_MASK              0x1f
+
+#define DP_DPTX_BW_ALLOCATION_MODE_CONTROL             0xe0030
+#define  DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE   (1 << 7)
+#define  DP_UNMASK_BW_ALLOCATION_IRQ                   (1 << 6)
+
+#define DP_REQUEST_BW                                  0xe0031
+#define  MAX_DP_REQUEST_BW                             255
+

 /* LTTPR: Link Training (LT)-tunable PHY Repeaters */ #define DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV 0xf0000 /* 1.3 */
 #define DP_MAX_LINK_RATE_PHY_REPEATER                      0xf0001 /* 1.4a */

include/drm/display/drm_dp_tunnel.h new file +38 -0
new file mode 100644
index 0000000000000..f6449b1b4e6e9
@@ -0,0 +1,270 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __DRM_DP_TUNNEL_H__
+#define __DRM_DP_TUNNEL_H__
+
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+
+struct drm_dp_aux;
+
+struct drm_device;
+
+struct drm_atomic_state;
+struct drm_dp_tunnel_mgr;
+struct drm_dp_tunnel_state;
+
+struct ref_tracker;
+
+struct drm_dp_tunnel_ref {
+       struct drm_dp_tunnel *tunnel;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       struct ref_tracker *tracker;
+#endif
+};
+
+#ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel);
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel);
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker

**tracker);

include/drm/display/drm_dp_tunnel.h new file +3 -0
+
+void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker

**tracker);

include/drm/display/drm_dp_tunnel.h new file +10 -0
+#else
+#define drm_dp_tunnel_get(tunnel, tracker) \
+       drm_dp_tunnel_get_untracked(tunnel)
+
+#define drm_dp_tunnel_put(tunnel, tracker) \
+       drm_dp_tunnel_put_untracked(tunnel)
+
+#endif
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel

*tunnel,
+                                          struct drm_dp_tunnel_ref *tunnel_ref)
+{
+       tunnel_ref->tunnel = drm_dp_tunnel_get(tunnel, &tunnel_ref-

Quoted 1 line

tracker);

include/drm/display/drm_dp_tunnel.h new file +3 -0
+}
+
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref

*tunnel_ref)

include/drm/display/drm_dp_tunnel.h new file +12 -0
+{
+       drm_dp_tunnel_put(tunnel_ref->tunnel, &tunnel_ref->tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+                    struct drm_dp_aux *aux);
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel);
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel

*tunnel);

include/drm/display/drm_dp_tunnel.h new file +11 -0
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw);
+int drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+                            struct drm_dp_aux *aux);
+
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel

*tunnel);

include/drm/display/drm_dp_tunnel.h new file +10 -0
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel);
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+                              struct drm_dp_tunnel *tunnel);
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+                                  const struct drm_dp_tunnel

*tunnel);

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state

*tunnel_state);

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state

*state,
+                                      struct drm_dp_tunnel *tunnel, +                                      u8 stream_id, int bw); +int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+                                                   const struct drm_dp_tunnel *tunnel,
+                                                   u32 *stream_mask);

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state

*state,
+                                         u32 *failed_stream_mask);

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct

drm_dp_tunnel_state *tunnel_state);

include/drm/display/drm_dp_tunnel.h new file +3 -0
+
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int

max_group_count);

include/drm/display/drm_dp_tunnel.h new file +15 -0
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr);
+
+#else
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+       return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker

**tracker)

include/drm/display/drm_dp_tunnel.h new file +6 -0
+{
+       return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker

**tracker) {}

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel

*tunnel,
+                                          struct drm_dp_tunnel_ref *tunnel_ref) {}
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref) {}

include/drm/display/drm_dp_tunnel.h new file +15 -0
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+                    struct drm_dp_aux *aux)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline int
+drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+       return 0;
+}
+
+static inline int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel

*tunnel)

include/drm/display/drm_dp_tunnel.h new file +5 -0
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int drm_dp_tunnel_disable_bw_alloc(struct

drm_dp_tunnel *tunnel)

include/drm/display/drm_dp_tunnel.h new file +5 -0
+{
+       return -EOPNOTSUPP;
+}
+
+static inline bool drm_dp_tunnel_bw_alloc_is_enabled(const struct

drm_dp_tunnel *tunnel)

include/drm/display/drm_dp_tunnel.h new file +23 -0
+{
+       return false;
+}
+
+static inline int
+drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel

*tunnel) {}

include/drm/display/drm_dp_tunnel.h new file +15 -0
+static inline int
+drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+                        struct drm_dp_aux *aux)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+       return 0;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel

*tunnel)

include/drm/display/drm_dp_tunnel.h new file +26 -0
+{
+       return 0;
+}
+
+static inline int
+drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+       return -1;
+}
+
+static inline const char *
+drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+       return NULL;
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+                              struct drm_dp_tunnel *tunnel)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+                                  const struct drm_dp_tunnel

*tunnel)

include/drm/display/drm_dp_tunnel.h new file +6 -0
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline void
+drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state

*tunnel_state) {}

include/drm/display/drm_dp_tunnel.h new file +11 -0
+
+static inline int
+drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+                                  struct drm_dp_tunnel *tunnel,
+                                  u8 stream_id, int bw)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_group_streams_in_state(struct

drm_atomic_state *state,
+                                               const struct drm_dp_tunnel *tunnel,
+                                               u32 *stream_mask)

include/drm/display/drm_dp_tunnel.h new file +6 -0
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state

*state,
+                                     u32 *failed_stream_mask)

include/drm/display/drm_dp_tunnel.h new file +6 -0
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state

*tunnel_state)

include/drm/display/drm_dp_tunnel.h new file +6 -0
+{
+       return 0;
+}
+
+static inline struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int

max_group_count)

include/drm/display/drm_dp_tunnel.h new file +11 -0
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr) {}
+
+
+#endif /* CONFIG_DRM_DISPLAY_DP_TUNNEL */
+
+#endif /* __DRM_DP_TUNNEL_H__ */
author
#7 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 37 lines · Jouni

[...]

Quoted 36 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +36 -0
+
+struct drm_dp_tunnel_group;
+
+struct drm_dp_tunnel {
+       struct drm_dp_tunnel_group *group;
+
+       struct list_head node;
+
+       struct kref kref;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       struct ref_tracker *tracker;
+#endif
+       struct drm_dp_aux *aux;
+       char name[8];
+
+       int bw_granularity;
+       int estimated_bw;
+       int allocated_bw;
+
+       int max_dprx_rate;
+       u8 max_dprx_lane_count;
+
+       u8 adapter_id;
+
+       bool bw_alloc_supported:1;
+       bool bw_alloc_enabled:1;
+       bool has_io_error:1;
+       bool destroyed:1;
+};
+
+struct drm_dp_tunnel_group_state;
+
+struct drm_dp_tunnel_state {
+       struct drm_dp_tunnel_group_state *group_state;
+
+       struct drm_dp_tunnel_ref tunnel_ref;
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 164

Quoted 4 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+
+       struct list_head node;
+
+       u32 stream_mask;

I'm wondering if drm_dp_tunnel_state can really contain several streams and what kind of scenario this would be? From i915 point of view I would understand that several pipes are routed to DP tunnel.

Yes, multiple pipes through the same tunnel and the use case for that is MST with multiple streams. The "stream" term is only an abstraction where it could be a different physical thing in various drivers, but for i915 it just means pipes. Not 100% sure if that's the best mapping, since in case of bigjoiner there would be multiple pipes, but possibly (in the SST case) only one stream from the tunneling POV.

Quoted 1 line

Is it bigjoiner case?

IIUC in that (SST) case the streams would be joined already before going to the TBT DP_IN adapter, so that's only one stream in stream_mask above (unless MST + bigjoiner, where you could have 2 MST/DP tunnel streams each consisting of 2 pipes).

Quoted 2,127 lines

BR,

Jouni Högander

Quoted 2,122 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+       int *stream_bw;
+};
+
+struct drm_dp_tunnel_group_state {
+       struct drm_private_state base;
+
+       struct list_head tunnel_states;
+};
+
+struct drm_dp_tunnel_group {
+       struct drm_private_obj base;
+       struct drm_dp_tunnel_mgr *mgr;
+
+       struct list_head tunnels;
+
+       int available_bw;       /* available BW including the

allocated_bw of all tunnels */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +24 -0
+       int drv_group_id;
+
+       char name[8];
+
+       bool active:1;
+};
+
+struct drm_dp_tunnel_mgr {
+       struct drm_device *dev;
+
+       int group_count;
+       struct drm_dp_tunnel_group *groups;
+       wait_queue_head_t bw_req_queue;
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       struct ref_tracker_dir ref_tracker;
+#endif
+};
+
+static int next_reg_area(int *offset)
+{
+       *offset = find_next_bit(dptun_info_regs, 64, *offset);
+
+       return find_next_zero_bit(dptun_info_regs, 64, *offset + 1) -

*offset;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+}
+
+#define tunnel_reg_ptr(__regs, __address) ({ \
+       WARN_ON(!test_bit((__address) - DP_TUNNELING_BASE,

dptun_info_regs)); \
+ &(__regs)->buf[bitmap_weight(dptun_info_regs, (__address) -
DP_TUNNELING_BASE)]; \

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+})
+
+static int read_tunnel_regs(struct drm_dp_aux *aux, struct

drm_dp_tunnel_regs *regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+{
+       int offset = 0;
+       int len;
+
+       while ((len = next_reg_area(&offset))) {
+               int address = DP_TUNNELING_BASE + offset;
+
+               if (drm_dp_dpcd_read(aux, address,

tunnel_reg_ptr(regs, address), len) < 0)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+                       return -EIO;
+
+               offset += len;
+       }
+
+       return 0;
+}
+
+static u8 tunnel_reg(const struct drm_dp_tunnel_regs *regs, int

address)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+{
+       return *tunnel_reg_ptr(regs, address);
+}
+
+static int tunnel_reg_drv_group_id(const struct drm_dp_tunnel_regs

*regs)

(quoted diff) +2 -0
+{
+       int drv_id = tunnel_reg(regs, DP_USB4_DRIVER_ID) &

DP_USB4_DRIVER_ID_MASK;
+ int group_id = tunnel_reg(regs,
DP_IN_ADAPTER_TUNNEL_INFORMATION) & DP_GROUP_ID_MASK;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+
+       if (!group_id)
+               return 0;
+
+       return (drv_id << DP_GROUP_ID_BITS) | group_id;
+}
+
+/* Return granularity in kB/s units */
+static int tunnel_reg_bw_granularity(const struct drm_dp_tunnel_regs

*regs)

(quoted diff) +2 -0
+{
+       int gr = tunnel_reg(regs, DP_BW_GRANULARITY) &

DP_BW_GRANULARITY_MASK;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+       WARN_ON(gr > 2);
+
+       return (250000 << gr) / 8;
+}
+
+static int tunnel_reg_max_dprx_rate(const struct drm_dp_tunnel_regs

*regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+{
+       u8 bw_code = tunnel_reg(regs, DP_TUNNELING_MAX_LINK_RATE);
+
+       return drm_dp_bw_code_to_link_rate(bw_code);
+}
+
+static int tunnel_reg_max_dprx_lane_count(const struct

drm_dp_tunnel_regs *regs)

(quoted diff) +2 -0
+{
+       u8 lane_count = tunnel_reg(regs, DP_TUNNELING_MAX_LANE_COUNT)

&

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+                       DP_TUNNELING_MAX_LANE_COUNT_MASK;
+
+       return lane_count;
+}
+
+static bool tunnel_reg_bw_alloc_supported(const struct

drm_dp_tunnel_regs *regs)

(quoted diff) +2 -0
+{
+       u8 cap_mask = DP_TUNNELING_SUPPORT |

DP_IN_BW_ALLOCATION_MODE_SUPPORT;

(quoted diff) +2 -0
+
+       if ((tunnel_reg(regs, DP_TUNNELING_CAPABILITIES) & cap_mask)

!= cap_mask)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+               return false;
+
+       return tunnel_reg(regs, DP_USB4_DRIVER_BW_CAPABILITY) &
+              DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT;
+}
+
+static bool tunnel_reg_bw_alloc_enabled(const struct

drm_dp_tunnel_regs *regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +22 -0
+{
+       return tunnel_reg(regs, DP_DPTX_BW_ALLOCATION_MODE_CONTROL) &
+               DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE;
+}
+
+static int tunnel_group_drv_id(int drv_group_id)
+{
+       return drv_group_id >> DP_GROUP_ID_BITS;
+}
+
+static int tunnel_group_id(int drv_group_id)
+{
+       return drv_group_id & DP_GROUP_ID_MASK;
+}
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+       return tunnel->name;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_name);
+
+static const char *drm_dp_tunnel_group_name(const struct

drm_dp_tunnel_group *group)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+       return group->name;
+}
+
+static struct drm_dp_tunnel_group *
+lookup_or_alloc_group(struct drm_dp_tunnel_mgr *mgr, int

drv_group_id)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+{
+       struct drm_dp_tunnel_group *group = NULL;
+       int i;
+
+       for (i = 0; i < mgr->group_count; i++) {
+               /*
+                * A tunnel group with 0 group ID shouldn't have more

than one

drivers/gpu/drm/display/drm_dp_tunnel.c new file +13 -0
+                * tunnels.
+                */
+               if (tunnel_group_id(drv_group_id) &&
+                   mgr->groups[i].drv_group_id == drv_group_id)
+                       return &mgr->groups[i];
+
+               if (!group && !mgr->groups[i].active)
+                       group = &mgr->groups[i];
+       }
+
+       if (!group) {
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: Can't allocate more tunnel

groups\n");

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+               return NULL;
+       }
+
+       group->drv_group_id = drv_group_id;
+       group->active = true;
+
+       snprintf(group->name, sizeof(group->name), "%d:%d:*",
+                tunnel_group_drv_id(drv_group_id) & ((1 <<

DP_GROUP_ID_BITS) - 1),
+ tunnel_group_id(drv_group_id) & ((1 <<
DP_USB4_DRIVER_ID_BITS) - 1));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +27 -0
+
+       return group;
+}
+
+static void free_group(struct drm_dp_tunnel_group *group)
+{
+       struct drm_dp_tunnel_mgr *mgr = group->mgr;
+
+       if (drm_WARN_ON(mgr->dev, !list_empty(&group->tunnels)))
+               return;
+
+       group->drv_group_id = 0;
+       group->available_bw = -1;
+       group->active = false;
+}
+
+static struct drm_dp_tunnel *
+tunnel_get(struct drm_dp_tunnel *tunnel)
+{
+       kref_get(&tunnel->kref);
+
+       return tunnel;
+}
+
+static void free_tunnel(struct kref *kref)
+{
+       struct drm_dp_tunnel *tunnel = container_of(kref,

typeof(*tunnel), kref);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +115 -0
+       struct drm_dp_tunnel_group *group = tunnel->group;
+
+       list_del(&tunnel->node);
+       if (list_empty(&group->tunnels))
+               free_group(group);
+
+       kfree(tunnel);
+}
+
+static void tunnel_put(struct drm_dp_tunnel *tunnel)
+{
+       kref_put(&tunnel->kref, free_tunnel);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void track_tunnel_ref(struct drm_dp_tunnel *tunnel,
+                            struct ref_tracker **tracker)
+{
+       ref_tracker_alloc(&tunnel->group->mgr->ref_tracker,
+                         tracker, GFP_KERNEL);
+}
+
+static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
+                              struct ref_tracker **tracker)
+{
+       ref_tracker_free(&tunnel->group->mgr->ref_tracker,
+                        tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+       track_tunnel_ref(tunnel, NULL);
+
+       return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);
+
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel)
+{
+       tunnel_put(tunnel);
+       untrack_tunnel_ref(tunnel, NULL);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put_untracked);
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel,
+                   struct ref_tracker **tracker)
+{
+       track_tunnel_ref(tunnel, tracker);
+
+       return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get);
+
+void drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel,
+                        struct ref_tracker **tracker)
+{
+       untrack_tunnel_ref(tunnel, tracker);
+       tunnel_put(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put);
+#else
+#define track_tunnel_ref(tunnel, tracker) do {} while (0)
+#define untrack_tunnel_ref(tunnel, tracker) do {} while (0)
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+       return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);
+
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel)
+{
+       tunnel_put(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put_untracked);
+#endif
+
+static bool add_tunnel_to_group(struct drm_dp_tunnel_mgr *mgr,
+                               int drv_group_id,
+                               struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_group *group =
+               lookup_or_alloc_group(mgr, drv_group_id);
+
+       if (!group)
+               return false;
+
+       tunnel->group = group;
+       list_add(&tunnel->node, &group->tunnels);
+
+       return true;
+}
+
+static struct drm_dp_tunnel *
+create_tunnel(struct drm_dp_tunnel_mgr *mgr,
+             struct drm_dp_aux *aux,
+             const struct drm_dp_tunnel_regs *regs)
+{
+       int drv_group_id = tunnel_reg_drv_group_id(regs);
+       struct drm_dp_tunnel *tunnel;
+
+       tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
+       if (!tunnel)
+               return NULL;
+
+       INIT_LIST_HEAD(&tunnel->node);
+
+       kref_init(&tunnel->kref);
+
+       tunnel->aux = aux;
+
+       tunnel->adapter_id = tunnel_reg(regs, DP_IN_ADAPTER_INFO) &

DP_IN_ADAPTER_NUMBER_MASK;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+
+       snprintf(tunnel->name, sizeof(tunnel->name), "%d:%d:%d",
+                tunnel_group_drv_id(drv_group_id) & ((1 <<

DP_GROUP_ID_BITS) - 1),
+ tunnel_group_id(drv_group_id) & ((1 <<
DP_USB4_DRIVER_ID_BITS) - 1),
+ tunnel->adapter_id & ((1 <<
DP_IN_ADAPTER_NUMBER_BITS) - 1));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+       tunnel->bw_granularity = tunnel_reg_bw_granularity(regs);
+       tunnel->allocated_bw = tunnel_reg(regs, DP_ALLOCATED_BW) *
+                              tunnel->bw_granularity;
+
+       tunnel->bw_alloc_supported =

tunnel_reg_bw_alloc_supported(regs);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +35 -0
+       tunnel->bw_alloc_enabled = tunnel_reg_bw_alloc_enabled(regs);
+
+       if (!add_tunnel_to_group(mgr, drv_group_id, tunnel)) {
+               kfree(tunnel);
+
+               return NULL;
+       }
+
+       track_tunnel_ref(tunnel, &tunnel->tracker);
+
+       return tunnel;
+}
+
+static void destroy_tunnel(struct drm_dp_tunnel *tunnel)
+{
+       untrack_tunnel_ref(tunnel, &tunnel->tracker);
+       tunnel_put(tunnel);
+}
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel)
+{
+       tunnel->has_io_error = true;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_set_io_error);
+
+static char yes_no_chr(int val)
+{
+       return val ? 'Y' : 'N';
+}
+
+#define SKIP_DPRX_CAPS_CHECK           BIT(0)
+#define ALLOW_ALLOCATED_BW_CHANGE      BIT(1)
+
+static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr,
+                                 const struct drm_dp_tunnel_regs

*regs,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+                                 unsigned int flags)
+{
+       int drv_group_id = tunnel_reg_drv_group_id(regs);
+       bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK);
+       bool ret = true;
+
+       if (!tunnel_reg_bw_alloc_supported(regs)) {
+               if (tunnel_group_id(drv_group_id)) {
+                       drm_dbg_kms(mgr->dev,
+                                   "DPTUN: A non-zero group ID is

only allowed with BWA support\n");

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+                       ret = false;
+               }
+
+               if (tunnel_reg(regs, DP_ALLOCATED_BW)) {
+                       drm_dbg_kms(mgr->dev,
+                                   "DPTUN: BW is allocated without

BWA support\n");

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+                       ret = false;
+               }
+
+               return ret;
+       }
+
+       if (!tunnel_group_id(drv_group_id)) {
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: BWA support requires a non-zero

group ID\n");

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+               ret = false;
+       }
+
+       if (check_dprx &&

hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) {

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: Invalid DPRX lane count: %d\n",
+                           tunnel_reg_max_dprx_lane_count(regs));
+
+               ret = false;
+       }
+
+       if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) {
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: DPRX rate is 0\n");
+
+               ret = false;
+       }
+
+       if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs,

DP_ESTIMATED_BW)) {

(quoted diff) +2 -0
+               drm_dbg_kms(mgr->dev,
+                           "DPTUN: Allocated BW %d > estimated BW %d

Mb/s\n",
+ DPTUN_BW_ARG(tunnel_reg(regs,
DP_ALLOCATED_BW) *
+
tunnel_reg_bw_granularity(regs)),
+ DPTUN_BW_ARG(tunnel_reg(regs,
DP_ESTIMATED_BW) *
+
tunnel_reg_bw_granularity(regs)));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+               ret = false;
+       }
+
+       return ret;
+}
+
+static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel

*tunnel,
+ const struct
drm_dp_tunnel_regs *regs,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+                                         unsigned int flags)
+{
+       int new_drv_group_id = tunnel_reg_drv_group_id(regs);
+       bool ret = true;
+
+       if (tunnel->bw_alloc_supported !=

tunnel_reg_bw_alloc_supported(regs)) {

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+               tun_dbg(tunnel,
+                       "BW alloc support has changed %c -> %c\n",
+                       yes_no_chr(tunnel->bw_alloc_supported),
+                       yes_no_chr(tunnel_reg_bw_alloc_supported(regs

)));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+
+               ret = false;
+       }
+
+       if (tunnel->group->drv_group_id != new_drv_group_id) {
+               tun_dbg(tunnel,
+                       "Driver/group ID has changed %d:%d:* ->

%d:%d:*\n",
+ tunnel_group_drv_id(tunnel->group-

Quoted 1 line

drv_group_id),

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+                       tunnel_group_id(tunnel->group->drv_group_id),
+                       tunnel_group_drv_id(new_drv_group_id),
+                       tunnel_group_id(new_drv_group_id));
+
+               ret = false;
+       }
+
+       if (!tunnel->bw_alloc_supported)
+               return ret;
+
+       if (tunnel->bw_granularity !=

tunnel_reg_bw_granularity(regs)) {

(quoted diff) +2 -0
+               tun_dbg(tunnel,
+                       "BW granularity has changed: %d -> %d

Mb/s\n",

(quoted diff) +2 -0
+                       DPTUN_BW_ARG(tunnel->bw_granularity),
+                       DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs))

);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+               ret = false;
+       }
+
+       /*
+        * On some devices at least the BW alloc mode enabled status

is always

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+        * reported as 0, so skip checking that here.
+        */
+
+       if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) &&
+           tunnel->allocated_bw !=
+           tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel-
Quoted 1 line

bw_granularity) {

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+               tun_dbg(tunnel,
+                       "Allocated BW has changed: %d -> %d Mb/s\n",
+                       DPTUN_BW_ARG(tunnel->allocated_bw),
+                       DPTUN_BW_ARG(tunnel_reg(regs,

DP_ALLOCATED_BW) * tunnel->bw_granularity));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +31 -0
+
+               ret = false;
+       }
+
+       return ret;
+}
+
+static int
+read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel,
+                           struct drm_dp_tunnel_regs *regs,
+                           unsigned int flags)
+{
+       int err;
+
+       err = read_tunnel_regs(tunnel->aux, regs);
+       if (err < 0) {
+               drm_dp_tunnel_set_io_error(tunnel);
+
+               return err;
+       }
+
+       if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags))
+               return -EINVAL;
+
+       if (!tunnel_info_changes_are_valid(tunnel, regs, flags))
+               return -EINVAL;
+
+       return 0;
+}
+
+static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const

struct drm_dp_tunnel_regs *regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+{
+       bool changed = false;
+
+       if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate)

{
+ tunnel->max_dprx_rate =
tunnel_reg_max_dprx_rate(regs);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+               changed = true;
+       }
+
+       if (tunnel_reg_max_dprx_lane_count(regs) != tunnel-
Quoted 1 line

max_dprx_lane_count) {

+ tunnel->max_dprx_lane_count =
tunnel_reg_max_dprx_lane_count(regs);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +18 -0
+               changed = true;
+       }
+
+       return changed;
+}
+
+static int dev_id_len(const u8 *dev_id, int max_len)
+{
+       while (max_len && dev_id[max_len - 1] == '\0')
+               max_len--;
+
+       return max_len;
+}
+
+static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel)
+{
+       int bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate,
+                                          tunnel-
Quoted 1 line

max_dprx_lane_count);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+
+       return min(roundup(bw, tunnel->bw_granularity),
+                  MAX_DP_REQUEST_BW * tunnel->bw_granularity);
+}
+
+static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel)
+{
+       return min(get_max_dprx_bw(tunnel), tunnel->group-
Quoted 1 line

available_bw);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+}
+
+/**
+ * drm_dp_tunnel_detect - Detect DP tunnel on the link
+ * @mgr: Tunnel manager
+ * @aux: DP AUX on which the tunnel will be detected
+ *
+ * Detect if there is any DP tunnel on the link and add it to the

tunnel
+ * group's tunnel list.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +17 -0
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+                      struct drm_dp_aux *aux)
+{
+       struct drm_dp_tunnel_regs regs;
+       struct drm_dp_tunnel *tunnel;
+       int err;
+
+       err = read_tunnel_regs(aux, &regs);
+       if (err)
+               return ERR_PTR(err);
+
+       if (!(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+             DP_TUNNELING_SUPPORT))
+               return ERR_PTR(-ENODEV);
+
+       /* The DPRX caps are valid only after enabling BW alloc mode.

*/

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+       if (!tunnel_regs_are_valid(mgr, &regs, SKIP_DPRX_CAPS_CHECK))
+               return ERR_PTR(-EINVAL);
+
+       tunnel = create_tunnel(mgr, aux, &regs);
+       if (!tunnel)
+               return ERR_PTR(-ENOMEM);
+
+       tun_dbg(tunnel,
+               "OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%c

BWA-Sup:%c BWA-En:%c\n",

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+               DP_TUNNELING_OUI_BYTES,
+                       tunnel_reg_ptr(&regs, DP_TUNNELING_OUI),
+               dev_id_len(tunnel_reg_ptr(&regs,

DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES),

(quoted diff) +2 -0
+                       tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID),
+               (tunnel_reg(&regs, DP_TUNNELING_HW_REV) &

DP_TUNNELING_HW_REV_MAJOR_MASK) >>

(quoted diff) +2 -0
+                       DP_TUNNELING_HW_REV_MAJOR_SHIFT,
+               (tunnel_reg(&regs, DP_TUNNELING_HW_REV) &

DP_TUNNELING_HW_REV_MINOR_MASK) >>

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+                       DP_TUNNELING_HW_REV_MINOR_SHIFT,
+               tunnel_reg(&regs, DP_TUNNELING_SW_REV_MAJOR),
+               tunnel_reg(&regs, DP_TUNNELING_SW_REV_MINOR),
+               yes_no_chr(tunnel_reg(&regs,

DP_TUNNELING_CAPABILITIES) &

drivers/gpu/drm/display/drm_dp_tunnel.c new file +54 -0
+                          DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT),
+               yes_no_chr(tunnel->bw_alloc_supported),
+               yes_no_chr(tunnel->bw_alloc_enabled));
+
+       return tunnel;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_detect);
+
+/**
+ * drm_dp_tunnel_destroy - Destroy tunnel object
+ * @tunnel: Tunnel object
+ *
+ * Remove the tunnel from the tunnel topology and destroy it.
+ */
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+       if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed))
+               return -ENODEV;
+
+       tun_dbg(tunnel, "destroying\n");
+
+       tunnel->destroyed = true;
+       destroy_tunnel(tunnel);
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_destroy);
+
+static int check_tunnel(const struct drm_dp_tunnel *tunnel)
+{
+       if (tunnel->destroyed)
+               return -ENODEV;
+
+       if (tunnel->has_io_error)
+               return -EIO;
+
+       return 0;
+}
+
+static int group_allocated_bw(struct drm_dp_tunnel_group *group)
+{
+       struct drm_dp_tunnel *tunnel;
+       int group_allocated_bw = 0;
+
+       for_each_tunnel_in_group(group, tunnel) {
+               if (check_tunnel(tunnel) == 0 &&
+                   tunnel->bw_alloc_enabled)
+                       group_allocated_bw += tunnel->allocated_bw;
+       }
+
+       return group_allocated_bw;
+}
+
+static int calc_group_available_bw(const struct drm_dp_tunnel

*tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+{
+       return group_allocated_bw(tunnel->group) -
+              tunnel->allocated_bw +
+              tunnel->estimated_bw;
+}
+
+static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
+                                    const struct drm_dp_tunnel_regs

*regs)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+       struct drm_dp_tunnel *tunnel_iter;
+       int group_available_bw;
+       bool changed;
+
+       tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) *

tunnel->bw_granularity;

(quoted diff) +2 -0
+
+       if (calc_group_available_bw(tunnel) == tunnel->group-
Quoted 1 line

available_bw)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +13 -0
+               return 0;
+
+       for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
+               int err;
+
+               if (tunnel_iter == tunnel)
+                       continue;
+
+               if (check_tunnel(tunnel_iter) != 0 ||
+                   !tunnel_iter->bw_alloc_enabled)
+                       continue;
+
+               err = drm_dp_dpcd_probe(tunnel_iter->aux,

DP_DPCD_REV);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+               if (err) {
+                       tun_dbg(tunnel_iter,
+                               "Probe failed, assume disconnected

(err %pe)\n",

drivers/gpu/drm/display/drm_dp_tunnel.c new file +19 -0
+                               ERR_PTR(err));
+                       drm_dp_tunnel_set_io_error(tunnel_iter);
+               }
+       }
+
+       group_available_bw = calc_group_available_bw(tunnel);
+
+       tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
+               DPTUN_BW_ARG(tunnel->group->available_bw),
+               DPTUN_BW_ARG(group_available_bw));
+
+       changed = tunnel->group->available_bw != group_available_bw;
+
+       tunnel->group->available_bw = group_available_bw;
+
+       return changed ? 1 : 0;
+}
+
+static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool

enable)

(quoted diff) +2 -0
+{
+       u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE |

DP_UNMASK_BW_ALLOCATION_IRQ;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+       u8 val;
+
+       if (drm_dp_dpcd_readb(tunnel->aux,

DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+               goto out_err;
+
+       if (enable)
+               val |= mask;
+       else
+               val &= ~mask;
+
+       if (drm_dp_dpcd_writeb(tunnel->aux,

DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +14 -0
+               goto out_err;
+
+       tunnel->bw_alloc_enabled = enable;
+
+       return 0;
+
+out_err:
+       drm_dp_tunnel_set_io_error(tunnel);
+
+       return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_enable_bw_alloc: Enable DP tunnel BW allocation

mode
+ * @tunnel: Tunnel object
+ *
+ * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +36 -0
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_regs regs;
+       int err = check_tunnel(tunnel);
+
+       if (err)
+               return err;
+
+       if (!tunnel->bw_alloc_supported)
+               return -EOPNOTSUPP;
+
+       if (!tunnel_group_id(tunnel->group->drv_group_id))
+               return -EINVAL;
+
+       err = set_bw_alloc_mode(tunnel, true);
+       if (err)
+               goto out;
+
+       err = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+       if (err) {
+               set_bw_alloc_mode(tunnel, false);
+
+               goto out;
+       }
+
+       if (!tunnel->max_dprx_rate)
+               update_dprx_caps(tunnel, &regs);
+
+       if (tunnel->group->available_bw == -1) {
+               err = update_group_available_bw(tunnel, &regs);
+               if (err > 0)
+                       err = 0;
+       }
+out:
+       tun_dbg_stat(tunnel, err,
+                    "Enabling BW alloc mode: DPRX:%dx%d Group

alloc:%d/%d Mb/s",
+ tunnel->max_dprx_rate / 100, tunnel-

Quoted 1 line

max_dprx_lane_count,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+                    DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+                    DPTUN_BW_ARG(tunnel->group->available_bw));
+
+       return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
+
+/**
+ * drm_dp_tunnel_disable_bw_alloc: Disable DP tunnel BW allocation

mode
+ * @tunnel: Tunnel object
+ *
+ * Disable the DP tunnel BW allocation mode on @tunnel.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+       int err = check_tunnel(tunnel);
+
+       if (err)
+               return err;
+
+       err = set_bw_alloc_mode(tunnel, false);
+
+       tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
+
+       return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
+
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel

*tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+       return tunnel->bw_alloc_enabled;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
+
+static int bw_req_complete(struct drm_dp_aux *aux, bool

*status_changed)

(quoted diff) +2 -0
+{
+       u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED |

DP_BW_REQUEST_FAILED;
+ u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED |
DP_ESTIMATED_BW_CHANGED;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +33 -0
+       u8 val;
+
+       if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+               return -EIO;
+
+       *status_changed = val & status_change_mask;
+
+       val &= bw_req_mask;
+
+       if (!val)
+               return -EAGAIN;
+
+       if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, val) < 0)
+               return -EIO;
+
+       return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
+}
+
+static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+       struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
+       int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
+       unsigned long wait_expires;
+       DEFINE_WAIT(wait);
+       int err;
+
+       /* Atomic check should prevent the following. */
+       if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
+               err = -EINVAL;
+               goto out;
+       }
+
+       if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW,

request_bw) < 0) {

drivers/gpu/drm/display/drm_dp_tunnel.c new file +17 -0
+               err = -EIO;
+               goto out;
+       }
+
+       wait_expires = jiffies + msecs_to_jiffies(3000);
+
+       for (;;) {
+               bool status_changed;
+
+               err = bw_req_complete(tunnel->aux, &status_changed);
+               if (err != -EAGAIN)
+                       break;
+
+               if (status_changed) {
+                       struct drm_dp_tunnel_regs regs;
+
+                       err = read_and_verify_tunnel_regs(tunnel,

&regs,
+
ALLOW_ALLOCATED_BW_CHANGE);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+                       if (err)
+                               break;
+               }
+
+               if (time_after(jiffies, wait_expires)) {
+                       err = -ETIMEDOUT;
+                       break;
+               }
+
+               prepare_to_wait(&mgr->bw_req_queue, &wait,

TASK_UNINTERRUPTIBLE);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+               schedule_timeout(msecs_to_jiffies(200));
+       };
+
+       finish_wait(&mgr->bw_req_queue, &wait);
+
+       if (err)
+               goto out;
+
+       tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
+
+out:
+       tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel:

Group alloc:%d/%d Mb/s",
+ DPTUN_BW_ARG(request_bw * tunnel-

Quoted 1 line

bw_granularity),

drivers/gpu/drm/display/drm_dp_tunnel.c new file +22 -0
+                    DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+                    DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+                    DPTUN_BW_ARG(tunnel->group->available_bw));
+
+       if (err == -EIO)
+               drm_dp_tunnel_set_io_error(tunnel);
+
+       return err;
+}
+
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+       int err = check_tunnel(tunnel);
+
+       if (err)
+               return err;
+
+       return allocate_tunnel_bw(tunnel, bw);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
+
+static int check_and_clear_status_change(struct drm_dp_tunnel

*tunnel)

(quoted diff) +2 -0
+{
+       u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED |

DP_ESTIMATED_BW_CHANGED;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+       u8 val;
+
+       if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val)

< 0)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+               goto out_err;
+
+       val &= mask;
+
+       if (val) {
+               if (drm_dp_dpcd_writeb(tunnel->aux,

DP_TUNNELING_STATUS, val) < 0)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+                       goto out_err;
+
+               return 1;
+       }
+
+       if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
+               return 0;
+
+       /*
+        * Check for estimated BW changes explicitly to account for

lost

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+        * BW change notifications.
+        */
+       if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) <

0)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+               goto out_err;
+
+       if (val * tunnel->bw_granularity != tunnel->estimated_bw)
+               return 1;
+
+       return 0;
+
+out_err:
+       drm_dp_tunnel_set_io_error(tunnel);
+
+       return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_update_state: Update DP tunnel SW state with the HW

state
+ * @tunnel: Tunnel object
+ *
+ * Update the SW state of @tunnel with the HW state.
+ *
+ * Returns 0 if the state has not changed, 1 if it has changed and got updated
+ * successfully and a negative error code otherwise.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +30 -0
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_regs regs;
+       bool changed = false;
+       int ret = check_tunnel(tunnel);
+
+       if (ret < 0)
+               return ret;
+
+       ret = check_and_clear_status_change(tunnel);
+       if (ret < 0)
+               goto out;
+
+       if (!ret)
+               return 0;
+
+       ret = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+       if (ret)
+               goto out;
+
+       if (update_dprx_caps(tunnel, &regs))
+               changed = true;
+
+       ret = update_group_available_bw(tunnel, &regs);
+       if (ret == 1)
+               changed = true;
+
+out:
+       tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
+                    "State update: Changed:%c DPRX:%dx%d Tunnel

alloc:%d/%d Group alloc:%d/%d Mb/s",

(quoted diff) +2 -0
+                    yes_no_chr(changed),
+                    tunnel->max_dprx_rate / 100, tunnel-
Quoted 1 line

max_dprx_lane_count,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +20 -0
+                    DPTUN_BW_ARG(tunnel->allocated_bw),
+                    DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+                    DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+                    DPTUN_BW_ARG(tunnel->group->available_bw));
+
+       if (ret < 0)
+               return ret;
+
+       if (changed)
+               return 1;
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_update_state);
+
+/*
+ * Returns 0 if no re-probe is needed, 1 if a re-probe is needed,
+ * a negative error code otherwise.
+ */
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct

drm_dp_aux *aux)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+{
+       u8 val;
+
+       if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+               return -EIO;
+
+       if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
+               wake_up_all(&mgr->bw_req_queue);
+
+       if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED |

DP_ESTIMATED_BW_CHANGED))

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+               return 1;
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
+
+/**
+ * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the

tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum link rate of the DPRX connected
+ * to @tunnel. Note that this rate will not be limited by the BW limit of the
+ * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
+ * registers.
+ *
+ * Returns the maximum link rate in 10 kbit/s units.
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+       return tunnel->max_dprx_rate;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
+
+/**
+ * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count

of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum lane count of the DPRX connected
+ * to @tunnel. Note that this lane count will not be limited by the BW limit of
+ * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
+ * registers.
+ *
+ * Returns the maximum lane count.
+ */
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+{
+       return tunnel->max_dprx_lane_count;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
+
+/**
+ * drm_dp_tunnel_available_bw - Query the estimated total available

BW of the tunnel
+ * @tunnel: Tunnel object
+ *
+ * This function is used to query the estimated total available BW of the
+ * tunnel. This includes the currently allocated and free BW for all the
+ * tunnels in @tunnel's group. The available BW is valid only after the BW
+ * allocation mode has been enabled for the tunnel and its state got updated
+ * calling drm_dp_tunnel_update_state().
+ *
+ * Returns the @tunnel group's estimated total available bandwidth in kB/s
+ * units, or -1 if the available BW isn't valid (the BW allocation mode is
+ * not enabled or the tunnel's state hasn't been updated).
+ */

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+       return tunnel->group->available_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
+
+static struct drm_dp_tunnel_group_state *
+drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
+                                    const struct drm_dp_tunnel

*tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+{
+       return (struct drm_dp_tunnel_group_state *)
+               drm_atomic_get_private_obj_state(state,
+                                                &tunnel->group-
Quoted 1 line

base);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+}
+
+static struct drm_dp_tunnel_state *
+add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+                struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       tun_dbg_atomic(tunnel,
+                      "Adding state for tunnel %p to group state

%p\n",

drivers/gpu/drm/display/drm_dp_tunnel.c new file +17 -0
+                      tunnel, group_state);
+
+       tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
+       if (!tunnel_state)
+               return NULL;
+
+       tunnel_state->group_state = group_state;
+
+       drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
+
+       INIT_LIST_HEAD(&tunnel_state->node);
+       list_add(&tunnel_state->node, &group_state->tunnel_states);
+
+       return tunnel_state;
+}
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state

*tunnel_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+{
+       tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
+                      "Clearing state for tunnel %p\n",
+                      tunnel_state->tunnel_ref.tunnel);
+
+       list_del(&tunnel_state->node);
+
+       kfree(tunnel_state->stream_bw);
+       drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
+
+       kfree(tunnel_state);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_clear_state);
+
+static void clear_tunnel_group_state(struct

drm_dp_tunnel_group_state *group_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+{
+       struct drm_dp_tunnel_state *tunnel_state;
+       struct drm_dp_tunnel_state *tunnel_state_tmp;
+
+       for_each_tunnel_state_safe(group_state, tunnel_state,

tunnel_state_tmp)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +18 -0
+               drm_dp_tunnel_atomic_clear_state(tunnel_state);
+}
+
+static struct drm_dp_tunnel_state *
+get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+                const struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       for_each_tunnel_state(group_state, tunnel_state)
+               if (tunnel_state->tunnel_ref.tunnel == tunnel)
+                       return tunnel_state;
+
+       return NULL;
+}
+
+static struct drm_dp_tunnel_state *
+get_or_add_tunnel_state(struct drm_dp_tunnel_group_state

*group_state,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+                       struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       tunnel_state = get_tunnel_state(group_state, tunnel);
+       if (tunnel_state)
+               return tunnel_state;
+
+       return add_tunnel_state(group_state, tunnel);
+}
+
+static struct drm_private_state *
+tunnel_group_duplicate_state(struct drm_private_obj *obj)
+{
+       struct drm_dp_tunnel_group_state *group_state =

to_group_state(obj->state);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+       if (!group_state)
+               return NULL;
+
+       INIT_LIST_HEAD(&group_state->tunnel_states);
+
+       __drm_atomic_helper_private_obj_duplicate_state(obj,

&group_state->base);

(quoted diff) +2 -0
+
+       for_each_tunnel_state(to_group_state(obj->state),

tunnel_state) {

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+               struct drm_dp_tunnel_state *new_tunnel_state;
+
+               new_tunnel_state =

get_or_add_tunnel_state(group_state,
+
tunnel_state->tunnel_ref.tunnel);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+               if (!new_tunnel_state)
+                       goto out_free_state;
+
+               new_tunnel_state->stream_mask = tunnel_state-
Quoted 1 line

stream_mask;

+ new_tunnel_state->stream_bw = kmemdup(tunnel_state-

Quoted 1 line

stream_bw,

+
sizeof(*tunnel_state->stream_bw) *
+ hweight32(tun
nel_state->stream_mask),

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+                                                     GFP_KERNEL);
+
+               if (!new_tunnel_state->stream_bw)
+                       goto out_free_state;
+       }
+
+       return &group_state->base;
+
+out_free_state:
+       clear_tunnel_group_state(group_state);
+       kfree(group_state);
+
+       return NULL;
+}
+
+static void tunnel_group_destroy_state(struct drm_private_obj *obj,

struct drm_private_state *state)

(quoted diff) +2 -0
+{
+       struct drm_dp_tunnel_group_state *group_state =

to_group_state(state);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +32 -0
+
+       clear_tunnel_group_state(group_state);
+       kfree(group_state);
+}
+
+static const struct drm_private_state_funcs tunnel_group_funcs = {
+       .atomic_duplicate_state = tunnel_group_duplicate_state,
+       .atomic_destroy_state = tunnel_group_destroy_state,
+};
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+                              struct drm_dp_tunnel *tunnel)
+{
+       struct drm_dp_tunnel_group_state *group_state =
+               drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       if (IS_ERR(group_state))
+               return ERR_CAST(group_state);
+
+       tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
+       if (!tunnel_state)
+               return ERR_PTR(-ENOMEM);
+
+       return tunnel_state;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+                                  const struct drm_dp_tunnel

*tunnel)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+       struct drm_dp_tunnel_group_state *new_group_state;
+       int i;
+
+       for_each_new_group_in_state(state, new_group_state, i)
+               if (to_group(new_group_state->base.obj) == tunnel-
Quoted 1 line

group)

+ return get_tunnel_state(new_group_state,
tunnel);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+       return NULL;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
+
+static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct

drm_dp_tunnel_group *group)

(quoted diff) +2 -0
+{
+       struct drm_dp_tunnel_group_state *group_state =

kzalloc(sizeof(*group_state), GFP_KERNEL);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+
+       if (!group_state)
+               return false;
+
+       INIT_LIST_HEAD(&group_state->tunnel_states);
+
+       group->mgr = mgr;
+       group->available_bw = -1;
+       INIT_LIST_HEAD(&group->tunnels);
+
+       drm_atomic_private_obj_init(mgr->dev, &group->base,

&group_state->base,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+                                   &tunnel_group_funcs);
+
+       return true;
+}
+
+static void cleanup_group(struct drm_dp_tunnel_group *group)
+{
+       drm_atomic_private_obj_fini(&group->base);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void check_unique_stream_ids(const struct

drm_dp_tunnel_group_state *group_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+{
+       const struct drm_dp_tunnel_state *tunnel_state;
+       u32 stream_mask = 0;
+
+       for_each_tunnel_state(group_state, tunnel_state) {
+               drm_WARN(to_group(group_state->base.obj)->mgr->dev,
+                        tunnel_state->stream_mask & stream_mask,
+                        "[DPTUN %s]: conflicting stream IDs %x (IDs

in other tunnels %x)\n",

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+                        tunnel_state->tunnel_ref.tunnel->name,
+                        tunnel_state->stream_mask,
+                        stream_mask);
+
+               stream_mask |= tunnel_state->stream_mask;
+       }
+}
+#else
+static void check_unique_stream_ids(const struct

drm_dp_tunnel_group_state *group_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+{
+}
+#endif
+
+static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
+{
+       return hweight32(stream_mask & (BIT(stream_id) - 1));
+}
+
+static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
+                          unsigned long old_mask, unsigned long

new_mask)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+{
+       unsigned long move_mask = old_mask & new_mask;
+       int *new_bws = NULL;
+       int id;
+
+       WARN_ON(!new_mask);
+
+       if (old_mask == new_mask)
+               return 0;
+
+       new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws),

GFP_KERNEL);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+       if (!new_bws)
+               return -ENOMEM;
+
+       for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask))
+               new_bws[stream_id_to_idx(new_mask, id)] =
+                       tunnel_state-
Quoted 1 line

stream_bw[stream_id_to_idx(old_mask, id)];

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+
+       kfree(tunnel_state->stream_bw);
+       tunnel_state->stream_bw = new_bws;
+       tunnel_state->stream_mask = new_mask;
+
+       return 0;
+}
+
+static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+                        u8 stream_id, int bw)
+{
+       int err;
+
+       err = resize_bw_array(tunnel_state,
+                             tunnel_state->stream_mask,
+                             tunnel_state->stream_mask |

BIT(stream_id));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+       if (err)
+               return err;
+
+       tunnel_state->stream_bw[stream_id_to_idx(tunnel_state-
Quoted 1 line

stream_mask, stream_id)] = bw;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+
+       return 0;
+}
+
+static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+                          u8 stream_id)
+{
+       if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
+               drm_dp_tunnel_atomic_clear_state(tunnel_state);
+               return 0;
+       }
+
+       return resize_bw_array(tunnel_state,
+                              tunnel_state->stream_mask,
+                              tunnel_state->stream_mask &

~BIT(stream_id));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+}
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state

*state,
+ struct drm_dp_tunnel
*tunnel,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+                                        u8 stream_id, int bw)
+{
+       struct drm_dp_tunnel_group_state *new_group_state =
+               drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+       struct drm_dp_tunnel_state *tunnel_state;
+       int err;
+
+       if (drm_WARN_ON(tunnel->group->mgr->dev,
+                       stream_id > BITS_PER_TYPE(tunnel_state-
Quoted 1 line

stream_mask)))

drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+               return -EINVAL;
+
+       tun_dbg(tunnel,
+               "Setting %d Mb/s for stream %d\n",
+               DPTUN_BW_ARG(bw), stream_id);
+
+       if (bw == 0) {
+               tunnel_state = get_tunnel_state(new_group_state,

tunnel);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +7 -0
+               if (!tunnel_state)
+                       return 0;
+
+               return clear_stream_bw(tunnel_state, stream_id);
+       }
+
+       tunnel_state = get_or_add_tunnel_state(new_group_state,

tunnel);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +14 -0
+       if (drm_WARN_ON(state->dev, !tunnel_state))
+               return -EINVAL;
+
+       err = set_stream_bw(tunnel_state, stream_id, bw);
+       if (err)
+               return err;
+
+       check_unique_stream_ids(new_group_state);
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct

drm_dp_tunnel_state *tunnel_state)

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+{
+       int tunnel_bw = 0;
+       int i;
+
+       for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
+               tunnel_bw += tunnel_state->stream_bw[i];
+
+       return tunnel_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_tunnel_bw);
+
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct

drm_atomic_state *state,
+ const struct
drm_dp_tunnel *tunnel,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +19 -0
+                                                   u32 *stream_mask)
+{
+       struct drm_dp_tunnel_group_state *group_state =
+               drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+       struct drm_dp_tunnel_state *tunnel_state;
+
+       if (IS_ERR(group_state))
+               return PTR_ERR(group_state);
+
+       *stream_mask = 0;
+       for_each_tunnel_state(group_state, tunnel_state)
+               *stream_mask |= tunnel_state->stream_mask;
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
+
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state

*new_group_state,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +3 -0
+                                   u32 *failed_stream_mask)
+{
+       struct drm_dp_tunnel_group *group = to_group(new_group_state-
Quoted 1 line

base.obj);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+       struct drm_dp_tunnel_state *new_tunnel_state;
+       u32 group_stream_mask = 0;
+       int group_bw = 0;
+
+       for_each_tunnel_state(new_group_state, new_tunnel_state) {
+               struct drm_dp_tunnel *tunnel = new_tunnel_state-
Quoted 1 line

tunnel_ref.tunnel;

(quoted diff) +2 -0
+               int max_dprx_bw = get_max_dprx_bw(tunnel);
+               int tunnel_bw =

drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+
+               tun_dbg(tunnel,
+                       "%sRequired %d/%d Mb/s total for tunnel.\n",
+                       tunnel_bw > max_dprx_bw ? "Not enough BW: " :

"",

drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+                       DPTUN_BW_ARG(tunnel_bw),
+                       DPTUN_BW_ARG(max_dprx_bw));
+
+               if (tunnel_bw > max_dprx_bw) {
+                       *failed_stream_mask = new_tunnel_state-
Quoted 1 line

stream_mask;

drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+                       return -ENOSPC;
+               }
+
+               group_bw += min(roundup(tunnel_bw, tunnel-
Quoted 1 line

bw_granularity),

drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+                               max_dprx_bw);
+               group_stream_mask |= new_tunnel_state->stream_mask;
+       }
+
+       tun_grp_dbg(group,
+                   "%sRequired %d/%d Mb/s total for tunnel

group.\n",
+ group_bw > group->available_bw ? "Not enough BW:
" : "",

drivers/gpu/drm/display/drm_dp_tunnel.c new file +12 -0
+                   DPTUN_BW_ARG(group_bw),
+                   DPTUN_BW_ARG(group->available_bw));
+
+       if (group_bw > group->available_bw) {
+               *failed_stream_mask = group_stream_mask;
+               return -ENOSPC;
+       }
+
+       return 0;
+}
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state

*state,

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+                                         u32 *failed_stream_mask)
+{
+       struct drm_dp_tunnel_group_state *new_group_state;
+       int i;
+
+       for_each_new_group_in_state(state, new_group_state, i) {
+               int ret;
+
+               ret =

drm_dp_tunnel_atomic_check_group_bw(new_group_state, +
failed_stream_mask);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +15 -0
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
+
+static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
+{
+       int i;
+
+       for (i = 0; i < mgr->group_count; i++) {
+               cleanup_group(&mgr->groups[i]);
+               drm_WARN_ON(mgr->dev, !list_empty(&mgr-
Quoted 1 line

groups[i].tunnels));

drivers/gpu/drm/display/drm_dp_tunnel.c new file +17 -0
+       }
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       ref_tracker_dir_exit(&mgr->ref_tracker);
+#endif
+
+       kfree(mgr->groups);
+       kfree(mgr);
+}
+
+/**
+ * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
+ * @i915: i915 driver object
+ *
+ * Creates a DP tunnel manager.
+ *
+ * Returns a pointer to the tunnel manager if created successfully

or NULL in
+ * case of an error.
+ */

(quoted diff) +2 -0
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int

max_group_count)

(quoted diff) +2 -0
+{
+       struct drm_dp_tunnel_mgr *mgr = kzalloc(sizeof(*mgr),

GFP_KERNEL);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+       int i;
+
+       if (!mgr)
+               return NULL;
+
+       mgr->dev = dev;
+       init_waitqueue_head(&mgr->bw_req_queue);
+
+       mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups),

GFP_KERNEL);

drivers/gpu/drm/display/drm_dp_tunnel.c new file +35 -0
+       if (!mgr->groups) {
+               kfree(mgr);
+
+               return NULL;
+       }
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
+#endif
+
+       for (i = 0; i < max_group_count; i++) {
+               if (!init_group(mgr, &mgr->groups[i])) {
+                       destroy_mgr(mgr);
+
+                       return NULL;
+               }
+
+               mgr->group_count++;
+       }
+
+       return mgr;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
+
+/**
+ * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
+ * @mgr: Tunnel manager object
+ *
+ * Destroy the tunnel manager.
+ */
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
+{
+       destroy_mgr(mgr);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);
include/drm/display/drm_dp.h +28 -0
index 281afff6ee4e5..8bfd5d007be8d 100644
@@ -1382,6 +1382,66 @@
 #define DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET     0x69494
 #define DP_HDCP_2_2_REG_DBG_OFFSET             0x69518
 
+/* DP-tunneling */
+#define DP_TUNNELING_OUI                               0xe0000
+#define  DP_TUNNELING_OUI_BYTES                                3
+
+#define DP_TUNNELING_DEV_ID                            0xe0003
+#define  DP_TUNNELING_DEV_ID_BYTES                     6
+
+#define DP_TUNNELING_HW_REV                            0xe0009
+#define  DP_TUNNELING_HW_REV_MAJOR_SHIFT               4
+#define  DP_TUNNELING_HW_REV_MAJOR_MASK                        (0xf
 << DP_TUNNELING_HW_REV_MAJOR_SHIFT)
+#define  DP_TUNNELING_HW_REV_MINOR_SHIFT               0
+#define  DP_TUNNELING_HW_REV_MINOR_MASK                        (0xf
 << DP_TUNNELING_HW_REV_MINOR_SHIFT)
+
+#define DP_TUNNELING_SW_REV_MAJOR                      0xe000a
+#define DP_TUNNELING_SW_REV_MINOR                      0xe000b
+
+#define DP_TUNNELING_CAPABILITIES                      0xe000d
+#define  DP_IN_BW_ALLOCATION_MODE_SUPPORT              (1 << 7)
+#define  DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT          (1 << 6)
+#define  DP_TUNNELING_SUPPORT                          (1 << 0)
+
+#define DP_IN_ADAPTER_INFO                             0xe000e
+#define  DP_IN_ADAPTER_NUMBER_BITS                     7
+#define  DP_IN_ADAPTER_NUMBER_MASK                     ((1 <<
 DP_IN_ADAPTER_NUMBER_BITS) - 1)
+
+#define DP_USB4_DRIVER_ID                              0xe000f
+#define  DP_USB4_DRIVER_ID_BITS                                4
+#define  DP_USB4_DRIVER_ID_MASK                                ((1

<< DP_USB4_DRIVER_ID_BITS) - 1)

include/drm/display/drm_dp.h +7 -0
+
+#define DP_USB4_DRIVER_BW_CAPABILITY                   0xe0020
+#define  DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT     (1 << 7)
+
+#define DP_IN_ADAPTER_TUNNEL_INFORMATION               0xe0021
+#define  DP_GROUP_ID_BITS                              3
+#define  DP_GROUP_ID_MASK                              ((1 <<

DP_GROUP_ID_BITS) - 1)

include/drm/display/drm_dp.h +5 -0
+
+#define DP_BW_GRANULARITY                              0xe0022
+#define  DP_BW_GRANULARITY_MASK                                0x3
+
+#define

DP_ESTIMATED_BW 0xe0023
+#define
DP_ALLOCATED_BW 0xe0024

include/drm/display/drm_dp.h +19 -0
+
+#define DP_TUNNELING_STATUS                            0xe0025
+#define  DP_BW_ALLOCATION_CAPABILITY_CHANGED           (1 << 3)
+#define  DP_ESTIMATED_BW_CHANGED                       (1 << 2)
+#define  DP_BW_REQUEST_SUCCEEDED                       (1 << 1)
+#define  DP_BW_REQUEST_FAILED                          (1 << 0)
+
+#define DP_TUNNELING_MAX_LINK_RATE                     0xe0028
+
+#define DP_TUNNELING_MAX_LANE_COUNT                    0xe0029
+#define  DP_TUNNELING_MAX_LANE_COUNT_MASK              0x1f
+
+#define DP_DPTX_BW_ALLOCATION_MODE_CONTROL             0xe0030
+#define  DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE   (1 << 7)
+#define  DP_UNMASK_BW_ALLOCATION_IRQ                   (1 << 6)
+
+#define DP_REQUEST_BW                                  0xe0031
+#define  MAX_DP_REQUEST_BW                             255
+
 /* LTTPR: Link Training (LT)-tunable PHY Repeaters */
 #define DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV 0xf0000

/* 1.3 */
#define DP_MAX_LINK_RATE_PHY_REPEATER 0xf0001
/* 1.4a */

include/drm/display/drm_dp_tunnel.h new file +38 -0
new file mode 100644
index 0000000000000..f6449b1b4e6e9
@@ -0,0 +1,270 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __DRM_DP_TUNNEL_H__
+#define __DRM_DP_TUNNEL_H__
+
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+
+struct drm_dp_aux;
+
+struct drm_device;
+
+struct drm_atomic_state;
+struct drm_dp_tunnel_mgr;
+struct drm_dp_tunnel_state;
+
+struct ref_tracker;
+
+struct drm_dp_tunnel_ref {
+       struct drm_dp_tunnel *tunnel;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+       struct ref_tracker *tracker;
+#endif
+};
+
+#ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel);
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel);
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker

**tracker);

include/drm/display/drm_dp_tunnel.h new file +3 -0
+
+void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker

**tracker);

include/drm/display/drm_dp_tunnel.h new file +10 -0
+#else
+#define drm_dp_tunnel_get(tunnel, tracker) \
+       drm_dp_tunnel_get_untracked(tunnel)
+
+#define drm_dp_tunnel_put(tunnel, tracker) \
+       drm_dp_tunnel_put_untracked(tunnel)
+
+#endif
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel

*tunnel,
+ struct drm_dp_tunnel_ref
*tunnel_ref)

include/drm/display/drm_dp_tunnel.h new file +2 -0
+{
+       tunnel_ref->tunnel = drm_dp_tunnel_get(tunnel, &tunnel_ref-
Quoted 1 line

tracker);

include/drm/display/drm_dp_tunnel.h new file +3 -0
+}
+
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref

*tunnel_ref)

include/drm/display/drm_dp_tunnel.h new file +12 -0
+{
+       drm_dp_tunnel_put(tunnel_ref->tunnel, &tunnel_ref->tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+                    struct drm_dp_aux *aux);
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel);
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel

*tunnel);

include/drm/display/drm_dp_tunnel.h new file +11 -0
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw);
+int drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+                            struct drm_dp_aux *aux);
+
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel

*tunnel);

include/drm/display/drm_dp_tunnel.h new file +10 -0
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel);
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+                              struct drm_dp_tunnel *tunnel);
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+                                  const struct drm_dp_tunnel

*tunnel);

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state

*tunnel_state);

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state

*state,

include/drm/display/drm_dp_tunnel.h new file +3 -0
+                                      struct drm_dp_tunnel *tunnel,
+                                      u8 stream_id, int bw);
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct

drm_atomic_state *state,
+ const struct
drm_dp_tunnel *tunnel,
+ u32
*stream_mask);

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state

*state,

include/drm/display/drm_dp_tunnel.h new file +3 -0
+                                         u32 *failed_stream_mask);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct

drm_dp_tunnel_state *tunnel_state);

include/drm/display/drm_dp_tunnel.h new file +3 -0
+
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int

max_group_count);

include/drm/display/drm_dp_tunnel.h new file +15 -0
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr);
+
+#else
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+       return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker

**tracker)

include/drm/display/drm_dp_tunnel.h new file +6 -0
+{
+       return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker

**tracker) {}

include/drm/display/drm_dp_tunnel.h new file +2 -0
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel

*tunnel,
+ struct drm_dp_tunnel_ref
*tunnel_ref) {}
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref) {}

include/drm/display/drm_dp_tunnel.h new file +15 -0
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+                    struct drm_dp_aux *aux)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline int
+drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+       return 0;
+}
+
+static inline int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel

*tunnel)

include/drm/display/drm_dp_tunnel.h new file +5 -0
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int drm_dp_tunnel_disable_bw_alloc(struct

drm_dp_tunnel *tunnel)

include/drm/display/drm_dp_tunnel.h new file +5 -0
+{
+       return -EOPNOTSUPP;
+}
+
+static inline bool drm_dp_tunnel_bw_alloc_is_enabled(const struct

drm_dp_tunnel *tunnel)

include/drm/display/drm_dp_tunnel.h new file +23 -0
+{
+       return false;
+}
+
+static inline int
+drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel

*tunnel) {}

include/drm/display/drm_dp_tunnel.h new file +15 -0
+static inline int
+drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+                        struct drm_dp_aux *aux)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+       return 0;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel

*tunnel)

include/drm/display/drm_dp_tunnel.h new file +26 -0
+{
+       return 0;
+}
+
+static inline int
+drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+       return -1;
+}
+
+static inline const char *
+drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+       return NULL;
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+                              struct drm_dp_tunnel *tunnel)
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+                                  const struct drm_dp_tunnel

*tunnel)

include/drm/display/drm_dp_tunnel.h new file +6 -0
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline void
+drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state

*tunnel_state) {}

include/drm/display/drm_dp_tunnel.h new file +11 -0
+
+static inline int
+drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+                                  struct drm_dp_tunnel *tunnel,
+                                  u8 stream_id, int bw)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_group_streams_in_state(struct

drm_atomic_state *state,
+ const struct
drm_dp_tunnel *tunnel,

include/drm/display/drm_dp_tunnel.h new file +7 -0
+                                               u32 *stream_mask)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state

*state,

include/drm/display/drm_dp_tunnel.h new file +7 -0
+                                     u32 *failed_stream_mask)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state

*tunnel_state)

include/drm/display/drm_dp_tunnel.h new file +6 -0
+{
+       return 0;
+}
+
+static inline struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int

max_group_count)

include/drm/display/drm_dp_tunnel.h new file +11 -0
+{
+       return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr) {}
+
+
+#endif /* CONFIG_DRM_DISPLAY_DP_TUNNEL */
+
+#endif /* __DRM_DP_TUNNEL_H__ */
#8 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

In reply to Imre Deak
Quoted 54 lines · Imre Deak

Add support for Display Port DP tunneling. For now this includes the support for Bandwidth Allocation Mode, leaving adding Panel Replay support for later.

BWA allows using displays that share the same (Thunderbolt) link with their maximum resolution. Atm, this may not be possible due to the coarse granularity of partitioning the link BW among the displays on the
link: the BW allocation policy is in a SW/FW/HW component on the link (on Thunderbolt it's the SW or FW Connection Manager), independent of the driver. This policy will set the DPRX maximum rate and lane count DPCD registers the GFX driver will see (0x00000, 0x00001, 0x02200, 0x02201) based on the available link BW.

The granularity of the current BW allocation policy is course, based on the required link rate in the 1.62Gbs..8.1Gbps range and it may prevent using higher resolutions all together: the display connected first will get a share of the link BW which corresponds to its full DPRX capability (regardless of the actual mode it uses). A subsequent display connected will only get the remaining BW, which could be well below its full capability.

BWA solves the above course granularity (reducing it to a 250Mbs..1Gps range) and first-come/first-served issues by letting the driver request the BW for each display on a link which reflects the actual modes the displays use.

This patch adds the DRM core helper functions, while a follow-up change in the patchset takes them into use in the i915 driver.

---
 drivers/gpu/drm/display/Kconfig         |   17 +
 drivers/gpu/drm/display/Makefile        |    2 +
 drivers/gpu/drm/display/drm_dp_tunnel.c | 1715 +++++++++++++++++++++++
 include/drm/display/drm_dp.h            |   60 +
 include/drm/display/drm_dp_tunnel.h     |  270 ++++
 5 files changed, 2064 insertions(+)
 create mode 100644 drivers/gpu/drm/display/drm_dp_tunnel.c
 create mode 100644 include/drm/display/drm_dp_tunnel.h
drivers/gpu/drm/display/Kconfig +6 -0
index 09712b88a5b83..b024a84b94c1c 100644
@@ -17,6 +17,23 @@ config DRM_DISPLAY_DP_HELPER
 	help
 	  DRM display helpers for DisplayPort.
 
+config DRM_DISPLAY_DP_TUNNEL
+	bool
+	select DRM_DISPLAY_DP_HELPER
+	help
+	  Enable support for DisplayPort tunnels.
+

drivers/gpu/drm/display/Kconfig config DRM_DISPLAY_DP_HELPER · line 35

Quoted 10 lines
drivers/gpu/drm/display/Kconfig +10 -0
+config DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	bool "Enable debugging the DP tunnel state"
+	depends on REF_TRACKER
+	depends on DRM_DISPLAY_DP_TUNNEL
+	depends on DEBUG_KERNEL
+	depends on EXPERT
+	help
+	  Enables debugging the DP tunnel manager's status.
+
+	  If in doubt, say "N".

It's not exactly clear what a "DP tunnel" is. Shouldn't thunderbolt be mentioned here somewhere?

Quoted 137 lines
drivers/gpu/drm/display/Kconfig +1 -0
+
 config DRM_DISPLAY_HDCP_HELPER
 	bool
 	depends on DRM_DISPLAY_HELPER
drivers/gpu/drm/display/Makefile +2 -0
index 17ac4a1006a80..7ca61333c6696 100644
@@ -8,6 +8,8 @@ drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_HELPER) += \
 	drm_dp_helper.o \
 	drm_dp_mst_topology.o \
 	drm_dsc_helper.o
+drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_TUNNEL) += \
+	drm_dp_tunnel.o
 drm_display_helper-$(CONFIG_DRM_DISPLAY_HDCP_HELPER) += drm_hdcp_helper.o
 drm_display_helper-$(CONFIG_DRM_DISPLAY_HDMI_HELPER) += \
 	drm_hdmi_helper.o \
drivers/gpu/drm/display/drm_dp_tunnel.c new file +114 -0
new file mode 100644
index 0000000000000..58f6330db7d9d
@@ -0,0 +1,1715 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/ref_tracker.h>
+#include <linux/types.h>
+
+#include <drm/drm_atomic_state_helper.h>
+
+#include <drm/drm_atomic.h>
+#include <drm/drm_print.h>
+#include <drm/display/drm_dp.h>
+#include <drm/display/drm_dp_helper.h>
+#include <drm/display/drm_dp_tunnel.h>
+
+#define to_group(__private_obj) \
+	container_of(__private_obj, struct drm_dp_tunnel_group, base)
+
+#define to_group_state(__private_state) \
+	container_of(__private_state, struct drm_dp_tunnel_group_state, base)
+
+#define is_dp_tunnel_private_obj(__obj) \
+	((__obj)->funcs == &tunnel_group_funcs)
+
+#define for_each_new_group_in_state(__state, __new_group_state, __i) \
+	for ((__i) = 0; \
+	     (__i) < (__state)->num_private_objs; \
+	     (__i)++) \
+		for_each_if ((__state)->private_objs[__i].ptr && \
+			     is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \
+			     ((__new_group_state) = \
+				to_group_state((__state)->private_objs[__i].new_state), 1))
+
+#define for_each_old_group_in_state(__state, __old_group_state, __i) \
+	for ((__i) = 0; \
+	     (__i) < (__state)->num_private_objs; \
+	     (__i)++) \
+		for_each_if ((__state)->private_objs[__i].ptr && \
+			     is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \
+			     ((__old_group_state) = \
+				to_group_state((__state)->private_objs[__i].old_state), 1))
+
+#define for_each_tunnel_in_group(__group, __tunnel) \
+	list_for_each_entry(__tunnel, &(__group)->tunnels, node)
+
+#define for_each_tunnel_state(__group_state, __tunnel_state) \
+	list_for_each_entry(__tunnel_state, &(__group_state)->tunnel_states, node)
+
+#define for_each_tunnel_state_safe(__group_state, __tunnel_state, __tunnel_state_tmp) \
+	list_for_each_entry_safe(__tunnel_state, __tunnel_state_tmp, \
+				 &(__group_state)->tunnel_states, node)
+
+#define kbytes_to_mbits(__kbytes) \
+	DIV_ROUND_UP((__kbytes) * 8, 1000)
+
+#define DPTUN_BW_ARG(__bw) ((__bw) < 0 ? (__bw) : kbytes_to_mbits(__bw))
+
+#define __tun_prn(__tunnel, __level, __type, __fmt, ...) \
+	drm_##__level##__type((__tunnel)->group->mgr->dev, \
+			      "[DPTUN %s][%s] " __fmt, \
+			      drm_dp_tunnel_name(__tunnel), \
+			      (__tunnel)->aux->name, ## \
+			      __VA_ARGS__)
+
+#define tun_dbg(__tunnel, __fmt, ...) \
+	__tun_prn(__tunnel, dbg, _kms, __fmt, ## __VA_ARGS__)
+
+#define tun_dbg_stat(__tunnel, __err, __fmt, ...) do { \
+	if (__err) \
+		__tun_prn(__tunnel, dbg, _kms, __fmt " (Failed, err: %pe)\n", \
+			  ## __VA_ARGS__, ERR_PTR(__err)); \
+	else \
+		__tun_prn(__tunnel, dbg, _kms, __fmt " (Ok)\n", \
+			  ## __VA_ARGS__); \
+} while (0)
+
+#define tun_dbg_atomic(__tunnel, __fmt, ...) \
+	__tun_prn(__tunnel, dbg, _atomic, __fmt, ## __VA_ARGS__)
+
+#define tun_grp_dbg(__group, __fmt, ...) \
+	drm_dbg_kms((__group)->mgr->dev, \
+		    "[DPTUN %s] " __fmt, \
+		    drm_dp_tunnel_group_name(__group), ## \
+		    __VA_ARGS__)
+
+#define DP_TUNNELING_BASE DP_TUNNELING_OUI
+
+#define __DPTUN_REG_RANGE(start, size) \
+	GENMASK_ULL(start + size - 1, start)
+
+#define DPTUN_REG_RANGE(addr, size) \
+	__DPTUN_REG_RANGE((addr) - DP_TUNNELING_BASE, size)
+
+#define DPTUN_REG(addr) DPTUN_REG_RANGE(addr, 1)
+
+#define DPTUN_INFO_REG_MASK ( \
+	DPTUN_REG_RANGE(DP_TUNNELING_OUI, DP_TUNNELING_OUI_BYTES) | \
+	DPTUN_REG_RANGE(DP_TUNNELING_DEV_ID, DP_TUNNELING_DEV_ID_BYTES) | \
+	DPTUN_REG(DP_TUNNELING_HW_REV) | \
+	DPTUN_REG(DP_TUNNELING_SW_REV_MAJOR) | \
+	DPTUN_REG(DP_TUNNELING_SW_REV_MINOR) | \
+	DPTUN_REG(DP_TUNNELING_CAPABILITIES) | \
+	DPTUN_REG(DP_IN_ADAPTER_INFO) | \
+	DPTUN_REG(DP_USB4_DRIVER_ID) | \
+	DPTUN_REG(DP_USB4_DRIVER_BW_CAPABILITY) | \
+	DPTUN_REG(DP_IN_ADAPTER_TUNNEL_INFORMATION) | \
+	DPTUN_REG(DP_BW_GRANULARITY) | \
+	DPTUN_REG(DP_ESTIMATED_BW) | \
+	DPTUN_REG(DP_ALLOCATED_BW) | \
+	DPTUN_REG(DP_TUNNELING_MAX_LINK_RATE) | \
+	DPTUN_REG(DP_TUNNELING_MAX_LANE_COUNT) | \
+	DPTUN_REG(DP_DPTX_BW_ALLOCATION_MODE_CONTROL))
+

drivers/gpu/drm/display/drm_dp_tunnel.c line 124

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+static const DECLARE_BITMAP(dptun_info_regs, 64) = {
+	DPTUN_INFO_REG_MASK & -1UL,
+#if BITS_PER_LONG == 32
+	DPTUN_INFO_REG_MASK >> 32,
+#endif
+};
+
+struct drm_dp_tunnel_regs {
+	u8 buf[HWEIGHT64(DPTUN_INFO_REG_MASK)];
+};

That seems to be some kind of thing to allow us to store the values for non-consecutive DPCD registers in a contiguous non-sparse array? How much memory are we actually saving here as opposed to just using the full sized array?

Wasn't really expecting this kind of thing in here...

Quoted 103 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +103 -0
+
+struct drm_dp_tunnel_group;
+
+struct drm_dp_tunnel {
+	struct drm_dp_tunnel_group *group;
+
+	struct list_head node;
+
+	struct kref kref;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker *tracker;
+#endif
+	struct drm_dp_aux *aux;
+	char name[8];
+
+	int bw_granularity;
+	int estimated_bw;
+	int allocated_bw;
+
+	int max_dprx_rate;
+	u8 max_dprx_lane_count;
+
+	u8 adapter_id;
+
+	bool bw_alloc_supported:1;
+	bool bw_alloc_enabled:1;
+	bool has_io_error:1;
+	bool destroyed:1;
+};
+
+struct drm_dp_tunnel_group_state;
+
+struct drm_dp_tunnel_state {
+	struct drm_dp_tunnel_group_state *group_state;
+
+	struct drm_dp_tunnel_ref tunnel_ref;
+
+	struct list_head node;
+
+	u32 stream_mask;
+	int *stream_bw;
+};
+
+struct drm_dp_tunnel_group_state {
+	struct drm_private_state base;
+
+	struct list_head tunnel_states;
+};
+
+struct drm_dp_tunnel_group {
+	struct drm_private_obj base;
+	struct drm_dp_tunnel_mgr *mgr;
+
+	struct list_head tunnels;
+
+	int available_bw;	/* available BW including the allocated_bw of all tunnels */
+	int drv_group_id;
+
+	char name[8];
+
+	bool active:1;
+};
+
+struct drm_dp_tunnel_mgr {
+	struct drm_device *dev;
+
+	int group_count;
+	struct drm_dp_tunnel_group *groups;
+	wait_queue_head_t bw_req_queue;
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker_dir ref_tracker;
+#endif
+};
+
+static int next_reg_area(int *offset)
+{
+	*offset = find_next_bit(dptun_info_regs, 64, *offset);
+
+	return find_next_zero_bit(dptun_info_regs, 64, *offset + 1) - *offset;
+}
+
+#define tunnel_reg_ptr(__regs, __address) ({ \
+	WARN_ON(!test_bit((__address) - DP_TUNNELING_BASE, dptun_info_regs)); \
+	&(__regs)->buf[bitmap_weight(dptun_info_regs, (__address) - DP_TUNNELING_BASE)]; \
+})
+
+static int read_tunnel_regs(struct drm_dp_aux *aux, struct drm_dp_tunnel_regs *regs)
+{
+	int offset = 0;
+	int len;
+
+	while ((len = next_reg_area(&offset))) {
+		int address = DP_TUNNELING_BASE + offset;
+
+		if (drm_dp_dpcd_read(aux, address, tunnel_reg_ptr(regs, address), len) < 0)
+			return -EIO;
+
+		offset += len;
+	}
+
+	return 0;
+}

drivers/gpu/drm/display/drm_dp_tunnel.c line 237

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+
+static u8 tunnel_reg(const struct drm_dp_tunnel_regs *regs, int address)
+{
+	return *tunnel_reg_ptr(regs, address);
+}
+
+static int tunnel_reg_drv_group_id(const struct drm_dp_tunnel_regs *regs)
+{
+	int drv_id = tunnel_reg(regs, DP_USB4_DRIVER_ID) & DP_USB4_DRIVER_ID_MASK;
+	int group_id = tunnel_reg(regs, DP_IN_ADAPTER_TUNNEL_INFORMATION) & DP_GROUP_ID_MASK;

Maybe these things should be u8/etc. everywhere? Would at least indicate that I don't need to look for where negative values are handled...

Quoted 89 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +89 -0
+
+	if (!group_id)
+		return 0;
+
+	return (drv_id << DP_GROUP_ID_BITS) | group_id;
+}
+
+/* Return granularity in kB/s units */
+static int tunnel_reg_bw_granularity(const struct drm_dp_tunnel_regs *regs)
+{
+	int gr = tunnel_reg(regs, DP_BW_GRANULARITY) & DP_BW_GRANULARITY_MASK;
+
+	WARN_ON(gr > 2);
+
+	return (250000 << gr) / 8;
+}
+
+static int tunnel_reg_max_dprx_rate(const struct drm_dp_tunnel_regs *regs)
+{
+	u8 bw_code = tunnel_reg(regs, DP_TUNNELING_MAX_LINK_RATE);
+
+	return drm_dp_bw_code_to_link_rate(bw_code);
+}
+
+static int tunnel_reg_max_dprx_lane_count(const struct drm_dp_tunnel_regs *regs)
+{
+	u8 lane_count = tunnel_reg(regs, DP_TUNNELING_MAX_LANE_COUNT) &
+			DP_TUNNELING_MAX_LANE_COUNT_MASK;
+
+	return lane_count;
+}
+
+static bool tunnel_reg_bw_alloc_supported(const struct drm_dp_tunnel_regs *regs)
+{
+	u8 cap_mask = DP_TUNNELING_SUPPORT | DP_IN_BW_ALLOCATION_MODE_SUPPORT;
+
+	if ((tunnel_reg(regs, DP_TUNNELING_CAPABILITIES) & cap_mask) != cap_mask)
+		return false;
+
+	return tunnel_reg(regs, DP_USB4_DRIVER_BW_CAPABILITY) &
+	       DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT;
+}
+
+static bool tunnel_reg_bw_alloc_enabled(const struct drm_dp_tunnel_regs *regs)
+{
+	return tunnel_reg(regs, DP_DPTX_BW_ALLOCATION_MODE_CONTROL) &
+		DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE;
+}
+
+static int tunnel_group_drv_id(int drv_group_id)
+{
+	return drv_group_id >> DP_GROUP_ID_BITS;
+}
+
+static int tunnel_group_id(int drv_group_id)
+{
+	return drv_group_id & DP_GROUP_ID_MASK;
+}
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->name;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_name);
+
+static const char *drm_dp_tunnel_group_name(const struct drm_dp_tunnel_group *group)
+{
+	return group->name;
+}
+
+static struct drm_dp_tunnel_group *
+lookup_or_alloc_group(struct drm_dp_tunnel_mgr *mgr, int drv_group_id)
+{
+	struct drm_dp_tunnel_group *group = NULL;
+	int i;
+
+	for (i = 0; i < mgr->group_count; i++) {
+		/*
+		 * A tunnel group with 0 group ID shouldn't have more than one
+		 * tunnels.
+		 */
+		if (tunnel_group_id(drv_group_id) &&
+		    mgr->groups[i].drv_group_id == drv_group_id)
+			return &mgr->groups[i];
+
+		if (!group && !mgr->groups[i].active)
+			group = &mgr->groups[i];
+	}
+

drivers/gpu/drm/display/drm_dp_tunnel.c line 336

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+	if (!group) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Can't allocate more tunnel groups\n");
+		return NULL;
+	}
+
+	group->drv_group_id = drv_group_id;
+	group->active = true;
+
+	snprintf(group->name, sizeof(group->name), "%d:%d:*",

What does the '*' indicate?

Quoted 56 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +56 -0
+		 tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
+		 tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1));
+
+	return group;
+}
+
+static void free_group(struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel_mgr *mgr = group->mgr;
+
+	if (drm_WARN_ON(mgr->dev, !list_empty(&group->tunnels)))
+		return;
+
+	group->drv_group_id = 0;
+	group->available_bw = -1;
+	group->active = false;
+}
+
+static struct drm_dp_tunnel *
+tunnel_get(struct drm_dp_tunnel *tunnel)
+{
+	kref_get(&tunnel->kref);
+
+	return tunnel;
+}
+
+static void free_tunnel(struct kref *kref)
+{
+	struct drm_dp_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref);
+	struct drm_dp_tunnel_group *group = tunnel->group;
+
+	list_del(&tunnel->node);
+	if (list_empty(&group->tunnels))
+		free_group(group);
+
+	kfree(tunnel);
+}
+
+static void tunnel_put(struct drm_dp_tunnel *tunnel)
+{
+	kref_put(&tunnel->kref, free_tunnel);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void track_tunnel_ref(struct drm_dp_tunnel *tunnel,
+			     struct ref_tracker **tracker)
+{
+	ref_tracker_alloc(&tunnel->group->mgr->ref_tracker,
+			  tracker, GFP_KERNEL);
+}
+
+static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
+			       struct ref_tracker **tracker)
+{
+	ref_tracker_free(&tunnel->group->mgr->ref_tracker,
+			 tracker);

drivers/gpu/drm/display/drm_dp_tunnel.c line 402

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	track_tunnel_ref(tunnel, NULL);
+
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);

Why do these exist?

Quoted 614 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +614 -0
+
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel)
+{
+	tunnel_put(tunnel);
+	untrack_tunnel_ref(tunnel, NULL);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put_untracked);
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel,
+		    struct ref_tracker **tracker)
+{
+	track_tunnel_ref(tunnel, tracker);
+
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get);
+
+void drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel,
+			 struct ref_tracker **tracker)
+{
+	untrack_tunnel_ref(tunnel, tracker);
+	tunnel_put(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put);
+#else
+#define track_tunnel_ref(tunnel, tracker) do {} while (0)
+#define untrack_tunnel_ref(tunnel, tracker) do {} while (0)
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);
+
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel)
+{
+	tunnel_put(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put_untracked);
+#endif
+
+static bool add_tunnel_to_group(struct drm_dp_tunnel_mgr *mgr,
+				int drv_group_id,
+				struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group *group =
+		lookup_or_alloc_group(mgr, drv_group_id);
+
+	if (!group)
+		return false;
+
+	tunnel->group = group;
+	list_add(&tunnel->node, &group->tunnels);
+
+	return true;
+}
+
+static struct drm_dp_tunnel *
+create_tunnel(struct drm_dp_tunnel_mgr *mgr,
+	      struct drm_dp_aux *aux,
+	      const struct drm_dp_tunnel_regs *regs)
+{
+	int drv_group_id = tunnel_reg_drv_group_id(regs);
+	struct drm_dp_tunnel *tunnel;
+
+	tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
+	if (!tunnel)
+		return NULL;
+
+	INIT_LIST_HEAD(&tunnel->node);
+
+	kref_init(&tunnel->kref);
+
+	tunnel->aux = aux;
+
+	tunnel->adapter_id = tunnel_reg(regs, DP_IN_ADAPTER_INFO) & DP_IN_ADAPTER_NUMBER_MASK;
+
+	snprintf(tunnel->name, sizeof(tunnel->name), "%d:%d:%d",
+		 tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
+		 tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1),
+		 tunnel->adapter_id & ((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1));
+
+	tunnel->bw_granularity = tunnel_reg_bw_granularity(regs);
+	tunnel->allocated_bw = tunnel_reg(regs, DP_ALLOCATED_BW) *
+			       tunnel->bw_granularity;
+
+	tunnel->bw_alloc_supported = tunnel_reg_bw_alloc_supported(regs);
+	tunnel->bw_alloc_enabled = tunnel_reg_bw_alloc_enabled(regs);
+
+	if (!add_tunnel_to_group(mgr, drv_group_id, tunnel)) {
+		kfree(tunnel);
+
+		return NULL;
+	}
+
+	track_tunnel_ref(tunnel, &tunnel->tracker);
+
+	return tunnel;
+}
+
+static void destroy_tunnel(struct drm_dp_tunnel *tunnel)
+{
+	untrack_tunnel_ref(tunnel, &tunnel->tracker);
+	tunnel_put(tunnel);
+}
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel)
+{
+	tunnel->has_io_error = true;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_set_io_error);
+
+static char yes_no_chr(int val)
+{
+	return val ? 'Y' : 'N';
+}
+
+#define SKIP_DPRX_CAPS_CHECK		BIT(0)
+#define ALLOW_ALLOCATED_BW_CHANGE	BIT(1)
+
+static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr,
+				  const struct drm_dp_tunnel_regs *regs,
+				  unsigned int flags)
+{
+	int drv_group_id = tunnel_reg_drv_group_id(regs);
+	bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK);
+	bool ret = true;
+
+	if (!tunnel_reg_bw_alloc_supported(regs)) {
+		if (tunnel_group_id(drv_group_id)) {
+			drm_dbg_kms(mgr->dev,
+				    "DPTUN: A non-zero group ID is only allowed with BWA support\n");
+			ret = false;
+		}
+
+		if (tunnel_reg(regs, DP_ALLOCATED_BW)) {
+			drm_dbg_kms(mgr->dev,
+				    "DPTUN: BW is allocated without BWA support\n");
+			ret = false;
+		}
+
+		return ret;
+	}
+
+	if (!tunnel_group_id(drv_group_id)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: BWA support requires a non-zero group ID\n");
+		ret = false;
+	}
+
+	if (check_dprx && hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Invalid DPRX lane count: %d\n",
+			    tunnel_reg_max_dprx_lane_count(regs));
+
+		ret = false;
+	}
+
+	if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: DPRX rate is 0\n");
+
+		ret = false;
+	}
+
+	if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs, DP_ESTIMATED_BW)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Allocated BW %d > estimated BW %d Mb/s\n",
+			    DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) *
+					 tunnel_reg_bw_granularity(regs)),
+			    DPTUN_BW_ARG(tunnel_reg(regs, DP_ESTIMATED_BW) *
+					 tunnel_reg_bw_granularity(regs)));
+
+		ret = false;
+	}
+
+	return ret;
+}
+
+static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel *tunnel,
+					  const struct drm_dp_tunnel_regs *regs,
+					  unsigned int flags)
+{
+	int new_drv_group_id = tunnel_reg_drv_group_id(regs);
+	bool ret = true;
+
+	if (tunnel->bw_alloc_supported != tunnel_reg_bw_alloc_supported(regs)) {
+		tun_dbg(tunnel,
+			"BW alloc support has changed %c -> %c\n",
+			yes_no_chr(tunnel->bw_alloc_supported),
+			yes_no_chr(tunnel_reg_bw_alloc_supported(regs)));
+
+		ret = false;
+	}
+
+	if (tunnel->group->drv_group_id != new_drv_group_id) {
+		tun_dbg(tunnel,
+			"Driver/group ID has changed %d:%d:* -> %d:%d:*\n",
+			tunnel_group_drv_id(tunnel->group->drv_group_id),
+			tunnel_group_id(tunnel->group->drv_group_id),
+			tunnel_group_drv_id(new_drv_group_id),
+			tunnel_group_id(new_drv_group_id));
+
+		ret = false;
+	}
+
+	if (!tunnel->bw_alloc_supported)
+		return ret;
+
+	if (tunnel->bw_granularity != tunnel_reg_bw_granularity(regs)) {
+		tun_dbg(tunnel,
+			"BW granularity has changed: %d -> %d Mb/s\n",
+			DPTUN_BW_ARG(tunnel->bw_granularity),
+			DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs)));
+
+		ret = false;
+	}
+
+	/*
+	 * On some devices at least the BW alloc mode enabled status is always
+	 * reported as 0, so skip checking that here.
+	 */
+
+	if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) &&
+	    tunnel->allocated_bw !=
+	    tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity) {
+		tun_dbg(tunnel,
+			"Allocated BW has changed: %d -> %d Mb/s\n",
+			DPTUN_BW_ARG(tunnel->allocated_bw),
+			DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity));
+
+		ret = false;
+	}
+
+	return ret;
+}
+
+static int
+read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel,
+			    struct drm_dp_tunnel_regs *regs,
+			    unsigned int flags)
+{
+	int err;
+
+	err = read_tunnel_regs(tunnel->aux, regs);
+	if (err < 0) {
+		drm_dp_tunnel_set_io_error(tunnel);
+
+		return err;
+	}
+
+	if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags))
+		return -EINVAL;
+
+	if (!tunnel_info_changes_are_valid(tunnel, regs, flags))
+		return -EINVAL;
+
+	return 0;
+}
+
+static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const struct drm_dp_tunnel_regs *regs)
+{
+	bool changed = false;
+
+	if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate) {
+		tunnel->max_dprx_rate = tunnel_reg_max_dprx_rate(regs);
+		changed = true;
+	}
+
+	if (tunnel_reg_max_dprx_lane_count(regs) != tunnel->max_dprx_lane_count) {
+		tunnel->max_dprx_lane_count = tunnel_reg_max_dprx_lane_count(regs);
+		changed = true;
+	}
+
+	return changed;
+}
+
+static int dev_id_len(const u8 *dev_id, int max_len)
+{
+	while (max_len && dev_id[max_len - 1] == '\0')
+		max_len--;
+
+	return max_len;
+}
+
+static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel)
+{
+	int bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate,
+					   tunnel->max_dprx_lane_count);
+
+	return min(roundup(bw, tunnel->bw_granularity),
+		   MAX_DP_REQUEST_BW * tunnel->bw_granularity);
+}
+
+static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return min(get_max_dprx_bw(tunnel), tunnel->group->available_bw);
+}
+
+/**
+ * drm_dp_tunnel_detect - Detect DP tunnel on the link
+ * @mgr: Tunnel manager
+ * @aux: DP AUX on which the tunnel will be detected
+ *
+ * Detect if there is any DP tunnel on the link and add it to the tunnel
+ * group's tunnel list.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		       struct drm_dp_aux *aux)
+{
+	struct drm_dp_tunnel_regs regs;
+	struct drm_dp_tunnel *tunnel;
+	int err;
+
+	err = read_tunnel_regs(aux, &regs);
+	if (err)
+		return ERR_PTR(err);
+
+	if (!(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+	      DP_TUNNELING_SUPPORT))
+		return ERR_PTR(-ENODEV);
+
+	/* The DPRX caps are valid only after enabling BW alloc mode. */
+	if (!tunnel_regs_are_valid(mgr, &regs, SKIP_DPRX_CAPS_CHECK))
+		return ERR_PTR(-EINVAL);
+
+	tunnel = create_tunnel(mgr, aux, &regs);
+	if (!tunnel)
+		return ERR_PTR(-ENOMEM);
+
+	tun_dbg(tunnel,
+		"OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%c BWA-Sup:%c BWA-En:%c\n",
+		DP_TUNNELING_OUI_BYTES,
+			tunnel_reg_ptr(&regs, DP_TUNNELING_OUI),
+		dev_id_len(tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES),
+			tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID),
+		(tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MAJOR_MASK) >>
+			DP_TUNNELING_HW_REV_MAJOR_SHIFT,
+		(tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MINOR_MASK) >>
+			DP_TUNNELING_HW_REV_MINOR_SHIFT,
+		tunnel_reg(&regs, DP_TUNNELING_SW_REV_MAJOR),
+		tunnel_reg(&regs, DP_TUNNELING_SW_REV_MINOR),
+		yes_no_chr(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+			   DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT),
+		yes_no_chr(tunnel->bw_alloc_supported),
+		yes_no_chr(tunnel->bw_alloc_enabled));
+
+	return tunnel;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_detect);
+
+/**
+ * drm_dp_tunnel_destroy - Destroy tunnel object
+ * @tunnel: Tunnel object
+ *
+ * Remove the tunnel from the tunnel topology and destroy it.
+ */
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed))
+		return -ENODEV;
+
+	tun_dbg(tunnel, "destroying\n");
+
+	tunnel->destroyed = true;
+	destroy_tunnel(tunnel);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_destroy);
+
+static int check_tunnel(const struct drm_dp_tunnel *tunnel)
+{
+	if (tunnel->destroyed)
+		return -ENODEV;
+
+	if (tunnel->has_io_error)
+		return -EIO;
+
+	return 0;
+}
+
+static int group_allocated_bw(struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel *tunnel;
+	int group_allocated_bw = 0;
+
+	for_each_tunnel_in_group(group, tunnel) {
+		if (check_tunnel(tunnel) == 0 &&
+		    tunnel->bw_alloc_enabled)
+			group_allocated_bw += tunnel->allocated_bw;
+	}
+
+	return group_allocated_bw;
+}
+
+static int calc_group_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return group_allocated_bw(tunnel->group) -
+	       tunnel->allocated_bw +
+	       tunnel->estimated_bw;
+}
+
+static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
+				     const struct drm_dp_tunnel_regs *regs)
+{
+	struct drm_dp_tunnel *tunnel_iter;
+	int group_available_bw;
+	bool changed;
+
+	tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) * tunnel->bw_granularity;
+
+	if (calc_group_available_bw(tunnel) == tunnel->group->available_bw)
+		return 0;
+
+	for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
+		int err;
+
+		if (tunnel_iter == tunnel)
+			continue;
+
+		if (check_tunnel(tunnel_iter) != 0 ||
+		    !tunnel_iter->bw_alloc_enabled)
+			continue;
+
+		err = drm_dp_dpcd_probe(tunnel_iter->aux, DP_DPCD_REV);
+		if (err) {
+			tun_dbg(tunnel_iter,
+				"Probe failed, assume disconnected (err %pe)\n",
+				ERR_PTR(err));
+			drm_dp_tunnel_set_io_error(tunnel_iter);
+		}
+	}
+
+	group_available_bw = calc_group_available_bw(tunnel);
+
+	tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
+		DPTUN_BW_ARG(tunnel->group->available_bw),
+		DPTUN_BW_ARG(group_available_bw));
+
+	changed = tunnel->group->available_bw != group_available_bw;
+
+	tunnel->group->available_bw = group_available_bw;
+
+	return changed ? 1 : 0;
+}
+
+static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool enable)
+{
+	u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)
+		goto out_err;
+
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)
+		goto out_err;
+
+	tunnel->bw_alloc_enabled = enable;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_enable_bw_alloc: Enable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	if (!tunnel->bw_alloc_supported)
+		return -EOPNOTSUPP;
+
+	if (!tunnel_group_id(tunnel->group->drv_group_id))
+		return -EINVAL;
+
+	err = set_bw_alloc_mode(tunnel, true);
+	if (err)
+		goto out;
+
+	err = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (err) {
+		set_bw_alloc_mode(tunnel, false);
+
+		goto out;
+	}
+
+	if (!tunnel->max_dprx_rate)
+		update_dprx_caps(tunnel, &regs);
+
+	if (tunnel->group->available_bw == -1) {
+		err = update_group_available_bw(tunnel, &regs);
+		if (err > 0)
+			err = 0;
+	}
+out:
+	tun_dbg_stat(tunnel, err,
+		     "Enabling BW alloc mode: DPRX:%dx%d Group alloc:%d/%d Mb/s",
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
+
+/**
+ * drm_dp_tunnel_disable_bw_alloc: Disable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Disable the DP tunnel BW allocation mode on @tunnel.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	err = set_bw_alloc_mode(tunnel, false);
+
+	tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
+
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->bw_alloc_enabled;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
+
+static int bw_req_complete(struct drm_dp_aux *aux, bool *status_changed)
+{
+	u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
+	u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	*status_changed = val & status_change_mask;
+
+	val &= bw_req_mask;
+
+	if (!val)
+		return -EAGAIN;
+
+	if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, val) < 0)
+		return -EIO;
+
+	return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
+}
+
+static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
+	int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
+	unsigned long wait_expires;
+	DEFINE_WAIT(wait);
+	int err;
+
+	/* Atomic check should prevent the following. */
+	if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
+		err = -EINVAL;
+		goto out;
+	}
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW, request_bw) < 0) {
+		err = -EIO;
+		goto out;
+	}
+
+	wait_expires = jiffies + msecs_to_jiffies(3000);
+
+	for (;;) {
+		bool status_changed;
+
+		err = bw_req_complete(tunnel->aux, &status_changed);
+		if (err != -EAGAIN)
+			break;
+
+		if (status_changed) {
+			struct drm_dp_tunnel_regs regs;
+
+			err = read_and_verify_tunnel_regs(tunnel, &regs,
+							  ALLOW_ALLOCATED_BW_CHANGE);

drivers/gpu/drm/display/drm_dp_tunnel.c line 1026

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+			if (err)
+				break;
+		}
+
+		if (time_after(jiffies, wait_expires)) {
+			err = -ETIMEDOUT;
+			break;
+		}
+
+		prepare_to_wait(&mgr->bw_req_queue, &wait, TASK_UNINTERRUPTIBLE);

Shouldn't the prepare_to_wait() be done before checking the condition?

drivers/gpu/drm/display/drm_dp_tunnel.c line 1027

Quoted 1 line
drivers/gpu/drm/display/drm_dp_tunnel.c new file +1 -0
+		schedule_timeout(msecs_to_jiffies(200));

I guess the timeout here saves us, even if we race with the wakeup due to the above.

Quoted 239 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +239 -0
+	};
+
+	finish_wait(&mgr->bw_req_queue, &wait);
+
+	if (err)
+		goto out;
+
+	tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
+
+out:
+	tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel: Group alloc:%d/%d Mb/s",
+		     DPTUN_BW_ARG(request_bw * tunnel->bw_granularity),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (err == -EIO)
+		drm_dp_tunnel_set_io_error(tunnel);
+
+	return err;
+}
+
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	return allocate_tunnel_bw(tunnel, bw);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
+
+static int check_and_clear_status_change(struct drm_dp_tunnel *tunnel)
+{
+	u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0)
+		goto out_err;
+
+	val &= mask;
+
+	if (val) {
+		if (drm_dp_dpcd_writeb(tunnel->aux, DP_TUNNELING_STATUS, val) < 0)
+			goto out_err;
+
+		return 1;
+	}
+
+	if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
+		return 0;
+
+	/*
+	 * Check for estimated BW changes explicitly to account for lost
+	 * BW change notifications.
+	 */
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) < 0)
+		goto out_err;
+
+	if (val * tunnel->bw_granularity != tunnel->estimated_bw)
+		return 1;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_update_state: Update DP tunnel SW state with the HW state
+ * @tunnel: Tunnel object
+ *
+ * Update the SW state of @tunnel with the HW state.
+ *
+ * Returns 0 if the state has not changed, 1 if it has changed and got updated
+ * successfully and a negative error code otherwise.
+ */
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	bool changed = false;
+	int ret = check_tunnel(tunnel);
+
+	if (ret < 0)
+		return ret;
+
+	ret = check_and_clear_status_change(tunnel);
+	if (ret < 0)
+		goto out;
+
+	if (!ret)
+		return 0;
+
+	ret = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (ret)
+		goto out;
+
+	if (update_dprx_caps(tunnel, &regs))
+		changed = true;
+
+	ret = update_group_available_bw(tunnel, &regs);
+	if (ret == 1)
+		changed = true;
+
+out:
+	tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
+		     "State update: Changed:%c DPRX:%dx%d Tunnel alloc:%d/%d Group alloc:%d/%d Mb/s",
+		     yes_no_chr(changed),
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(tunnel->allocated_bw),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (ret < 0)
+		return ret;
+
+	if (changed)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_update_state);
+
+/*
+ * Returns 0 if no re-probe is needed, 1 if a re-probe is needed,
+ * a negative error code otherwise.
+ */
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_aux *aux)
+{
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
+		wake_up_all(&mgr->bw_req_queue);
+
+	if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED))
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
+
+/**
+ * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum link rate of the DPRX connected
+ * to @tunnel. Note that this rate will not be limited by the BW limit of the
+ * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
+ * registers.
+ *
+ * Returns the maximum link rate in 10 kbit/s units.
+ */
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_rate;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
+
+/**
+ * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum lane count of the DPRX connected
+ * to @tunnel. Note that this lane count will not be limited by the BW limit of
+ * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
+ * registers.
+ *
+ * Returns the maximum lane count.
+ */
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_lane_count;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
+
+/**
+ * drm_dp_tunnel_available_bw - Query the estimated total available BW of the tunnel
+ * @tunnel: Tunnel object
+ *
+ * This function is used to query the estimated total available BW of the
+ * tunnel. This includes the currently allocated and free BW for all the
+ * tunnels in @tunnel's group. The available BW is valid only after the BW
+ * allocation mode has been enabled for the tunnel and its state got updated
+ * calling drm_dp_tunnel_update_state().
+ *
+ * Returns the @tunnel group's estimated total available bandwidth in kB/s
+ * units, or -1 if the available BW isn't valid (the BW allocation mode is
+ * not enabled or the tunnel's state hasn't been updated).
+ */
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->group->available_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
+
+static struct drm_dp_tunnel_group_state *
+drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
+				     const struct drm_dp_tunnel *tunnel)
+{
+	return (struct drm_dp_tunnel_group_state *)
+		drm_atomic_get_private_obj_state(state,
+						 &tunnel->group->base);
+}
+
+static struct drm_dp_tunnel_state *
+add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tun_dbg_atomic(tunnel,
+		       "Adding state for tunnel %p to group state %p\n",
+		       tunnel, group_state);
+
+	tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
+	if (!tunnel_state)
+		return NULL;
+
+	tunnel_state->group_state = group_state;
+
+	drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
+
+	INIT_LIST_HEAD(&tunnel_state->node);
+	list_add(&tunnel_state->node, &group_state->tunnel_states);
+
+	return tunnel_state;
+}
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state)
+{
+	tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
+		       "Clearing state for tunnel %p\n",

drivers/gpu/drm/display/drm_dp_tunnel.c line 1276

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+		       tunnel_state->tunnel_ref.tunnel);
+
+	list_del(&tunnel_state->node);
+
+	kfree(tunnel_state->stream_bw);
+	drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
+
+	kfree(tunnel_state);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_clear_state);

That looks like some kind of destructor so the function name doesn't seem to fit.

Is there even any need to export that since it doesn't look like any kind of high level thing, and it's called from a static function below?

Quoted 385 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +385 -0
+
+static void clear_tunnel_group_state(struct drm_dp_tunnel_group_state *group_state)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+	struct drm_dp_tunnel_state *tunnel_state_tmp;
+
+	for_each_tunnel_state_safe(group_state, tunnel_state, tunnel_state_tmp)
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+}
+
+static struct drm_dp_tunnel_state *
+get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	for_each_tunnel_state(group_state, tunnel_state)
+		if (tunnel_state->tunnel_ref.tunnel == tunnel)
+			return tunnel_state;
+
+	return NULL;
+}
+
+static struct drm_dp_tunnel_state *
+get_or_add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+			struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tunnel_state = get_tunnel_state(group_state, tunnel);
+	if (tunnel_state)
+		return tunnel_state;
+
+	return add_tunnel_state(group_state, tunnel);
+}
+
+static struct drm_private_state *
+tunnel_group_duplicate_state(struct drm_private_obj *obj)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(obj->state);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+	if (!group_state)
+		return NULL;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	__drm_atomic_helper_private_obj_duplicate_state(obj, &group_state->base);
+
+	for_each_tunnel_state(to_group_state(obj->state), tunnel_state) {
+		struct drm_dp_tunnel_state *new_tunnel_state;
+
+		new_tunnel_state = get_or_add_tunnel_state(group_state,
+							   tunnel_state->tunnel_ref.tunnel);
+		if (!new_tunnel_state)
+			goto out_free_state;
+
+		new_tunnel_state->stream_mask = tunnel_state->stream_mask;
+		new_tunnel_state->stream_bw = kmemdup(tunnel_state->stream_bw,
+						      sizeof(*tunnel_state->stream_bw) *
+							hweight32(tunnel_state->stream_mask),
+						      GFP_KERNEL);
+
+		if (!new_tunnel_state->stream_bw)
+			goto out_free_state;
+	}
+
+	return &group_state->base;
+
+out_free_state:
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+
+	return NULL;
+}
+
+static void tunnel_group_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(state);
+
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+}
+
+static const struct drm_private_state_funcs tunnel_group_funcs = {
+	.atomic_duplicate_state = tunnel_group_duplicate_state,
+	.atomic_destroy_state = tunnel_group_destroy_state,
+};
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return ERR_CAST(group_state);
+
+	tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
+	if (!tunnel_state)
+		return ERR_PTR(-ENOMEM);
+
+	return tunnel_state;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i)
+		if (to_group(new_group_state->base.obj) == tunnel->group)
+			return get_tunnel_state(new_group_state, tunnel);
+
+	return NULL;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
+
+static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel_group_state *group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+
+	if (!group_state)
+		return false;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	group->mgr = mgr;
+	group->available_bw = -1;
+	INIT_LIST_HEAD(&group->tunnels);
+
+	drm_atomic_private_obj_init(mgr->dev, &group->base, &group_state->base,
+				    &tunnel_group_funcs);
+
+	return true;
+}
+
+static void cleanup_group(struct drm_dp_tunnel_group *group)
+{
+	drm_atomic_private_obj_fini(&group->base);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+	const struct drm_dp_tunnel_state *tunnel_state;
+	u32 stream_mask = 0;
+
+	for_each_tunnel_state(group_state, tunnel_state) {
+		drm_WARN(to_group(group_state->base.obj)->mgr->dev,
+			 tunnel_state->stream_mask & stream_mask,
+			 "[DPTUN %s]: conflicting stream IDs %x (IDs in other tunnels %x)\n",
+			 tunnel_state->tunnel_ref.tunnel->name,
+			 tunnel_state->stream_mask,
+			 stream_mask);
+
+		stream_mask |= tunnel_state->stream_mask;
+	}
+}
+#else
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+}
+#endif
+
+static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
+{
+	return hweight32(stream_mask & (BIT(stream_id) - 1));
+}
+
+static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
+			   unsigned long old_mask, unsigned long new_mask)
+{
+	unsigned long move_mask = old_mask & new_mask;
+	int *new_bws = NULL;
+	int id;
+
+	WARN_ON(!new_mask);
+
+	if (old_mask == new_mask)
+		return 0;
+
+	new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws), GFP_KERNEL);
+	if (!new_bws)
+		return -ENOMEM;
+
+	for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask))
+		new_bws[stream_id_to_idx(new_mask, id)] =
+			tunnel_state->stream_bw[stream_id_to_idx(old_mask, id)];
+
+	kfree(tunnel_state->stream_bw);
+	tunnel_state->stream_bw = new_bws;
+	tunnel_state->stream_mask = new_mask;
+
+	return 0;
+}
+
+static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			 u8 stream_id, int bw)
+{
+	int err;
+
+	err = resize_bw_array(tunnel_state,
+			      tunnel_state->stream_mask,
+			      tunnel_state->stream_mask | BIT(stream_id));
+	if (err)
+		return err;
+
+	tunnel_state->stream_bw[stream_id_to_idx(tunnel_state->stream_mask, stream_id)] = bw;
+
+	return 0;
+}
+
+static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			   u8 stream_id)
+{
+	if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+		return 0;
+	}
+
+	return resize_bw_array(tunnel_state,
+			       tunnel_state->stream_mask,
+			       tunnel_state->stream_mask & ~BIT(stream_id));
+}
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+					 struct drm_dp_tunnel *tunnel,
+					 u8 stream_id, int bw)
+{
+	struct drm_dp_tunnel_group_state *new_group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+	int err;
+
+	if (drm_WARN_ON(tunnel->group->mgr->dev,
+			stream_id > BITS_PER_TYPE(tunnel_state->stream_mask)))
+		return -EINVAL;
+
+	tun_dbg(tunnel,
+		"Setting %d Mb/s for stream %d\n",
+		DPTUN_BW_ARG(bw), stream_id);
+
+	if (bw == 0) {
+		tunnel_state = get_tunnel_state(new_group_state, tunnel);
+		if (!tunnel_state)
+			return 0;
+
+		return clear_stream_bw(tunnel_state, stream_id);
+	}
+
+	tunnel_state = get_or_add_tunnel_state(new_group_state, tunnel);
+	if (drm_WARN_ON(state->dev, !tunnel_state))
+		return -EINVAL;
+
+	err = set_stream_bw(tunnel_state, stream_id, bw);
+	if (err)
+		return err;
+
+	check_unique_stream_ids(new_group_state);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	int tunnel_bw = 0;
+	int i;
+
+	for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
+		tunnel_bw += tunnel_state->stream_bw[i];
+
+	return tunnel_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_tunnel_bw);
+
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return PTR_ERR(group_state);
+
+	*stream_mask = 0;
+	for_each_tunnel_state(group_state, tunnel_state)
+		*stream_mask |= tunnel_state->stream_mask;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
+
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
+				    u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
+	struct drm_dp_tunnel_state *new_tunnel_state;
+	u32 group_stream_mask = 0;
+	int group_bw = 0;
+
+	for_each_tunnel_state(new_group_state, new_tunnel_state) {
+		struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;
+		int max_dprx_bw = get_max_dprx_bw(tunnel);
+		int tunnel_bw = drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);
+
+		tun_dbg(tunnel,
+			"%sRequired %d/%d Mb/s total for tunnel.\n",
+			tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
+			DPTUN_BW_ARG(tunnel_bw),
+			DPTUN_BW_ARG(max_dprx_bw));
+
+		if (tunnel_bw > max_dprx_bw) {
+			*failed_stream_mask = new_tunnel_state->stream_mask;
+			return -ENOSPC;
+		}
+
+		group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
+				max_dprx_bw);
+		group_stream_mask |= new_tunnel_state->stream_mask;
+	}
+
+	tun_grp_dbg(group,
+		    "%sRequired %d/%d Mb/s total for tunnel group.\n",
+		    group_bw > group->available_bw ? "Not enough BW: " : "",
+		    DPTUN_BW_ARG(group_bw),
+		    DPTUN_BW_ARG(group->available_bw));
+
+	if (group_bw > group->available_bw) {
+		*failed_stream_mask = group_stream_mask;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i) {
+		int ret;
+
+		ret = drm_dp_tunnel_atomic_check_group_bw(new_group_state,
+							  failed_stream_mask);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
+
+static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
+{
+	int i;
+
+	for (i = 0; i < mgr->group_count; i++) {
+		cleanup_group(&mgr->groups[i]);
+		drm_WARN_ON(mgr->dev, !list_empty(&mgr->groups[i].tunnels));
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_exit(&mgr->ref_tracker);
+#endif
+
+	kfree(mgr->groups);
+	kfree(mgr);
+}
+
+/**
+ * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
+ * @i915: i915 driver object

drivers/gpu/drm/display/drm_dp_tunnel.c line 1671

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+ *
+ * Creates a DP tunnel manager.
+ *
+ * Returns a pointer to the tunnel manager if created successfully or NULL in
+ * case of an error.
+ */
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	struct drm_dp_tunnel_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);

I dislike it when functions that can fail or that have side effects are called from the variable declaration block. There's quite a bit of that in this patch. IMO it's far too easy to overlook such function calls.

drivers/gpu/drm/display/drm_dp_tunnel.c line 1673

Quoted 2 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +2 -0
+	int i;
+

ie. the kzalloc() should be here IMO.

Quoted 391 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +42 -0
+	if (!mgr)
+		return NULL;
+
+	mgr->dev = dev;
+	init_waitqueue_head(&mgr->bw_req_queue);
+
+	mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups), GFP_KERNEL);
+	if (!mgr->groups) {
+		kfree(mgr);
+
+		return NULL;
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
+#endif
+
+	for (i = 0; i < max_group_count; i++) {
+		if (!init_group(mgr, &mgr->groups[i])) {
+			destroy_mgr(mgr);
+
+			return NULL;
+		}
+
+		mgr->group_count++;
+	}
+
+	return mgr;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
+
+/**
+ * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
+ * @mgr: Tunnel manager object
+ *
+ * Destroy the tunnel manager.
+ */
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
+{
+	destroy_mgr(mgr);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);
include/drm/display/drm_dp.h +60 -0
index 281afff6ee4e5..8bfd5d007be8d 100644
@@ -1382,6 +1382,66 @@
 #define DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET	0x69494
 #define DP_HDCP_2_2_REG_DBG_OFFSET		0x69518
 
+/* DP-tunneling */
+#define DP_TUNNELING_OUI				0xe0000
+#define  DP_TUNNELING_OUI_BYTES				3
+
+#define DP_TUNNELING_DEV_ID				0xe0003
+#define  DP_TUNNELING_DEV_ID_BYTES			6
+
+#define DP_TUNNELING_HW_REV				0xe0009
+#define  DP_TUNNELING_HW_REV_MAJOR_SHIFT		4
+#define  DP_TUNNELING_HW_REV_MAJOR_MASK			(0xf << DP_TUNNELING_HW_REV_MAJOR_SHIFT)
+#define  DP_TUNNELING_HW_REV_MINOR_SHIFT		0
+#define  DP_TUNNELING_HW_REV_MINOR_MASK			(0xf << DP_TUNNELING_HW_REV_MINOR_SHIFT)
+
+#define DP_TUNNELING_SW_REV_MAJOR			0xe000a
+#define DP_TUNNELING_SW_REV_MINOR			0xe000b
+
+#define DP_TUNNELING_CAPABILITIES			0xe000d
+#define  DP_IN_BW_ALLOCATION_MODE_SUPPORT		(1 << 7)
+#define  DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT		(1 << 6)
+#define  DP_TUNNELING_SUPPORT				(1 << 0)
+
+#define DP_IN_ADAPTER_INFO				0xe000e
+#define  DP_IN_ADAPTER_NUMBER_BITS			7
+#define  DP_IN_ADAPTER_NUMBER_MASK			((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1)
+
+#define DP_USB4_DRIVER_ID				0xe000f
+#define  DP_USB4_DRIVER_ID_BITS				4
+#define  DP_USB4_DRIVER_ID_MASK				((1 << DP_USB4_DRIVER_ID_BITS) - 1)
+
+#define DP_USB4_DRIVER_BW_CAPABILITY			0xe0020
+#define  DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT	(1 << 7)
+
+#define DP_IN_ADAPTER_TUNNEL_INFORMATION		0xe0021
+#define  DP_GROUP_ID_BITS				3
+#define  DP_GROUP_ID_MASK				((1 << DP_GROUP_ID_BITS) - 1)
+
+#define DP_BW_GRANULARITY				0xe0022
+#define  DP_BW_GRANULARITY_MASK				0x3
+
+#define DP_ESTIMATED_BW					0xe0023
+#define DP_ALLOCATED_BW					0xe0024
+
+#define DP_TUNNELING_STATUS				0xe0025
+#define  DP_BW_ALLOCATION_CAPABILITY_CHANGED		(1 << 3)
+#define  DP_ESTIMATED_BW_CHANGED			(1 << 2)
+#define  DP_BW_REQUEST_SUCCEEDED			(1 << 1)
+#define  DP_BW_REQUEST_FAILED				(1 << 0)
+
+#define DP_TUNNELING_MAX_LINK_RATE			0xe0028
+
+#define DP_TUNNELING_MAX_LANE_COUNT			0xe0029
+#define  DP_TUNNELING_MAX_LANE_COUNT_MASK		0x1f
+
+#define DP_DPTX_BW_ALLOCATION_MODE_CONTROL		0xe0030
+#define  DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE	(1 << 7)
+#define  DP_UNMASK_BW_ALLOCATION_IRQ			(1 << 6)
+
+#define DP_REQUEST_BW					0xe0031
+#define  MAX_DP_REQUEST_BW				255
+
 /* LTTPR: Link Training (LT)-tunable PHY Repeaters */
 #define DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV 0xf0000 /* 1.3 */
 #define DP_MAX_LINK_RATE_PHY_REPEATER			    0xf0001 /* 1.4a */
include/drm/display/drm_dp_tunnel.h new file +270 -0
new file mode 100644
index 0000000000000..f6449b1b4e6e9
@@ -0,0 +1,270 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __DRM_DP_TUNNEL_H__
+#define __DRM_DP_TUNNEL_H__
+
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+
+struct drm_dp_aux;
+
+struct drm_device;
+
+struct drm_atomic_state;
+struct drm_dp_tunnel_mgr;
+struct drm_dp_tunnel_state;
+
+struct ref_tracker;
+
+struct drm_dp_tunnel_ref {
+	struct drm_dp_tunnel *tunnel;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker *tracker;
+#endif
+};
+
+#ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel);
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel);
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+
+void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+#else
+#define drm_dp_tunnel_get(tunnel, tracker) \
+	drm_dp_tunnel_get_untracked(tunnel)
+
+#define drm_dp_tunnel_put(tunnel, tracker) \
+	drm_dp_tunnel_put_untracked(tunnel)
+
+#endif
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	tunnel_ref->tunnel = drm_dp_tunnel_get(tunnel, &tunnel_ref->tracker);
+}
+
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	drm_dp_tunnel_put(tunnel_ref->tunnel, &tunnel_ref->tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux);
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel);
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw);
+int drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			     struct drm_dp_aux *aux);
+
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel);
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel);
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state);
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				       struct drm_dp_tunnel *tunnel,
+				       u8 stream_id, int bw);
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask);
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state);
+
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count);
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr);
+
+#else
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker) {}
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref) {}
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline int
+drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return false;
+}
+
+static inline int
+drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel) {}
+static inline int
+drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			 struct drm_dp_aux *aux)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return -1;
+}
+
+static inline const char *
+drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline void
+drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state) {}
+
+static inline int
+drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				   struct drm_dp_tunnel *tunnel,
+				   u8 stream_id, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						const struct drm_dp_tunnel *tunnel,
+						u32 *stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+				      u32 *failed_stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	return 0;
+}
+
+static inline struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr) {}
+
+
+#endif /* CONFIG_DRM_DISPLAY_DP_TUNNEL */
+
+#endif /* __DRM_DP_TUNNEL_H__ */
Signature · 1 line
2.39.2
Signature · 2 lines
Ville Syrjälä
Intel
author
#9 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 60 lines · Ville Syrjälä
Quoted 59 lines · Imre Deak

Add support for Display Port DP tunneling. For now this includes the support for Bandwidth Allocation Mode, leaving adding Panel Replay support for later.

BWA allows using displays that share the same (Thunderbolt) link with their maximum resolution. Atm, this may not be possible due to the coarse granularity of partitioning the link BW among the displays on the
link: the BW allocation policy is in a SW/FW/HW component on the link (on Thunderbolt it's the SW or FW Connection Manager), independent of the driver. This policy will set the DPRX maximum rate and lane count DPCD registers the GFX driver will see (0x00000, 0x00001, 0x02200, 0x02201) based on the available link BW.

The granularity of the current BW allocation policy is course, based on the required link rate in the 1.62Gbs..8.1Gbps range and it may prevent using higher resolutions all together: the display connected first will get a share of the link BW which corresponds to its full DPRX capability (regardless of the actual mode it uses). A subsequent display connected will only get the remaining BW, which could be well below its full capability.

BWA solves the above course granularity (reducing it to a 250Mbs..1Gps range) and first-come/first-served issues by letting the driver request the BW for each display on a link which reflects the actual modes the displays use.

This patch adds the DRM core helper functions, while a follow-up change in the patchset takes them into use in the i915 driver.

---
 drivers/gpu/drm/display/Kconfig         |   17 +
 drivers/gpu/drm/display/Makefile        |    2 +
 drivers/gpu/drm/display/drm_dp_tunnel.c | 1715 +++++++++++++++++++++++
 include/drm/display/drm_dp.h            |   60 +
 include/drm/display/drm_dp_tunnel.h     |  270 ++++
 5 files changed, 2064 insertions(+)
 create mode 100644 drivers/gpu/drm/display/drm_dp_tunnel.c
 create mode 100644 include/drm/display/drm_dp_tunnel.h
drivers/gpu/drm/display/Kconfig +11 -0
index 09712b88a5b83..b024a84b94c1c 100644
@@ -17,6 +17,23 @@ config DRM_DISPLAY_DP_HELPER
 	help
 	  DRM display helpers for DisplayPort.
 
+config DRM_DISPLAY_DP_TUNNEL
+	bool
+	select DRM_DISPLAY_DP_HELPER
+	help
+	  Enable support for DisplayPort tunnels.
+
+config DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	bool "Enable debugging the DP tunnel state"
+	depends on REF_TRACKER
+	depends on DRM_DISPLAY_DP_TUNNEL
+	depends on DEBUG_KERNEL
Quoted 8 lines

drivers/gpu/drm/display/Kconfig config DRM_DISPLAY_DP_HELPER · line 35

Quoted 5 lines
drivers/gpu/drm/display/Kconfig +5 -0
+	depends on EXPERT
+	help
+	  Enables debugging the DP tunnel manager's status.
+
+	  If in doubt, say "N".

It's not exactly clear what a "DP tunnel" is. Shouldn't thunderbolt be mentioned here somewhere?

The only way I'm aware of tunneling can work is through a TBT link yes, however I'm not sure if it couldn't work on any DP link, the interface - to request BW - is simply the AUX bus after all and AFAIR the standard doesn't mention TBT either (but have to reread that). The above descriptions should be extended anyway and the usual TBT scenario mentioned at least, so will do that.

Quoted 145 lines
Quoted 145 lines
drivers/gpu/drm/display/Kconfig +1 -0
+
 config DRM_DISPLAY_HDCP_HELPER
 	bool
 	depends on DRM_DISPLAY_HELPER
drivers/gpu/drm/display/Makefile +2 -0
index 17ac4a1006a80..7ca61333c6696 100644
@@ -8,6 +8,8 @@ drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_HELPER) += \
 	drm_dp_helper.o \
 	drm_dp_mst_topology.o \
 	drm_dsc_helper.o
+drm_display_helper-$(CONFIG_DRM_DISPLAY_DP_TUNNEL) += \
+	drm_dp_tunnel.o
 drm_display_helper-$(CONFIG_DRM_DISPLAY_HDCP_HELPER) += drm_hdcp_helper.o
 drm_display_helper-$(CONFIG_DRM_DISPLAY_HDMI_HELPER) += \
 	drm_hdmi_helper.o \
drivers/gpu/drm/display/drm_dp_tunnel.c new file +122 -0
new file mode 100644
index 0000000000000..58f6330db7d9d
@@ -0,0 +1,1715 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/ref_tracker.h>
+#include <linux/types.h>
+
+#include <drm/drm_atomic_state_helper.h>
+
+#include <drm/drm_atomic.h>
+#include <drm/drm_print.h>
+#include <drm/display/drm_dp.h>
+#include <drm/display/drm_dp_helper.h>
+#include <drm/display/drm_dp_tunnel.h>
+
+#define to_group(__private_obj) \
+	container_of(__private_obj, struct drm_dp_tunnel_group, base)
+
+#define to_group_state(__private_state) \
+	container_of(__private_state, struct drm_dp_tunnel_group_state, base)
+
+#define is_dp_tunnel_private_obj(__obj) \
+	((__obj)->funcs == &tunnel_group_funcs)
+
+#define for_each_new_group_in_state(__state, __new_group_state, __i) \
+	for ((__i) = 0; \
+	     (__i) < (__state)->num_private_objs; \
+	     (__i)++) \
+		for_each_if ((__state)->private_objs[__i].ptr && \
+			     is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \
+			     ((__new_group_state) = \
+				to_group_state((__state)->private_objs[__i].new_state), 1))
+
+#define for_each_old_group_in_state(__state, __old_group_state, __i) \
+	for ((__i) = 0; \
+	     (__i) < (__state)->num_private_objs; \
+	     (__i)++) \
+		for_each_if ((__state)->private_objs[__i].ptr && \
+			     is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \
+			     ((__old_group_state) = \
+				to_group_state((__state)->private_objs[__i].old_state), 1))
+
+#define for_each_tunnel_in_group(__group, __tunnel) \
+	list_for_each_entry(__tunnel, &(__group)->tunnels, node)
+
+#define for_each_tunnel_state(__group_state, __tunnel_state) \
+	list_for_each_entry(__tunnel_state, &(__group_state)->tunnel_states, node)
+
+#define for_each_tunnel_state_safe(__group_state, __tunnel_state, __tunnel_state_tmp) \
+	list_for_each_entry_safe(__tunnel_state, __tunnel_state_tmp, \
+				 &(__group_state)->tunnel_states, node)
+
+#define kbytes_to_mbits(__kbytes) \
+	DIV_ROUND_UP((__kbytes) * 8, 1000)
+
+#define DPTUN_BW_ARG(__bw) ((__bw) < 0 ? (__bw) : kbytes_to_mbits(__bw))
+
+#define __tun_prn(__tunnel, __level, __type, __fmt, ...) \
+	drm_##__level##__type((__tunnel)->group->mgr->dev, \
+			      "[DPTUN %s][%s] " __fmt, \
+			      drm_dp_tunnel_name(__tunnel), \
+			      (__tunnel)->aux->name, ## \
+			      __VA_ARGS__)
+
+#define tun_dbg(__tunnel, __fmt, ...) \
+	__tun_prn(__tunnel, dbg, _kms, __fmt, ## __VA_ARGS__)
+
+#define tun_dbg_stat(__tunnel, __err, __fmt, ...) do { \
+	if (__err) \
+		__tun_prn(__tunnel, dbg, _kms, __fmt " (Failed, err: %pe)\n", \
+			  ## __VA_ARGS__, ERR_PTR(__err)); \
+	else \
+		__tun_prn(__tunnel, dbg, _kms, __fmt " (Ok)\n", \
+			  ## __VA_ARGS__); \
+} while (0)
+
+#define tun_dbg_atomic(__tunnel, __fmt, ...) \
+	__tun_prn(__tunnel, dbg, _atomic, __fmt, ## __VA_ARGS__)
+
+#define tun_grp_dbg(__group, __fmt, ...) \
+	drm_dbg_kms((__group)->mgr->dev, \
+		    "[DPTUN %s] " __fmt, \
+		    drm_dp_tunnel_group_name(__group), ## \
+		    __VA_ARGS__)
+
+#define DP_TUNNELING_BASE DP_TUNNELING_OUI
+
+#define __DPTUN_REG_RANGE(start, size) \
+	GENMASK_ULL(start + size - 1, start)
+
+#define DPTUN_REG_RANGE(addr, size) \
+	__DPTUN_REG_RANGE((addr) - DP_TUNNELING_BASE, size)
+
+#define DPTUN_REG(addr) DPTUN_REG_RANGE(addr, 1)
+
+#define DPTUN_INFO_REG_MASK ( \
+	DPTUN_REG_RANGE(DP_TUNNELING_OUI, DP_TUNNELING_OUI_BYTES) | \
+	DPTUN_REG_RANGE(DP_TUNNELING_DEV_ID, DP_TUNNELING_DEV_ID_BYTES) | \
+	DPTUN_REG(DP_TUNNELING_HW_REV) | \
+	DPTUN_REG(DP_TUNNELING_SW_REV_MAJOR) | \
+	DPTUN_REG(DP_TUNNELING_SW_REV_MINOR) | \
+	DPTUN_REG(DP_TUNNELING_CAPABILITIES) | \
+	DPTUN_REG(DP_IN_ADAPTER_INFO) | \
+	DPTUN_REG(DP_USB4_DRIVER_ID) | \
+	DPTUN_REG(DP_USB4_DRIVER_BW_CAPABILITY) | \
+	DPTUN_REG(DP_IN_ADAPTER_TUNNEL_INFORMATION) | \
+	DPTUN_REG(DP_BW_GRANULARITY) | \
+	DPTUN_REG(DP_ESTIMATED_BW) | \
+	DPTUN_REG(DP_ALLOCATED_BW) | \
+	DPTUN_REG(DP_TUNNELING_MAX_LINK_RATE) | \
+	DPTUN_REG(DP_TUNNELING_MAX_LANE_COUNT) | \
+	DPTUN_REG(DP_DPTX_BW_ALLOCATION_MODE_CONTROL))
+
+static const DECLARE_BITMAP(dptun_info_regs, 64) = {
+	DPTUN_INFO_REG_MASK & -1UL,
+#if BITS_PER_LONG == 32
+	DPTUN_INFO_REG_MASK >> 32,
+#endif
+};
+
+struct drm_dp_tunnel_regs {
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 124

Quoted 2 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +2 -0
+	u8 buf[HWEIGHT64(DPTUN_INFO_REG_MASK)];
+};

That seems to be some kind of thing to allow us to store the values for non-consecutive DPCD registers in a contiguous non-sparse array? How much memory are we actually saving here as opposed to just using the full sized array?

Actually not for saving space, rather a way to define the contiguous ranges that can be read out in one transfer, without accessing other registers (which may have side-effects). A bitmap being the more decent way without having to specify the ranges in an ad-hoc way.

Quoted 112 lines

Wasn't really expecting this kind of thing in here...

Quoted 109 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +109 -0
+
+struct drm_dp_tunnel_group;
+
+struct drm_dp_tunnel {
+	struct drm_dp_tunnel_group *group;
+
+	struct list_head node;
+
+	struct kref kref;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker *tracker;
+#endif
+	struct drm_dp_aux *aux;
+	char name[8];
+
+	int bw_granularity;
+	int estimated_bw;
+	int allocated_bw;
+
+	int max_dprx_rate;
+	u8 max_dprx_lane_count;
+
+	u8 adapter_id;
+
+	bool bw_alloc_supported:1;
+	bool bw_alloc_enabled:1;
+	bool has_io_error:1;
+	bool destroyed:1;
+};
+
+struct drm_dp_tunnel_group_state;
+
+struct drm_dp_tunnel_state {
+	struct drm_dp_tunnel_group_state *group_state;
+
+	struct drm_dp_tunnel_ref tunnel_ref;
+
+	struct list_head node;
+
+	u32 stream_mask;
+	int *stream_bw;
+};
+
+struct drm_dp_tunnel_group_state {
+	struct drm_private_state base;
+
+	struct list_head tunnel_states;
+};
+
+struct drm_dp_tunnel_group {
+	struct drm_private_obj base;
+	struct drm_dp_tunnel_mgr *mgr;
+
+	struct list_head tunnels;
+
+	int available_bw;	/* available BW including the allocated_bw of all tunnels */
+	int drv_group_id;
+
+	char name[8];
+
+	bool active:1;
+};
+
+struct drm_dp_tunnel_mgr {
+	struct drm_device *dev;
+
+	int group_count;
+	struct drm_dp_tunnel_group *groups;
+	wait_queue_head_t bw_req_queue;
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker_dir ref_tracker;
+#endif
+};
+
+static int next_reg_area(int *offset)
+{
+	*offset = find_next_bit(dptun_info_regs, 64, *offset);
+
+	return find_next_zero_bit(dptun_info_regs, 64, *offset + 1) - *offset;
+}
+
+#define tunnel_reg_ptr(__regs, __address) ({ \
+	WARN_ON(!test_bit((__address) - DP_TUNNELING_BASE, dptun_info_regs)); \
+	&(__regs)->buf[bitmap_weight(dptun_info_regs, (__address) - DP_TUNNELING_BASE)]; \
+})
+
+static int read_tunnel_regs(struct drm_dp_aux *aux, struct drm_dp_tunnel_regs *regs)
+{
+	int offset = 0;
+	int len;
+
+	while ((len = next_reg_area(&offset))) {
+		int address = DP_TUNNELING_BASE + offset;
+
+		if (drm_dp_dpcd_read(aux, address, tunnel_reg_ptr(regs, address), len) < 0)
+			return -EIO;
+
+		offset += len;
+	}
+
+	return 0;
+}
+
+static u8 tunnel_reg(const struct drm_dp_tunnel_regs *regs, int address)
+{
+	return *tunnel_reg_ptr(regs, address);
+}
+
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 237

Quoted 4 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+static int tunnel_reg_drv_group_id(const struct drm_dp_tunnel_regs *regs)
+{
+	int drv_id = tunnel_reg(regs, DP_USB4_DRIVER_ID) & DP_USB4_DRIVER_ID_MASK;
+	int group_id = tunnel_reg(regs, DP_IN_ADAPTER_TUNNEL_INFORMATION) & DP_GROUP_ID_MASK;

Maybe these things should be u8/etc. everywhere? Would at least indicate that I don't need to look for where negative values are handled...

Ok, will change these.

Quoted 93 lines
Quoted 93 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +93 -0
+
+	if (!group_id)
+		return 0;
+
+	return (drv_id << DP_GROUP_ID_BITS) | group_id;
+}
+
+/* Return granularity in kB/s units */
+static int tunnel_reg_bw_granularity(const struct drm_dp_tunnel_regs *regs)
+{
+	int gr = tunnel_reg(regs, DP_BW_GRANULARITY) & DP_BW_GRANULARITY_MASK;
+
+	WARN_ON(gr > 2);
+
+	return (250000 << gr) / 8;
+}
+
+static int tunnel_reg_max_dprx_rate(const struct drm_dp_tunnel_regs *regs)
+{
+	u8 bw_code = tunnel_reg(regs, DP_TUNNELING_MAX_LINK_RATE);
+
+	return drm_dp_bw_code_to_link_rate(bw_code);
+}
+
+static int tunnel_reg_max_dprx_lane_count(const struct drm_dp_tunnel_regs *regs)
+{
+	u8 lane_count = tunnel_reg(regs, DP_TUNNELING_MAX_LANE_COUNT) &
+			DP_TUNNELING_MAX_LANE_COUNT_MASK;
+
+	return lane_count;
+}
+
+static bool tunnel_reg_bw_alloc_supported(const struct drm_dp_tunnel_regs *regs)
+{
+	u8 cap_mask = DP_TUNNELING_SUPPORT | DP_IN_BW_ALLOCATION_MODE_SUPPORT;
+
+	if ((tunnel_reg(regs, DP_TUNNELING_CAPABILITIES) & cap_mask) != cap_mask)
+		return false;
+
+	return tunnel_reg(regs, DP_USB4_DRIVER_BW_CAPABILITY) &
+	       DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT;
+}
+
+static bool tunnel_reg_bw_alloc_enabled(const struct drm_dp_tunnel_regs *regs)
+{
+	return tunnel_reg(regs, DP_DPTX_BW_ALLOCATION_MODE_CONTROL) &
+		DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE;
+}
+
+static int tunnel_group_drv_id(int drv_group_id)
+{
+	return drv_group_id >> DP_GROUP_ID_BITS;
+}
+
+static int tunnel_group_id(int drv_group_id)
+{
+	return drv_group_id & DP_GROUP_ID_MASK;
+}
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->name;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_name);
+
+static const char *drm_dp_tunnel_group_name(const struct drm_dp_tunnel_group *group)
+{
+	return group->name;
+}
+
+static struct drm_dp_tunnel_group *
+lookup_or_alloc_group(struct drm_dp_tunnel_mgr *mgr, int drv_group_id)
+{
+	struct drm_dp_tunnel_group *group = NULL;
+	int i;
+
+	for (i = 0; i < mgr->group_count; i++) {
+		/*
+		 * A tunnel group with 0 group ID shouldn't have more than one
+		 * tunnels.
+		 */
+		if (tunnel_group_id(drv_group_id) &&
+		    mgr->groups[i].drv_group_id == drv_group_id)
+			return &mgr->groups[i];
+
+		if (!group && !mgr->groups[i].active)
+			group = &mgr->groups[i];
+	}
+
+	if (!group) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Can't allocate more tunnel groups\n");
+		return NULL;
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 336

Quoted 6 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+	}
+
+	group->drv_group_id = drv_group_id;
+	group->active = true;
+
+	snprintf(group->name, sizeof(group->name), "%d:%d:*",

What does the '*' indicate?

The prefix in all DP tunnel/group debug message is Driver-ID:Group-ID:DP-Adapter-ID, for group debug messages * standing for all tunnels (aka DP-Adapters) in the group.

Quoted 60 lines
Quoted 60 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +60 -0
+		 tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
+		 tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1));
+
+	return group;
+}
+
+static void free_group(struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel_mgr *mgr = group->mgr;
+
+	if (drm_WARN_ON(mgr->dev, !list_empty(&group->tunnels)))
+		return;
+
+	group->drv_group_id = 0;
+	group->available_bw = -1;
+	group->active = false;
+}
+
+static struct drm_dp_tunnel *
+tunnel_get(struct drm_dp_tunnel *tunnel)
+{
+	kref_get(&tunnel->kref);
+
+	return tunnel;
+}
+
+static void free_tunnel(struct kref *kref)
+{
+	struct drm_dp_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref);
+	struct drm_dp_tunnel_group *group = tunnel->group;
+
+	list_del(&tunnel->node);
+	if (list_empty(&group->tunnels))
+		free_group(group);
+
+	kfree(tunnel);
+}
+
+static void tunnel_put(struct drm_dp_tunnel *tunnel)
+{
+	kref_put(&tunnel->kref, free_tunnel);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void track_tunnel_ref(struct drm_dp_tunnel *tunnel,
+			     struct ref_tracker **tracker)
+{
+	ref_tracker_alloc(&tunnel->group->mgr->ref_tracker,
+			  tracker, GFP_KERNEL);
+}
+
+static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
+			       struct ref_tracker **tracker)
+{
+	ref_tracker_free(&tunnel->group->mgr->ref_tracker,
+			 tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 402

Quoted 6 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+	track_tunnel_ref(tunnel, NULL);
+
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);

Why do these exist?

They implement drm_dp_tunnel_get()/put() if
CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE=n.

Quoted 302 lines
Quoted 302 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +302 -0
+
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel)
+{
+	tunnel_put(tunnel);
+	untrack_tunnel_ref(tunnel, NULL);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put_untracked);
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel,
+		    struct ref_tracker **tracker)
+{
+	track_tunnel_ref(tunnel, tracker);
+
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get);
+
+void drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel,
+			 struct ref_tracker **tracker)
+{
+	untrack_tunnel_ref(tunnel, tracker);
+	tunnel_put(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put);
+#else
+#define track_tunnel_ref(tunnel, tracker) do {} while (0)
+#define untrack_tunnel_ref(tunnel, tracker) do {} while (0)
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);
+
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel)
+{
+	tunnel_put(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_put_untracked);
+#endif
+
+static bool add_tunnel_to_group(struct drm_dp_tunnel_mgr *mgr,
+				int drv_group_id,
+				struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group *group =
+		lookup_or_alloc_group(mgr, drv_group_id);
+
+	if (!group)
+		return false;
+
+	tunnel->group = group;
+	list_add(&tunnel->node, &group->tunnels);
+
+	return true;
+}
+
+static struct drm_dp_tunnel *
+create_tunnel(struct drm_dp_tunnel_mgr *mgr,
+	      struct drm_dp_aux *aux,
+	      const struct drm_dp_tunnel_regs *regs)
+{
+	int drv_group_id = tunnel_reg_drv_group_id(regs);
+	struct drm_dp_tunnel *tunnel;
+
+	tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
+	if (!tunnel)
+		return NULL;
+
+	INIT_LIST_HEAD(&tunnel->node);
+
+	kref_init(&tunnel->kref);
+
+	tunnel->aux = aux;
+
+	tunnel->adapter_id = tunnel_reg(regs, DP_IN_ADAPTER_INFO) & DP_IN_ADAPTER_NUMBER_MASK;
+
+	snprintf(tunnel->name, sizeof(tunnel->name), "%d:%d:%d",
+		 tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
+		 tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1),
+		 tunnel->adapter_id & ((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1));
+
+	tunnel->bw_granularity = tunnel_reg_bw_granularity(regs);
+	tunnel->allocated_bw = tunnel_reg(regs, DP_ALLOCATED_BW) *
+			       tunnel->bw_granularity;
+
+	tunnel->bw_alloc_supported = tunnel_reg_bw_alloc_supported(regs);
+	tunnel->bw_alloc_enabled = tunnel_reg_bw_alloc_enabled(regs);
+
+	if (!add_tunnel_to_group(mgr, drv_group_id, tunnel)) {
+		kfree(tunnel);
+
+		return NULL;
+	}
+
+	track_tunnel_ref(tunnel, &tunnel->tracker);
+
+	return tunnel;
+}
+
+static void destroy_tunnel(struct drm_dp_tunnel *tunnel)
+{
+	untrack_tunnel_ref(tunnel, &tunnel->tracker);
+	tunnel_put(tunnel);
+}
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel)
+{
+	tunnel->has_io_error = true;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_set_io_error);
+
+static char yes_no_chr(int val)
+{
+	return val ? 'Y' : 'N';
+}
+
+#define SKIP_DPRX_CAPS_CHECK		BIT(0)
+#define ALLOW_ALLOCATED_BW_CHANGE	BIT(1)
+
+static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr,
+				  const struct drm_dp_tunnel_regs *regs,
+				  unsigned int flags)
+{
+	int drv_group_id = tunnel_reg_drv_group_id(regs);
+	bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK);
+	bool ret = true;
+
+	if (!tunnel_reg_bw_alloc_supported(regs)) {
+		if (tunnel_group_id(drv_group_id)) {
+			drm_dbg_kms(mgr->dev,
+				    "DPTUN: A non-zero group ID is only allowed with BWA support\n");
+			ret = false;
+		}
+
+		if (tunnel_reg(regs, DP_ALLOCATED_BW)) {
+			drm_dbg_kms(mgr->dev,
+				    "DPTUN: BW is allocated without BWA support\n");
+			ret = false;
+		}
+
+		return ret;
+	}
+
+	if (!tunnel_group_id(drv_group_id)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: BWA support requires a non-zero group ID\n");
+		ret = false;
+	}
+
+	if (check_dprx && hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Invalid DPRX lane count: %d\n",
+			    tunnel_reg_max_dprx_lane_count(regs));
+
+		ret = false;
+	}
+
+	if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: DPRX rate is 0\n");
+
+		ret = false;
+	}
+
+	if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs, DP_ESTIMATED_BW)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Allocated BW %d > estimated BW %d Mb/s\n",
+			    DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) *
+					 tunnel_reg_bw_granularity(regs)),
+			    DPTUN_BW_ARG(tunnel_reg(regs, DP_ESTIMATED_BW) *
+					 tunnel_reg_bw_granularity(regs)));
+
+		ret = false;
+	}
+
+	return ret;
+}
+
+static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel *tunnel,
+					  const struct drm_dp_tunnel_regs *regs,
+					  unsigned int flags)
+{
+	int new_drv_group_id = tunnel_reg_drv_group_id(regs);
+	bool ret = true;
+
+	if (tunnel->bw_alloc_supported != tunnel_reg_bw_alloc_supported(regs)) {
+		tun_dbg(tunnel,
+			"BW alloc support has changed %c -> %c\n",
+			yes_no_chr(tunnel->bw_alloc_supported),
+			yes_no_chr(tunnel_reg_bw_alloc_supported(regs)));
+
+		ret = false;
+	}
+
+	if (tunnel->group->drv_group_id != new_drv_group_id) {
+		tun_dbg(tunnel,
+			"Driver/group ID has changed %d:%d:* -> %d:%d:*\n",
+			tunnel_group_drv_id(tunnel->group->drv_group_id),
+			tunnel_group_id(tunnel->group->drv_group_id),
+			tunnel_group_drv_id(new_drv_group_id),
+			tunnel_group_id(new_drv_group_id));
+
+		ret = false;
+	}
+
+	if (!tunnel->bw_alloc_supported)
+		return ret;
+
+	if (tunnel->bw_granularity != tunnel_reg_bw_granularity(regs)) {
+		tun_dbg(tunnel,
+			"BW granularity has changed: %d -> %d Mb/s\n",
+			DPTUN_BW_ARG(tunnel->bw_granularity),
+			DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs)));
+
+		ret = false;
+	}
+
+	/*
+	 * On some devices at least the BW alloc mode enabled status is always
+	 * reported as 0, so skip checking that here.
+	 */
+
+	if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) &&
+	    tunnel->allocated_bw !=
+	    tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity) {
+		tun_dbg(tunnel,
+			"Allocated BW has changed: %d -> %d Mb/s\n",
+			DPTUN_BW_ARG(tunnel->allocated_bw),
+			DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity));
+
+		ret = false;
+	}
+
+	return ret;
+}
+
+static int
+read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel,
+			    struct drm_dp_tunnel_regs *regs,
+			    unsigned int flags)
+{
+	int err;
+
+	err = read_tunnel_regs(tunnel->aux, regs);
+	if (err < 0) {
+		drm_dp_tunnel_set_io_error(tunnel);
+
+		return err;
+	}
+
+	if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags))
+		return -EINVAL;
+
+	if (!tunnel_info_changes_are_valid(tunnel, regs, flags))
+		return -EINVAL;
+
+	return 0;
+}
+
+static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const struct drm_dp_tunnel_regs *regs)
+{
+	bool changed = false;
+
+	if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate) {
+		tunnel->max_dprx_rate = tunnel_reg_max_dprx_rate(regs);
+		changed = true;
+	}
+
+	if (tunnel_reg_max_dprx_lane_count(regs) != tunnel->max_dprx_lane_count) {
+		tunnel->max_dprx_lane_count = tunnel_reg_max_dprx_lane_count(regs);
+		changed = true;
+	}
+
+	return changed;
+}
+
+static int dev_id_len(const u8 *dev_id, int max_len)
+{
+	while (max_len && dev_id[max_len - 1] == '\0')
+		max_len--;
+
+	return max_len;
+}
+
+static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel)
+{
+	int bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate,
+					   tunnel->max_dprx_lane_count);
+
+	return min(roundup(bw, tunnel->bw_granularity),
+		   MAX_DP_REQUEST_BW * tunnel->bw_granularity);
+}
+
+static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return min(get_max_dprx_bw(tunnel), tunnel->group->available_bw);
+}
+
+/**
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 712

Quoted 8 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +8 -0
+ * drm_dp_tunnel_detect - Detect DP tunnel on the link
+ * @mgr: Tunnel manager
+ * @aux: DP AUX on which the tunnel will be detected
+ *
+ * Detect if there is any DP tunnel on the link and add it to the tunnel
+ * group's tunnel list.
+ *
+ * Returns 0 on success, negative error code on failure.

The above is buggy, will fix it.

Quoted 309 lines
Quoted 309 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +309 -0
+ */
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		       struct drm_dp_aux *aux)
+{
+	struct drm_dp_tunnel_regs regs;
+	struct drm_dp_tunnel *tunnel;
+	int err;
+
+	err = read_tunnel_regs(aux, &regs);
+	if (err)
+		return ERR_PTR(err);
+
+	if (!(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+	      DP_TUNNELING_SUPPORT))
+		return ERR_PTR(-ENODEV);
+
+	/* The DPRX caps are valid only after enabling BW alloc mode. */
+	if (!tunnel_regs_are_valid(mgr, &regs, SKIP_DPRX_CAPS_CHECK))
+		return ERR_PTR(-EINVAL);
+
+	tunnel = create_tunnel(mgr, aux, &regs);
+	if (!tunnel)
+		return ERR_PTR(-ENOMEM);
+
+	tun_dbg(tunnel,
+		"OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%c BWA-Sup:%c BWA-En:%c\n",
+		DP_TUNNELING_OUI_BYTES,
+			tunnel_reg_ptr(&regs, DP_TUNNELING_OUI),
+		dev_id_len(tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES),
+			tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID),
+		(tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MAJOR_MASK) >>
+			DP_TUNNELING_HW_REV_MAJOR_SHIFT,
+		(tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MINOR_MASK) >>
+			DP_TUNNELING_HW_REV_MINOR_SHIFT,
+		tunnel_reg(&regs, DP_TUNNELING_SW_REV_MAJOR),
+		tunnel_reg(&regs, DP_TUNNELING_SW_REV_MINOR),
+		yes_no_chr(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+			   DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT),
+		yes_no_chr(tunnel->bw_alloc_supported),
+		yes_no_chr(tunnel->bw_alloc_enabled));
+
+	return tunnel;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_detect);
+
+/**
+ * drm_dp_tunnel_destroy - Destroy tunnel object
+ * @tunnel: Tunnel object
+ *
+ * Remove the tunnel from the tunnel topology and destroy it.
+ */
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed))
+		return -ENODEV;
+
+	tun_dbg(tunnel, "destroying\n");
+
+	tunnel->destroyed = true;
+	destroy_tunnel(tunnel);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_destroy);
+
+static int check_tunnel(const struct drm_dp_tunnel *tunnel)
+{
+	if (tunnel->destroyed)
+		return -ENODEV;
+
+	if (tunnel->has_io_error)
+		return -EIO;
+
+	return 0;
+}
+
+static int group_allocated_bw(struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel *tunnel;
+	int group_allocated_bw = 0;
+
+	for_each_tunnel_in_group(group, tunnel) {
+		if (check_tunnel(tunnel) == 0 &&
+		    tunnel->bw_alloc_enabled)
+			group_allocated_bw += tunnel->allocated_bw;
+	}
+
+	return group_allocated_bw;
+}
+
+static int calc_group_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return group_allocated_bw(tunnel->group) -
+	       tunnel->allocated_bw +
+	       tunnel->estimated_bw;
+}
+
+static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
+				     const struct drm_dp_tunnel_regs *regs)
+{
+	struct drm_dp_tunnel *tunnel_iter;
+	int group_available_bw;
+	bool changed;
+
+	tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) * tunnel->bw_granularity;
+
+	if (calc_group_available_bw(tunnel) == tunnel->group->available_bw)
+		return 0;
+
+	for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
+		int err;
+
+		if (tunnel_iter == tunnel)
+			continue;
+
+		if (check_tunnel(tunnel_iter) != 0 ||
+		    !tunnel_iter->bw_alloc_enabled)
+			continue;
+
+		err = drm_dp_dpcd_probe(tunnel_iter->aux, DP_DPCD_REV);
+		if (err) {
+			tun_dbg(tunnel_iter,
+				"Probe failed, assume disconnected (err %pe)\n",
+				ERR_PTR(err));
+			drm_dp_tunnel_set_io_error(tunnel_iter);
+		}
+	}
+
+	group_available_bw = calc_group_available_bw(tunnel);
+
+	tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
+		DPTUN_BW_ARG(tunnel->group->available_bw),
+		DPTUN_BW_ARG(group_available_bw));
+
+	changed = tunnel->group->available_bw != group_available_bw;
+
+	tunnel->group->available_bw = group_available_bw;
+
+	return changed ? 1 : 0;
+}
+
+static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool enable)
+{
+	u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)
+		goto out_err;
+
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)
+		goto out_err;
+
+	tunnel->bw_alloc_enabled = enable;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_enable_bw_alloc: Enable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	if (!tunnel->bw_alloc_supported)
+		return -EOPNOTSUPP;
+
+	if (!tunnel_group_id(tunnel->group->drv_group_id))
+		return -EINVAL;
+
+	err = set_bw_alloc_mode(tunnel, true);
+	if (err)
+		goto out;
+
+	err = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (err) {
+		set_bw_alloc_mode(tunnel, false);
+
+		goto out;
+	}
+
+	if (!tunnel->max_dprx_rate)
+		update_dprx_caps(tunnel, &regs);
+
+	if (tunnel->group->available_bw == -1) {
+		err = update_group_available_bw(tunnel, &regs);
+		if (err > 0)
+			err = 0;
+	}
+out:
+	tun_dbg_stat(tunnel, err,
+		     "Enabling BW alloc mode: DPRX:%dx%d Group alloc:%d/%d Mb/s",
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
+
+/**
+ * drm_dp_tunnel_disable_bw_alloc: Disable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Disable the DP tunnel BW allocation mode on @tunnel.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	err = set_bw_alloc_mode(tunnel, false);
+
+	tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
+
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->bw_alloc_enabled;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
+
+static int bw_req_complete(struct drm_dp_aux *aux, bool *status_changed)
+{
+	u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
+	u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	*status_changed = val & status_change_mask;
+
+	val &= bw_req_mask;
+
+	if (!val)
+		return -EAGAIN;
+
+	if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, val) < 0)
+		return -EIO;
+
+	return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
+}
+
+static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
+	int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
+	unsigned long wait_expires;
+	DEFINE_WAIT(wait);
+	int err;
+
+	/* Atomic check should prevent the following. */
+	if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
+		err = -EINVAL;
+		goto out;
+	}
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW, request_bw) < 0) {
+		err = -EIO;
+		goto out;
+	}
+
+	wait_expires = jiffies + msecs_to_jiffies(3000);
+
+	for (;;) {
+		bool status_changed;
+
+		err = bw_req_complete(tunnel->aux, &status_changed);
+		if (err != -EAGAIN)
+			break;
+
+		if (status_changed) {
+			struct drm_dp_tunnel_regs regs;
+
+			err = read_and_verify_tunnel_regs(tunnel, &regs,
+							  ALLOW_ALLOCATED_BW_CHANGE);
+			if (err)
+				break;
+		}
+
+		if (time_after(jiffies, wait_expires)) {
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 1026

Quoted 5 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+			err = -ETIMEDOUT;
+			break;
+		}
+
+		prepare_to_wait(&mgr->bw_req_queue, &wait, TASK_UNINTERRUPTIBLE);

Shouldn't the prepare_to_wait() be done before checking the condition?

Yes, this order could miss a wake-up between bw_req_complete() and prepare_to_wait(), will move the latter before the former thanks for spotting it.

Quoted 5 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 1027

Quoted 2 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +1 -0
+		schedule_timeout(msecs_to_jiffies(200));

I guess the timeout here saves us, even if we race with the wakeup due to the above.

Yes, it's a poll+IRQ wait but for another reason: the TBT stack on some platforms (ADLP, granted only a development platform) does not raise an interrupt at all. Maybe needs a comment.

Quoted 244 lines
Quoted 244 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +244 -0
+	};
+
+	finish_wait(&mgr->bw_req_queue, &wait);
+
+	if (err)
+		goto out;
+
+	tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
+
+out:
+	tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel: Group alloc:%d/%d Mb/s",
+		     DPTUN_BW_ARG(request_bw * tunnel->bw_granularity),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (err == -EIO)
+		drm_dp_tunnel_set_io_error(tunnel);
+
+	return err;
+}
+
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	return allocate_tunnel_bw(tunnel, bw);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
+
+static int check_and_clear_status_change(struct drm_dp_tunnel *tunnel)
+{
+	u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0)
+		goto out_err;
+
+	val &= mask;
+
+	if (val) {
+		if (drm_dp_dpcd_writeb(tunnel->aux, DP_TUNNELING_STATUS, val) < 0)
+			goto out_err;
+
+		return 1;
+	}
+
+	if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
+		return 0;
+
+	/*
+	 * Check for estimated BW changes explicitly to account for lost
+	 * BW change notifications.
+	 */
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) < 0)
+		goto out_err;
+
+	if (val * tunnel->bw_granularity != tunnel->estimated_bw)
+		return 1;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_update_state: Update DP tunnel SW state with the HW state
+ * @tunnel: Tunnel object
+ *
+ * Update the SW state of @tunnel with the HW state.
+ *
+ * Returns 0 if the state has not changed, 1 if it has changed and got updated
+ * successfully and a negative error code otherwise.
+ */
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	bool changed = false;
+	int ret = check_tunnel(tunnel);
+
+	if (ret < 0)
+		return ret;
+
+	ret = check_and_clear_status_change(tunnel);
+	if (ret < 0)
+		goto out;
+
+	if (!ret)
+		return 0;
+
+	ret = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (ret)
+		goto out;
+
+	if (update_dprx_caps(tunnel, &regs))
+		changed = true;
+
+	ret = update_group_available_bw(tunnel, &regs);
+	if (ret == 1)
+		changed = true;
+
+out:
+	tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
+		     "State update: Changed:%c DPRX:%dx%d Tunnel alloc:%d/%d Group alloc:%d/%d Mb/s",
+		     yes_no_chr(changed),
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(tunnel->allocated_bw),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (ret < 0)
+		return ret;
+
+	if (changed)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_update_state);
+
+/*
+ * Returns 0 if no re-probe is needed, 1 if a re-probe is needed,
+ * a negative error code otherwise.
+ */
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_aux *aux)
+{
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
+		wake_up_all(&mgr->bw_req_queue);
+
+	if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED))
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
+
+/**
+ * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum link rate of the DPRX connected
+ * to @tunnel. Note that this rate will not be limited by the BW limit of the
+ * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
+ * registers.
+ *
+ * Returns the maximum link rate in 10 kbit/s units.
+ */
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_rate;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
+
+/**
+ * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum lane count of the DPRX connected
+ * to @tunnel. Note that this lane count will not be limited by the BW limit of
+ * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
+ * registers.
+ *
+ * Returns the maximum lane count.
+ */
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_lane_count;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
+
+/**
+ * drm_dp_tunnel_available_bw - Query the estimated total available BW of the tunnel
+ * @tunnel: Tunnel object
+ *
+ * This function is used to query the estimated total available BW of the
+ * tunnel. This includes the currently allocated and free BW for all the
+ * tunnels in @tunnel's group. The available BW is valid only after the BW
+ * allocation mode has been enabled for the tunnel and its state got updated
+ * calling drm_dp_tunnel_update_state().
+ *
+ * Returns the @tunnel group's estimated total available bandwidth in kB/s
+ * units, or -1 if the available BW isn't valid (the BW allocation mode is
+ * not enabled or the tunnel's state hasn't been updated).
+ */
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->group->available_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
+
+static struct drm_dp_tunnel_group_state *
+drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
+				     const struct drm_dp_tunnel *tunnel)
+{
+	return (struct drm_dp_tunnel_group_state *)
+		drm_atomic_get_private_obj_state(state,
+						 &tunnel->group->base);
+}
+
+static struct drm_dp_tunnel_state *
+add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tun_dbg_atomic(tunnel,
+		       "Adding state for tunnel %p to group state %p\n",
+		       tunnel, group_state);
+
+	tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
+	if (!tunnel_state)
+		return NULL;
+
+	tunnel_state->group_state = group_state;
+
+	drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
+
+	INIT_LIST_HEAD(&tunnel_state->node);
+	list_add(&tunnel_state->node, &group_state->tunnel_states);
+
+	return tunnel_state;
+}
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state)
+{
+	tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
+		       "Clearing state for tunnel %p\n",
+		       tunnel_state->tunnel_ref.tunnel);
+
+	list_del(&tunnel_state->node);
+
+	kfree(tunnel_state->stream_bw);
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 1276

Quoted 5 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+	drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
+
+	kfree(tunnel_state);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_clear_state);

That looks like some kind of destructor so the function name doesn't seem to fit.

Right, will rename it.

Quoted 3 lines

Is there even any need to export that since it doesn't look like any kind of high level thing, and it's called from a static function below?

Yes, the export could be only a left-over, will make it static. Btw, the latest version is also available at
https://github.com/ideak/linux/commits/dp_tun_bw_alloc

Quoted 401 lines
Quoted 385 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +385 -0
+
+static void clear_tunnel_group_state(struct drm_dp_tunnel_group_state *group_state)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+	struct drm_dp_tunnel_state *tunnel_state_tmp;
+
+	for_each_tunnel_state_safe(group_state, tunnel_state, tunnel_state_tmp)
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+}
+
+static struct drm_dp_tunnel_state *
+get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	for_each_tunnel_state(group_state, tunnel_state)
+		if (tunnel_state->tunnel_ref.tunnel == tunnel)
+			return tunnel_state;
+
+	return NULL;
+}
+
+static struct drm_dp_tunnel_state *
+get_or_add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+			struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tunnel_state = get_tunnel_state(group_state, tunnel);
+	if (tunnel_state)
+		return tunnel_state;
+
+	return add_tunnel_state(group_state, tunnel);
+}
+
+static struct drm_private_state *
+tunnel_group_duplicate_state(struct drm_private_obj *obj)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(obj->state);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+	if (!group_state)
+		return NULL;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	__drm_atomic_helper_private_obj_duplicate_state(obj, &group_state->base);
+
+	for_each_tunnel_state(to_group_state(obj->state), tunnel_state) {
+		struct drm_dp_tunnel_state *new_tunnel_state;
+
+		new_tunnel_state = get_or_add_tunnel_state(group_state,
+							   tunnel_state->tunnel_ref.tunnel);
+		if (!new_tunnel_state)
+			goto out_free_state;
+
+		new_tunnel_state->stream_mask = tunnel_state->stream_mask;
+		new_tunnel_state->stream_bw = kmemdup(tunnel_state->stream_bw,
+						      sizeof(*tunnel_state->stream_bw) *
+							hweight32(tunnel_state->stream_mask),
+						      GFP_KERNEL);
+
+		if (!new_tunnel_state->stream_bw)
+			goto out_free_state;
+	}
+
+	return &group_state->base;
+
+out_free_state:
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+
+	return NULL;
+}
+
+static void tunnel_group_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(state);
+
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+}
+
+static const struct drm_private_state_funcs tunnel_group_funcs = {
+	.atomic_duplicate_state = tunnel_group_duplicate_state,
+	.atomic_destroy_state = tunnel_group_destroy_state,
+};
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return ERR_CAST(group_state);
+
+	tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
+	if (!tunnel_state)
+		return ERR_PTR(-ENOMEM);
+
+	return tunnel_state;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i)
+		if (to_group(new_group_state->base.obj) == tunnel->group)
+			return get_tunnel_state(new_group_state, tunnel);
+
+	return NULL;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
+
+static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel_group_state *group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+
+	if (!group_state)
+		return false;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	group->mgr = mgr;
+	group->available_bw = -1;
+	INIT_LIST_HEAD(&group->tunnels);
+
+	drm_atomic_private_obj_init(mgr->dev, &group->base, &group_state->base,
+				    &tunnel_group_funcs);
+
+	return true;
+}
+
+static void cleanup_group(struct drm_dp_tunnel_group *group)
+{
+	drm_atomic_private_obj_fini(&group->base);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+	const struct drm_dp_tunnel_state *tunnel_state;
+	u32 stream_mask = 0;
+
+	for_each_tunnel_state(group_state, tunnel_state) {
+		drm_WARN(to_group(group_state->base.obj)->mgr->dev,
+			 tunnel_state->stream_mask & stream_mask,
+			 "[DPTUN %s]: conflicting stream IDs %x (IDs in other tunnels %x)\n",
+			 tunnel_state->tunnel_ref.tunnel->name,
+			 tunnel_state->stream_mask,
+			 stream_mask);
+
+		stream_mask |= tunnel_state->stream_mask;
+	}
+}
+#else
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+}
+#endif
+
+static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
+{
+	return hweight32(stream_mask & (BIT(stream_id) - 1));
+}
+
+static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
+			   unsigned long old_mask, unsigned long new_mask)
+{
+	unsigned long move_mask = old_mask & new_mask;
+	int *new_bws = NULL;
+	int id;
+
+	WARN_ON(!new_mask);
+
+	if (old_mask == new_mask)
+		return 0;
+
+	new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws), GFP_KERNEL);
+	if (!new_bws)
+		return -ENOMEM;
+
+	for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask))
+		new_bws[stream_id_to_idx(new_mask, id)] =
+			tunnel_state->stream_bw[stream_id_to_idx(old_mask, id)];
+
+	kfree(tunnel_state->stream_bw);
+	tunnel_state->stream_bw = new_bws;
+	tunnel_state->stream_mask = new_mask;
+
+	return 0;
+}
+
+static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			 u8 stream_id, int bw)
+{
+	int err;
+
+	err = resize_bw_array(tunnel_state,
+			      tunnel_state->stream_mask,
+			      tunnel_state->stream_mask | BIT(stream_id));
+	if (err)
+		return err;
+
+	tunnel_state->stream_bw[stream_id_to_idx(tunnel_state->stream_mask, stream_id)] = bw;
+
+	return 0;
+}
+
+static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			   u8 stream_id)
+{
+	if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+		return 0;
+	}
+
+	return resize_bw_array(tunnel_state,
+			       tunnel_state->stream_mask,
+			       tunnel_state->stream_mask & ~BIT(stream_id));
+}
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+					 struct drm_dp_tunnel *tunnel,
+					 u8 stream_id, int bw)
+{
+	struct drm_dp_tunnel_group_state *new_group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+	int err;
+
+	if (drm_WARN_ON(tunnel->group->mgr->dev,
+			stream_id > BITS_PER_TYPE(tunnel_state->stream_mask)))
+		return -EINVAL;
+
+	tun_dbg(tunnel,
+		"Setting %d Mb/s for stream %d\n",
+		DPTUN_BW_ARG(bw), stream_id);
+
+	if (bw == 0) {
+		tunnel_state = get_tunnel_state(new_group_state, tunnel);
+		if (!tunnel_state)
+			return 0;
+
+		return clear_stream_bw(tunnel_state, stream_id);
+	}
+
+	tunnel_state = get_or_add_tunnel_state(new_group_state, tunnel);
+	if (drm_WARN_ON(state->dev, !tunnel_state))
+		return -EINVAL;
+
+	err = set_stream_bw(tunnel_state, stream_id, bw);
+	if (err)
+		return err;
+
+	check_unique_stream_ids(new_group_state);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	int tunnel_bw = 0;
+	int i;
+
+	for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
+		tunnel_bw += tunnel_state->stream_bw[i];
+
+	return tunnel_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_tunnel_bw);
+
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return PTR_ERR(group_state);
+
+	*stream_mask = 0;
+	for_each_tunnel_state(group_state, tunnel_state)
+		*stream_mask |= tunnel_state->stream_mask;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
+
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
+				    u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
+	struct drm_dp_tunnel_state *new_tunnel_state;
+	u32 group_stream_mask = 0;
+	int group_bw = 0;
+
+	for_each_tunnel_state(new_group_state, new_tunnel_state) {
+		struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;
+		int max_dprx_bw = get_max_dprx_bw(tunnel);
+		int tunnel_bw = drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);
+
+		tun_dbg(tunnel,
+			"%sRequired %d/%d Mb/s total for tunnel.\n",
+			tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
+			DPTUN_BW_ARG(tunnel_bw),
+			DPTUN_BW_ARG(max_dprx_bw));
+
+		if (tunnel_bw > max_dprx_bw) {
+			*failed_stream_mask = new_tunnel_state->stream_mask;
+			return -ENOSPC;
+		}
+
+		group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
+				max_dprx_bw);
+		group_stream_mask |= new_tunnel_state->stream_mask;
+	}
+
+	tun_grp_dbg(group,
+		    "%sRequired %d/%d Mb/s total for tunnel group.\n",
+		    group_bw > group->available_bw ? "Not enough BW: " : "",
+		    DPTUN_BW_ARG(group_bw),
+		    DPTUN_BW_ARG(group->available_bw));
+
+	if (group_bw > group->available_bw) {
+		*failed_stream_mask = group_stream_mask;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i) {
+		int ret;
+
+		ret = drm_dp_tunnel_atomic_check_group_bw(new_group_state,
+							  failed_stream_mask);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
+
+static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
+{
+	int i;
+
+	for (i = 0; i < mgr->group_count; i++) {
+		cleanup_group(&mgr->groups[i]);
+		drm_WARN_ON(mgr->dev, !list_empty(&mgr->groups[i].tunnels));
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_exit(&mgr->ref_tracker);
+#endif
+
+	kfree(mgr->groups);
+	kfree(mgr);
+}
+
+/**
+ * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
+ * @i915: i915 driver object

drivers/gpu/drm/display/drm_dp_tunnel.c line 1671

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+ *
+ * Creates a DP tunnel manager.
+ *
+ * Returns a pointer to the tunnel manager if created successfully or NULL in
+ * case of an error.
+ */
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	struct drm_dp_tunnel_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);

I dislike it when functions that can fail or that have side effects are called from the variable declaration block. There's quite a bit of that in this patch. IMO it's far too easy to overlook such function calls.

Quoted 4 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 1673

Quoted 2 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +2 -0
+	int i;
+

ie. the kzalloc() should be here IMO.

Okay, will change this and other places.

Quoted 396 lines
Quoted 392 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +42 -0
+	if (!mgr)
+		return NULL;
+
+	mgr->dev = dev;
+	init_waitqueue_head(&mgr->bw_req_queue);
+
+	mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups), GFP_KERNEL);
+	if (!mgr->groups) {
+		kfree(mgr);
+
+		return NULL;
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
+#endif
+
+	for (i = 0; i < max_group_count; i++) {
+		if (!init_group(mgr, &mgr->groups[i])) {
+			destroy_mgr(mgr);
+
+			return NULL;
+		}
+
+		mgr->group_count++;
+	}
+
+	return mgr;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
+
+/**
+ * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
+ * @mgr: Tunnel manager object
+ *
+ * Destroy the tunnel manager.
+ */
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
+{
+	destroy_mgr(mgr);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);
include/drm/display/drm_dp.h +60 -0
index 281afff6ee4e5..8bfd5d007be8d 100644
@@ -1382,6 +1382,66 @@
 #define DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET	0x69494
 #define DP_HDCP_2_2_REG_DBG_OFFSET		0x69518
 
+/* DP-tunneling */
+#define DP_TUNNELING_OUI				0xe0000
+#define  DP_TUNNELING_OUI_BYTES				3
+
+#define DP_TUNNELING_DEV_ID				0xe0003
+#define  DP_TUNNELING_DEV_ID_BYTES			6
+
+#define DP_TUNNELING_HW_REV				0xe0009
+#define  DP_TUNNELING_HW_REV_MAJOR_SHIFT		4
+#define  DP_TUNNELING_HW_REV_MAJOR_MASK			(0xf << DP_TUNNELING_HW_REV_MAJOR_SHIFT)
+#define  DP_TUNNELING_HW_REV_MINOR_SHIFT		0
+#define  DP_TUNNELING_HW_REV_MINOR_MASK			(0xf << DP_TUNNELING_HW_REV_MINOR_SHIFT)
+
+#define DP_TUNNELING_SW_REV_MAJOR			0xe000a
+#define DP_TUNNELING_SW_REV_MINOR			0xe000b
+
+#define DP_TUNNELING_CAPABILITIES			0xe000d
+#define  DP_IN_BW_ALLOCATION_MODE_SUPPORT		(1 << 7)
+#define  DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT		(1 << 6)
+#define  DP_TUNNELING_SUPPORT				(1 << 0)
+
+#define DP_IN_ADAPTER_INFO				0xe000e
+#define  DP_IN_ADAPTER_NUMBER_BITS			7
+#define  DP_IN_ADAPTER_NUMBER_MASK			((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1)
+
+#define DP_USB4_DRIVER_ID				0xe000f
+#define  DP_USB4_DRIVER_ID_BITS				4
+#define  DP_USB4_DRIVER_ID_MASK				((1 << DP_USB4_DRIVER_ID_BITS) - 1)
+
+#define DP_USB4_DRIVER_BW_CAPABILITY			0xe0020
+#define  DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT	(1 << 7)
+
+#define DP_IN_ADAPTER_TUNNEL_INFORMATION		0xe0021
+#define  DP_GROUP_ID_BITS				3
+#define  DP_GROUP_ID_MASK				((1 << DP_GROUP_ID_BITS) - 1)
+
+#define DP_BW_GRANULARITY				0xe0022
+#define  DP_BW_GRANULARITY_MASK				0x3
+
+#define DP_ESTIMATED_BW					0xe0023
+#define DP_ALLOCATED_BW					0xe0024
+
+#define DP_TUNNELING_STATUS				0xe0025
+#define  DP_BW_ALLOCATION_CAPABILITY_CHANGED		(1 << 3)
+#define  DP_ESTIMATED_BW_CHANGED			(1 << 2)
+#define  DP_BW_REQUEST_SUCCEEDED			(1 << 1)
+#define  DP_BW_REQUEST_FAILED				(1 << 0)
+
+#define DP_TUNNELING_MAX_LINK_RATE			0xe0028
+
+#define DP_TUNNELING_MAX_LANE_COUNT			0xe0029
+#define  DP_TUNNELING_MAX_LANE_COUNT_MASK		0x1f
+
+#define DP_DPTX_BW_ALLOCATION_MODE_CONTROL		0xe0030
+#define  DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE	(1 << 7)
+#define  DP_UNMASK_BW_ALLOCATION_IRQ			(1 << 6)
+
+#define DP_REQUEST_BW					0xe0031
+#define  MAX_DP_REQUEST_BW				255
+
 /* LTTPR: Link Training (LT)-tunable PHY Repeaters */
 #define DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV 0xf0000 /* 1.3 */
 #define DP_MAX_LINK_RATE_PHY_REPEATER			    0xf0001 /* 1.4a */
include/drm/display/drm_dp_tunnel.h new file +270 -0
new file mode 100644
index 0000000000000..f6449b1b4e6e9
@@ -0,0 +1,270 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __DRM_DP_TUNNEL_H__
+#define __DRM_DP_TUNNEL_H__
+
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+
+struct drm_dp_aux;
+
+struct drm_device;
+
+struct drm_atomic_state;
+struct drm_dp_tunnel_mgr;
+struct drm_dp_tunnel_state;
+
+struct ref_tracker;
+
+struct drm_dp_tunnel_ref {
+	struct drm_dp_tunnel *tunnel;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker *tracker;
+#endif
+};
+
+#ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel);
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel);
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+
+void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+#else
+#define drm_dp_tunnel_get(tunnel, tracker) \
+	drm_dp_tunnel_get_untracked(tunnel)
+
+#define drm_dp_tunnel_put(tunnel, tracker) \
+	drm_dp_tunnel_put_untracked(tunnel)
+
+#endif
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	tunnel_ref->tunnel = drm_dp_tunnel_get(tunnel, &tunnel_ref->tracker);
+}
+
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	drm_dp_tunnel_put(tunnel_ref->tunnel, &tunnel_ref->tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux);
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel);
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw);
+int drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			     struct drm_dp_aux *aux);
+
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel);
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel);
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state);
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				       struct drm_dp_tunnel *tunnel,
+				       u8 stream_id, int bw);
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask);
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state);
+
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count);
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr);
+
+#else
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker) {}
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref) {}
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline int
+drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return false;
+}
+
+static inline int
+drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel) {}
+static inline int
+drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			 struct drm_dp_aux *aux)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return -1;
+}
+
+static inline const char *
+drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline void
+drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state) {}
+
+static inline int
+drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				   struct drm_dp_tunnel *tunnel,
+				   u8 stream_id, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						const struct drm_dp_tunnel *tunnel,
+						u32 *stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+				      u32 *failed_stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	return 0;
+}
+
+static inline struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr) {}
+
+
+#endif /* CONFIG_DRM_DISPLAY_DP_TUNNEL */
+
+#endif /* __DRM_DP_TUNNEL_H__ */
Signature · 1 line
2.39.2
Signature · 2 lines
Ville Syrjälä
Intel
#10 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 20 lines · Imre Deak
Quoted 10 lines · Ville Syrjälä
Quoted 9 lines · Imre Deak
drivers/gpu/drm/display/drm_dp_tunnel.c new file +9 -0
+static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
+			       struct ref_tracker **tracker)
+{
+	ref_tracker_free(&tunnel->group->mgr->ref_tracker,
+			 tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 402

Quoted 6 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+{
+	track_tunnel_ref(tunnel, NULL);
+
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);

Why do these exist?

Quoted 2 lines

They implement drm_dp_tunnel_get()/put() if
CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE=n.

Why does that kind of irrelevant detail need to be visible in the exported api?

Signature · 2 lines
Ville Syrjälä
Intel
author
#11 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 21 lines · Ville Syrjälä
Quoted 20 lines · Imre Deak
Quoted 18 lines · Ville Syrjälä
Quoted 5 lines · Imre Deak
drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
+			       struct ref_tracker **tracker)
+{
+	ref_tracker_free(&tunnel->group->mgr->ref_tracker,
+			 tracker);

drivers/gpu/drm/display/drm_dp_tunnel.c line 402

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	track_tunnel_ref(tunnel, NULL);
+
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);

Why do these exist?

Quoted 5 lines
Quoted 2 lines

They implement drm_dp_tunnel_get()/put() if
CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE=n.

Why does that kind of irrelevant detail need to be visible in the exported api?

In non-debug builds the ref_tracker object isn't needed and so drm_dp_tunnel_ref won't contain a pointer to it either. drm_dp_tunnel_get/put_untracked() provide a way to get/put a tunnel reference without having to pass a ref_tracker pointer.

Quoted 4 lines
Signature · 3 lines
-- 
Ville Syrjälä
Intel
#12 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 22 lines · Imre Deak
Quoted 21 lines · Ville Syrjälä
Quoted 20 lines · Imre Deak
Quoted 18 lines · Ville Syrjälä
Quoted 5 lines · Imre Deak
drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
+			       struct ref_tracker **tracker)
+{
+	ref_tracker_free(&tunnel->group->mgr->ref_tracker,
+			 tracker);

drivers/gpu/drm/display/drm_dp_tunnel.c line 402

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	track_tunnel_ref(tunnel, NULL);
+
+	return tunnel_get(tunnel);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_get_untracked);

Why do these exist?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

They implement drm_dp_tunnel_get()/put() if
CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE=n.

Why does that kind of irrelevant detail need to be visible in the exported api?

In non-debug builds the ref_tracker object isn't needed and so drm_dp_tunnel_ref won't contain a pointer to it either.

Since it's just a pointer I don't see much point in making things more complicated by leaving it out.

Quoted 7 lines

drm_dp_tunnel_get/put_untracked() provide a way to get/put a tunnel reference without having to pass a ref_tracker pointer.

Quoted 4 lines
Signature · 3 lines
-- 
Ville Syrjälä
Intel
Signature · 2 lines
Ville Syrjälä
Intel
#13 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

In reply to Imre Deak

drivers/gpu/drm/display/drm_dp_tunnel.c line 520

Quoted 4 lines · Imre Deak
drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+static char yes_no_chr(int val)
+{
+	return val ? 'Y' : 'N';
+}

We have str_yes_no() already.

v> +

Quoted 95 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +95 -0
+#define SKIP_DPRX_CAPS_CHECK		BIT(0)
+#define ALLOW_ALLOCATED_BW_CHANGE	BIT(1)
+
+static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr,
+				  const struct drm_dp_tunnel_regs *regs,
+				  unsigned int flags)
+{
+	int drv_group_id = tunnel_reg_drv_group_id(regs);
+	bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK);
+	bool ret = true;
+
+	if (!tunnel_reg_bw_alloc_supported(regs)) {
+		if (tunnel_group_id(drv_group_id)) {
+			drm_dbg_kms(mgr->dev,
+				    "DPTUN: A non-zero group ID is only allowed with BWA support\n");
+			ret = false;
+		}
+
+		if (tunnel_reg(regs, DP_ALLOCATED_BW)) {
+			drm_dbg_kms(mgr->dev,
+				    "DPTUN: BW is allocated without BWA support\n");
+			ret = false;
+		}
+
+		return ret;
+	}
+
+	if (!tunnel_group_id(drv_group_id)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: BWA support requires a non-zero group ID\n");
+		ret = false;
+	}
+
+	if (check_dprx && hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Invalid DPRX lane count: %d\n",
+			    tunnel_reg_max_dprx_lane_count(regs));
+
+		ret = false;
+	}
+
+	if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: DPRX rate is 0\n");
+
+		ret = false;
+	}
+
+	if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs, DP_ESTIMATED_BW)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Allocated BW %d > estimated BW %d Mb/s\n",
+			    DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) *
+					 tunnel_reg_bw_granularity(regs)),
+			    DPTUN_BW_ARG(tunnel_reg(regs, DP_ESTIMATED_BW) *
+					 tunnel_reg_bw_granularity(regs)));
+
+		ret = false;
+	}
+
+	return ret;
+}
+
+static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel *tunnel,
+					  const struct drm_dp_tunnel_regs *regs,
+					  unsigned int flags)
+{
+	int new_drv_group_id = tunnel_reg_drv_group_id(regs);
+	bool ret = true;
+
+	if (tunnel->bw_alloc_supported != tunnel_reg_bw_alloc_supported(regs)) {
+		tun_dbg(tunnel,
+			"BW alloc support has changed %c -> %c\n",
+			yes_no_chr(tunnel->bw_alloc_supported),
+			yes_no_chr(tunnel_reg_bw_alloc_supported(regs)));
+
+		ret = false;
+	}
+
+	if (tunnel->group->drv_group_id != new_drv_group_id) {
+		tun_dbg(tunnel,
+			"Driver/group ID has changed %d:%d:* -> %d:%d:*\n",
+			tunnel_group_drv_id(tunnel->group->drv_group_id),
+			tunnel_group_id(tunnel->group->drv_group_id),
+			tunnel_group_drv_id(new_drv_group_id),
+			tunnel_group_id(new_drv_group_id));
+
+		ret = false;
+	}
+
+	if (!tunnel->bw_alloc_supported)
+		return ret;
+
+	if (tunnel->bw_granularity != tunnel_reg_bw_granularity(regs)) {
+		tun_dbg(tunnel,
+			"BW granularity has changed: %d -> %d Mb/s\n",

drivers/gpu/drm/display/drm_dp_tunnel.c line 626

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+			DPTUN_BW_ARG(tunnel->bw_granularity),
+			DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs)));
+
+		ret = false;
+	}
+
+	/*
+	 * On some devices at least the BW alloc mode enabled status is always
+	 * reported as 0, so skip checking that here.
+	 */

So it's reported as supported and we enable it, but it's never reported back as being enabled?

Quoted 59 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +59 -0
+
+	if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) &&
+	    tunnel->allocated_bw !=
+	    tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity) {
+		tun_dbg(tunnel,
+			"Allocated BW has changed: %d -> %d Mb/s\n",
+			DPTUN_BW_ARG(tunnel->allocated_bw),
+			DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity));
+
+		ret = false;
+	}
+
+	return ret;
+}
+
+static int
+read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel,
+			    struct drm_dp_tunnel_regs *regs,
+			    unsigned int flags)
+{
+	int err;
+
+	err = read_tunnel_regs(tunnel->aux, regs);
+	if (err < 0) {
+		drm_dp_tunnel_set_io_error(tunnel);
+
+		return err;
+	}
+
+	if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags))
+		return -EINVAL;
+
+	if (!tunnel_info_changes_are_valid(tunnel, regs, flags))
+		return -EINVAL;
+
+	return 0;
+}
+
+static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const struct drm_dp_tunnel_regs *regs)
+{
+	bool changed = false;
+
+	if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate) {
+		tunnel->max_dprx_rate = tunnel_reg_max_dprx_rate(regs);
+		changed = true;
+	}
+
+	if (tunnel_reg_max_dprx_lane_count(regs) != tunnel->max_dprx_lane_count) {
+		tunnel->max_dprx_lane_count = tunnel_reg_max_dprx_lane_count(regs);
+		changed = true;
+	}
+
+	return changed;
+}
+
+static int dev_id_len(const u8 *dev_id, int max_len)
+{
+	while (max_len && dev_id[max_len - 1] == '\0')
+		max_len--;

drivers/gpu/drm/display/drm_dp_tunnel.c line 695

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+
+	return max_len;
+}
+
+static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel)
+{
+	int bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate,
+					   tunnel->max_dprx_lane_count);
+
+	return min(roundup(bw, tunnel->bw_granularity),

Should this round down?

Quoted 103 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +103 -0
+		   MAX_DP_REQUEST_BW * tunnel->bw_granularity);
+}
+
+static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return min(get_max_dprx_bw(tunnel), tunnel->group->available_bw);
+}
+
+/**
+ * drm_dp_tunnel_detect - Detect DP tunnel on the link
+ * @mgr: Tunnel manager
+ * @aux: DP AUX on which the tunnel will be detected
+ *
+ * Detect if there is any DP tunnel on the link and add it to the tunnel
+ * group's tunnel list.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		       struct drm_dp_aux *aux)
+{
+	struct drm_dp_tunnel_regs regs;
+	struct drm_dp_tunnel *tunnel;
+	int err;
+
+	err = read_tunnel_regs(aux, &regs);
+	if (err)
+		return ERR_PTR(err);
+
+	if (!(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+	      DP_TUNNELING_SUPPORT))
+		return ERR_PTR(-ENODEV);
+
+	/* The DPRX caps are valid only after enabling BW alloc mode. */
+	if (!tunnel_regs_are_valid(mgr, &regs, SKIP_DPRX_CAPS_CHECK))
+		return ERR_PTR(-EINVAL);
+
+	tunnel = create_tunnel(mgr, aux, &regs);
+	if (!tunnel)
+		return ERR_PTR(-ENOMEM);
+
+	tun_dbg(tunnel,
+		"OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%c BWA-Sup:%c BWA-En:%c\n",
+		DP_TUNNELING_OUI_BYTES,
+			tunnel_reg_ptr(&regs, DP_TUNNELING_OUI),
+		dev_id_len(tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES),
+			tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID),
+		(tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MAJOR_MASK) >>
+			DP_TUNNELING_HW_REV_MAJOR_SHIFT,
+		(tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MINOR_MASK) >>
+			DP_TUNNELING_HW_REV_MINOR_SHIFT,
+		tunnel_reg(&regs, DP_TUNNELING_SW_REV_MAJOR),
+		tunnel_reg(&regs, DP_TUNNELING_SW_REV_MINOR),
+		yes_no_chr(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+			   DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT),
+		yes_no_chr(tunnel->bw_alloc_supported),
+		yes_no_chr(tunnel->bw_alloc_enabled));
+
+	return tunnel;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_detect);
+
+/**
+ * drm_dp_tunnel_destroy - Destroy tunnel object
+ * @tunnel: Tunnel object
+ *
+ * Remove the tunnel from the tunnel topology and destroy it.
+ */
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed))
+		return -ENODEV;
+
+	tun_dbg(tunnel, "destroying\n");
+
+	tunnel->destroyed = true;
+	destroy_tunnel(tunnel);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_destroy);
+
+static int check_tunnel(const struct drm_dp_tunnel *tunnel)
+{
+	if (tunnel->destroyed)
+		return -ENODEV;
+
+	if (tunnel->has_io_error)
+		return -EIO;
+
+	return 0;
+}
+
+static int group_allocated_bw(struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel *tunnel;
+	int group_allocated_bw = 0;
+
+	for_each_tunnel_in_group(group, tunnel) {
+		if (check_tunnel(tunnel) == 0 &&
+		    tunnel->bw_alloc_enabled)
+			group_allocated_bw += tunnel->allocated_bw;

drivers/gpu/drm/display/drm_dp_tunnel.c line 808

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+	}
+
+	return group_allocated_bw;
+}
+
+static int calc_group_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return group_allocated_bw(tunnel->group) -
+	       tunnel->allocated_bw +
+	       tunnel->estimated_bw;

Hmm. So the estimated_bw=actually_free_bw + tunnel->allocated_bw? Ie. how much bw might be available for this tunnel right now? And here we're trying to deduce the total bandwidth available by adding in the allocated_bw of all the other tunnels in the group? Rather weird that we can't just get that number directly...

Quoted 781 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +781 -0
+}
+
+static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
+				     const struct drm_dp_tunnel_regs *regs)
+{
+	struct drm_dp_tunnel *tunnel_iter;
+	int group_available_bw;
+	bool changed;
+
+	tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) * tunnel->bw_granularity;
+
+	if (calc_group_available_bw(tunnel) == tunnel->group->available_bw)
+		return 0;
+
+	for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
+		int err;
+
+		if (tunnel_iter == tunnel)
+			continue;
+
+		if (check_tunnel(tunnel_iter) != 0 ||
+		    !tunnel_iter->bw_alloc_enabled)
+			continue;
+
+		err = drm_dp_dpcd_probe(tunnel_iter->aux, DP_DPCD_REV);
+		if (err) {
+			tun_dbg(tunnel_iter,
+				"Probe failed, assume disconnected (err %pe)\n",
+				ERR_PTR(err));
+			drm_dp_tunnel_set_io_error(tunnel_iter);
+		}
+	}
+
+	group_available_bw = calc_group_available_bw(tunnel);
+
+	tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
+		DPTUN_BW_ARG(tunnel->group->available_bw),
+		DPTUN_BW_ARG(group_available_bw));
+
+	changed = tunnel->group->available_bw != group_available_bw;
+
+	tunnel->group->available_bw = group_available_bw;
+
+	return changed ? 1 : 0;
+}
+
+static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool enable)
+{
+	u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)
+		goto out_err;
+
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)
+		goto out_err;
+
+	tunnel->bw_alloc_enabled = enable;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_enable_bw_alloc: Enable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	if (!tunnel->bw_alloc_supported)
+		return -EOPNOTSUPP;
+
+	if (!tunnel_group_id(tunnel->group->drv_group_id))
+		return -EINVAL;
+
+	err = set_bw_alloc_mode(tunnel, true);
+	if (err)
+		goto out;
+
+	err = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (err) {
+		set_bw_alloc_mode(tunnel, false);
+
+		goto out;
+	}
+
+	if (!tunnel->max_dprx_rate)
+		update_dprx_caps(tunnel, &regs);
+
+	if (tunnel->group->available_bw == -1) {
+		err = update_group_available_bw(tunnel, &regs);
+		if (err > 0)
+			err = 0;
+	}
+out:
+	tun_dbg_stat(tunnel, err,
+		     "Enabling BW alloc mode: DPRX:%dx%d Group alloc:%d/%d Mb/s",
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
+
+/**
+ * drm_dp_tunnel_disable_bw_alloc: Disable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Disable the DP tunnel BW allocation mode on @tunnel.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	err = set_bw_alloc_mode(tunnel, false);
+
+	tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
+
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->bw_alloc_enabled;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
+
+static int bw_req_complete(struct drm_dp_aux *aux, bool *status_changed)
+{
+	u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
+	u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	*status_changed = val & status_change_mask;
+
+	val &= bw_req_mask;
+
+	if (!val)
+		return -EAGAIN;
+
+	if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, val) < 0)
+		return -EIO;
+
+	return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
+}
+
+static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
+	int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
+	unsigned long wait_expires;
+	DEFINE_WAIT(wait);
+	int err;
+
+	/* Atomic check should prevent the following. */
+	if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
+		err = -EINVAL;
+		goto out;
+	}
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW, request_bw) < 0) {
+		err = -EIO;
+		goto out;
+	}
+
+	wait_expires = jiffies + msecs_to_jiffies(3000);
+
+	for (;;) {
+		bool status_changed;
+
+		err = bw_req_complete(tunnel->aux, &status_changed);
+		if (err != -EAGAIN)
+			break;
+
+		if (status_changed) {
+			struct drm_dp_tunnel_regs regs;
+
+			err = read_and_verify_tunnel_regs(tunnel, &regs,
+							  ALLOW_ALLOCATED_BW_CHANGE);
+			if (err)
+				break;
+		}
+
+		if (time_after(jiffies, wait_expires)) {
+			err = -ETIMEDOUT;
+			break;
+		}
+
+		prepare_to_wait(&mgr->bw_req_queue, &wait, TASK_UNINTERRUPTIBLE);
+		schedule_timeout(msecs_to_jiffies(200));
+	};
+
+	finish_wait(&mgr->bw_req_queue, &wait);
+
+	if (err)
+		goto out;
+
+	tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
+
+out:
+	tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel: Group alloc:%d/%d Mb/s",
+		     DPTUN_BW_ARG(request_bw * tunnel->bw_granularity),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (err == -EIO)
+		drm_dp_tunnel_set_io_error(tunnel);
+
+	return err;
+}
+
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	return allocate_tunnel_bw(tunnel, bw);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
+
+static int check_and_clear_status_change(struct drm_dp_tunnel *tunnel)
+{
+	u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0)
+		goto out_err;
+
+	val &= mask;
+
+	if (val) {
+		if (drm_dp_dpcd_writeb(tunnel->aux, DP_TUNNELING_STATUS, val) < 0)
+			goto out_err;
+
+		return 1;
+	}
+
+	if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
+		return 0;
+
+	/*
+	 * Check for estimated BW changes explicitly to account for lost
+	 * BW change notifications.
+	 */
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) < 0)
+		goto out_err;
+
+	if (val * tunnel->bw_granularity != tunnel->estimated_bw)
+		return 1;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_update_state: Update DP tunnel SW state with the HW state
+ * @tunnel: Tunnel object
+ *
+ * Update the SW state of @tunnel with the HW state.
+ *
+ * Returns 0 if the state has not changed, 1 if it has changed and got updated
+ * successfully and a negative error code otherwise.
+ */
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	bool changed = false;
+	int ret = check_tunnel(tunnel);
+
+	if (ret < 0)
+		return ret;
+
+	ret = check_and_clear_status_change(tunnel);
+	if (ret < 0)
+		goto out;
+
+	if (!ret)
+		return 0;
+
+	ret = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (ret)
+		goto out;
+
+	if (update_dprx_caps(tunnel, &regs))
+		changed = true;
+
+	ret = update_group_available_bw(tunnel, &regs);
+	if (ret == 1)
+		changed = true;
+
+out:
+	tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
+		     "State update: Changed:%c DPRX:%dx%d Tunnel alloc:%d/%d Group alloc:%d/%d Mb/s",
+		     yes_no_chr(changed),
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(tunnel->allocated_bw),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (ret < 0)
+		return ret;
+
+	if (changed)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_update_state);
+
+/*
+ * Returns 0 if no re-probe is needed, 1 if a re-probe is needed,
+ * a negative error code otherwise.
+ */
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_aux *aux)
+{
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
+		wake_up_all(&mgr->bw_req_queue);
+
+	if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED))
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
+
+/**
+ * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum link rate of the DPRX connected
+ * to @tunnel. Note that this rate will not be limited by the BW limit of the
+ * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
+ * registers.
+ *
+ * Returns the maximum link rate in 10 kbit/s units.
+ */
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_rate;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
+
+/**
+ * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum lane count of the DPRX connected
+ * to @tunnel. Note that this lane count will not be limited by the BW limit of
+ * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
+ * registers.
+ *
+ * Returns the maximum lane count.
+ */
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_lane_count;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
+
+/**
+ * drm_dp_tunnel_available_bw - Query the estimated total available BW of the tunnel
+ * @tunnel: Tunnel object
+ *
+ * This function is used to query the estimated total available BW of the
+ * tunnel. This includes the currently allocated and free BW for all the
+ * tunnels in @tunnel's group. The available BW is valid only after the BW
+ * allocation mode has been enabled for the tunnel and its state got updated
+ * calling drm_dp_tunnel_update_state().
+ *
+ * Returns the @tunnel group's estimated total available bandwidth in kB/s
+ * units, or -1 if the available BW isn't valid (the BW allocation mode is
+ * not enabled or the tunnel's state hasn't been updated).
+ */
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->group->available_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
+
+static struct drm_dp_tunnel_group_state *
+drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
+				     const struct drm_dp_tunnel *tunnel)
+{
+	return (struct drm_dp_tunnel_group_state *)
+		drm_atomic_get_private_obj_state(state,
+						 &tunnel->group->base);
+}
+
+static struct drm_dp_tunnel_state *
+add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tun_dbg_atomic(tunnel,
+		       "Adding state for tunnel %p to group state %p\n",
+		       tunnel, group_state);
+
+	tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
+	if (!tunnel_state)
+		return NULL;
+
+	tunnel_state->group_state = group_state;
+
+	drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
+
+	INIT_LIST_HEAD(&tunnel_state->node);
+	list_add(&tunnel_state->node, &group_state->tunnel_states);
+
+	return tunnel_state;
+}
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state)
+{
+	tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
+		       "Clearing state for tunnel %p\n",
+		       tunnel_state->tunnel_ref.tunnel);
+
+	list_del(&tunnel_state->node);
+
+	kfree(tunnel_state->stream_bw);
+	drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
+
+	kfree(tunnel_state);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_clear_state);
+
+static void clear_tunnel_group_state(struct drm_dp_tunnel_group_state *group_state)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+	struct drm_dp_tunnel_state *tunnel_state_tmp;
+
+	for_each_tunnel_state_safe(group_state, tunnel_state, tunnel_state_tmp)
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+}
+
+static struct drm_dp_tunnel_state *
+get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	for_each_tunnel_state(group_state, tunnel_state)
+		if (tunnel_state->tunnel_ref.tunnel == tunnel)
+			return tunnel_state;
+
+	return NULL;
+}
+
+static struct drm_dp_tunnel_state *
+get_or_add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+			struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tunnel_state = get_tunnel_state(group_state, tunnel);
+	if (tunnel_state)
+		return tunnel_state;
+
+	return add_tunnel_state(group_state, tunnel);
+}
+
+static struct drm_private_state *
+tunnel_group_duplicate_state(struct drm_private_obj *obj)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(obj->state);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+	if (!group_state)
+		return NULL;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	__drm_atomic_helper_private_obj_duplicate_state(obj, &group_state->base);
+
+	for_each_tunnel_state(to_group_state(obj->state), tunnel_state) {
+		struct drm_dp_tunnel_state *new_tunnel_state;
+
+		new_tunnel_state = get_or_add_tunnel_state(group_state,
+							   tunnel_state->tunnel_ref.tunnel);
+		if (!new_tunnel_state)
+			goto out_free_state;
+
+		new_tunnel_state->stream_mask = tunnel_state->stream_mask;
+		new_tunnel_state->stream_bw = kmemdup(tunnel_state->stream_bw,
+						      sizeof(*tunnel_state->stream_bw) *
+							hweight32(tunnel_state->stream_mask),
+						      GFP_KERNEL);
+
+		if (!new_tunnel_state->stream_bw)
+			goto out_free_state;
+	}
+
+	return &group_state->base;
+
+out_free_state:
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+
+	return NULL;
+}
+
+static void tunnel_group_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(state);
+
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+}
+
+static const struct drm_private_state_funcs tunnel_group_funcs = {
+	.atomic_duplicate_state = tunnel_group_duplicate_state,
+	.atomic_destroy_state = tunnel_group_destroy_state,
+};
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return ERR_CAST(group_state);
+
+	tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
+	if (!tunnel_state)
+		return ERR_PTR(-ENOMEM);
+
+	return tunnel_state;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i)
+		if (to_group(new_group_state->base.obj) == tunnel->group)
+			return get_tunnel_state(new_group_state, tunnel);
+
+	return NULL;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
+
+static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel_group_state *group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+
+	if (!group_state)
+		return false;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	group->mgr = mgr;
+	group->available_bw = -1;
+	INIT_LIST_HEAD(&group->tunnels);
+
+	drm_atomic_private_obj_init(mgr->dev, &group->base, &group_state->base,
+				    &tunnel_group_funcs);
+
+	return true;
+}
+
+static void cleanup_group(struct drm_dp_tunnel_group *group)
+{
+	drm_atomic_private_obj_fini(&group->base);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+	const struct drm_dp_tunnel_state *tunnel_state;
+	u32 stream_mask = 0;
+
+	for_each_tunnel_state(group_state, tunnel_state) {
+		drm_WARN(to_group(group_state->base.obj)->mgr->dev,
+			 tunnel_state->stream_mask & stream_mask,
+			 "[DPTUN %s]: conflicting stream IDs %x (IDs in other tunnels %x)\n",
+			 tunnel_state->tunnel_ref.tunnel->name,
+			 tunnel_state->stream_mask,
+			 stream_mask);
+
+		stream_mask |= tunnel_state->stream_mask;
+	}
+}
+#else
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+}
+#endif
+
+static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
+{
+	return hweight32(stream_mask & (BIT(stream_id) - 1));
+}
+
+static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
+			   unsigned long old_mask, unsigned long new_mask)
+{
+	unsigned long move_mask = old_mask & new_mask;
+	int *new_bws = NULL;
+	int id;
+
+	WARN_ON(!new_mask);
+
+	if (old_mask == new_mask)
+		return 0;
+
+	new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws), GFP_KERNEL);
+	if (!new_bws)
+		return -ENOMEM;
+
+	for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask))
+		new_bws[stream_id_to_idx(new_mask, id)] =
+			tunnel_state->stream_bw[stream_id_to_idx(old_mask, id)];
+
+	kfree(tunnel_state->stream_bw);
+	tunnel_state->stream_bw = new_bws;
+	tunnel_state->stream_mask = new_mask;
+
+	return 0;
+}
+
+static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			 u8 stream_id, int bw)
+{
+	int err;
+
+	err = resize_bw_array(tunnel_state,
+			      tunnel_state->stream_mask,
+			      tunnel_state->stream_mask | BIT(stream_id));
+	if (err)
+		return err;
+
+	tunnel_state->stream_bw[stream_id_to_idx(tunnel_state->stream_mask, stream_id)] = bw;
+
+	return 0;
+}
+
+static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			   u8 stream_id)
+{
+	if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+		return 0;
+	}
+
+	return resize_bw_array(tunnel_state,
+			       tunnel_state->stream_mask,
+			       tunnel_state->stream_mask & ~BIT(stream_id));
+}
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+					 struct drm_dp_tunnel *tunnel,
+					 u8 stream_id, int bw)
+{
+	struct drm_dp_tunnel_group_state *new_group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+	int err;
+
+	if (drm_WARN_ON(tunnel->group->mgr->dev,
+			stream_id > BITS_PER_TYPE(tunnel_state->stream_mask)))
+		return -EINVAL;
+
+	tun_dbg(tunnel,
+		"Setting %d Mb/s for stream %d\n",
+		DPTUN_BW_ARG(bw), stream_id);
+
+	if (bw == 0) {
+		tunnel_state = get_tunnel_state(new_group_state, tunnel);
+		if (!tunnel_state)
+			return 0;
+
+		return clear_stream_bw(tunnel_state, stream_id);
+	}
+
+	tunnel_state = get_or_add_tunnel_state(new_group_state, tunnel);
+	if (drm_WARN_ON(state->dev, !tunnel_state))
+		return -EINVAL;
+
+	err = set_stream_bw(tunnel_state, stream_id, bw);
+	if (err)
+		return err;
+
+	check_unique_stream_ids(new_group_state);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	int tunnel_bw = 0;
+	int i;
+
+	for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
+		tunnel_bw += tunnel_state->stream_bw[i];
+
+	return tunnel_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_tunnel_bw);
+
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return PTR_ERR(group_state);
+
+	*stream_mask = 0;
+	for_each_tunnel_state(group_state, tunnel_state)
+		*stream_mask |= tunnel_state->stream_mask;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
+
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
+				    u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
+	struct drm_dp_tunnel_state *new_tunnel_state;
+	u32 group_stream_mask = 0;
+	int group_bw = 0;
+
+	for_each_tunnel_state(new_group_state, new_tunnel_state) {
+		struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;

drivers/gpu/drm/display/drm_dp_tunnel.c line 1599

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+		int max_dprx_bw = get_max_dprx_bw(tunnel);
+		int tunnel_bw = drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);
+
+		tun_dbg(tunnel,
+			"%sRequired %d/%d Mb/s total for tunnel.\n",
+			tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
+			DPTUN_BW_ARG(tunnel_bw),
+			DPTUN_BW_ARG(max_dprx_bw));
+
+		if (tunnel_bw > max_dprx_bw) {

I'm a bit confused why we're checking this here. Aren't we already checking this somewhere else?

Quoted 465 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +116 -0
+			*failed_stream_mask = new_tunnel_state->stream_mask;
+			return -ENOSPC;
+		}
+
+		group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
+				max_dprx_bw);
+		group_stream_mask |= new_tunnel_state->stream_mask;
+	}
+
+	tun_grp_dbg(group,
+		    "%sRequired %d/%d Mb/s total for tunnel group.\n",
+		    group_bw > group->available_bw ? "Not enough BW: " : "",
+		    DPTUN_BW_ARG(group_bw),
+		    DPTUN_BW_ARG(group->available_bw));
+
+	if (group_bw > group->available_bw) {
+		*failed_stream_mask = group_stream_mask;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i) {
+		int ret;
+
+		ret = drm_dp_tunnel_atomic_check_group_bw(new_group_state,
+							  failed_stream_mask);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
+
+static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
+{
+	int i;
+
+	for (i = 0; i < mgr->group_count; i++) {
+		cleanup_group(&mgr->groups[i]);
+		drm_WARN_ON(mgr->dev, !list_empty(&mgr->groups[i].tunnels));
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_exit(&mgr->ref_tracker);
+#endif
+
+	kfree(mgr->groups);
+	kfree(mgr);
+}
+
+/**
+ * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
+ * @i915: i915 driver object
+ *
+ * Creates a DP tunnel manager.
+ *
+ * Returns a pointer to the tunnel manager if created successfully or NULL in
+ * case of an error.
+ */
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	struct drm_dp_tunnel_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
+	int i;
+
+	if (!mgr)
+		return NULL;
+
+	mgr->dev = dev;
+	init_waitqueue_head(&mgr->bw_req_queue);
+
+	mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups), GFP_KERNEL);
+	if (!mgr->groups) {
+		kfree(mgr);
+
+		return NULL;
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
+#endif
+
+	for (i = 0; i < max_group_count; i++) {
+		if (!init_group(mgr, &mgr->groups[i])) {
+			destroy_mgr(mgr);
+
+			return NULL;
+		}
+
+		mgr->group_count++;
+	}
+
+	return mgr;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
+
+/**
+ * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
+ * @mgr: Tunnel manager object
+ *
+ * Destroy the tunnel manager.
+ */
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
+{
+	destroy_mgr(mgr);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);
include/drm/display/drm_dp.h +60 -0
index 281afff6ee4e5..8bfd5d007be8d 100644
@@ -1382,6 +1382,66 @@
 #define DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET	0x69494
 #define DP_HDCP_2_2_REG_DBG_OFFSET		0x69518
 
+/* DP-tunneling */
+#define DP_TUNNELING_OUI				0xe0000
+#define  DP_TUNNELING_OUI_BYTES				3
+
+#define DP_TUNNELING_DEV_ID				0xe0003
+#define  DP_TUNNELING_DEV_ID_BYTES			6
+
+#define DP_TUNNELING_HW_REV				0xe0009
+#define  DP_TUNNELING_HW_REV_MAJOR_SHIFT		4
+#define  DP_TUNNELING_HW_REV_MAJOR_MASK			(0xf << DP_TUNNELING_HW_REV_MAJOR_SHIFT)
+#define  DP_TUNNELING_HW_REV_MINOR_SHIFT		0
+#define  DP_TUNNELING_HW_REV_MINOR_MASK			(0xf << DP_TUNNELING_HW_REV_MINOR_SHIFT)
+
+#define DP_TUNNELING_SW_REV_MAJOR			0xe000a
+#define DP_TUNNELING_SW_REV_MINOR			0xe000b
+
+#define DP_TUNNELING_CAPABILITIES			0xe000d
+#define  DP_IN_BW_ALLOCATION_MODE_SUPPORT		(1 << 7)
+#define  DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT		(1 << 6)
+#define  DP_TUNNELING_SUPPORT				(1 << 0)
+
+#define DP_IN_ADAPTER_INFO				0xe000e
+#define  DP_IN_ADAPTER_NUMBER_BITS			7
+#define  DP_IN_ADAPTER_NUMBER_MASK			((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1)
+
+#define DP_USB4_DRIVER_ID				0xe000f
+#define  DP_USB4_DRIVER_ID_BITS				4
+#define  DP_USB4_DRIVER_ID_MASK				((1 << DP_USB4_DRIVER_ID_BITS) - 1)
+
+#define DP_USB4_DRIVER_BW_CAPABILITY			0xe0020
+#define  DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT	(1 << 7)
+
+#define DP_IN_ADAPTER_TUNNEL_INFORMATION		0xe0021
+#define  DP_GROUP_ID_BITS				3
+#define  DP_GROUP_ID_MASK				((1 << DP_GROUP_ID_BITS) - 1)
+
+#define DP_BW_GRANULARITY				0xe0022
+#define  DP_BW_GRANULARITY_MASK				0x3
+
+#define DP_ESTIMATED_BW					0xe0023
+#define DP_ALLOCATED_BW					0xe0024
+
+#define DP_TUNNELING_STATUS				0xe0025
+#define  DP_BW_ALLOCATION_CAPABILITY_CHANGED		(1 << 3)
+#define  DP_ESTIMATED_BW_CHANGED			(1 << 2)
+#define  DP_BW_REQUEST_SUCCEEDED			(1 << 1)
+#define  DP_BW_REQUEST_FAILED				(1 << 0)
+
+#define DP_TUNNELING_MAX_LINK_RATE			0xe0028
+
+#define DP_TUNNELING_MAX_LANE_COUNT			0xe0029
+#define  DP_TUNNELING_MAX_LANE_COUNT_MASK		0x1f
+
+#define DP_DPTX_BW_ALLOCATION_MODE_CONTROL		0xe0030
+#define  DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE	(1 << 7)
+#define  DP_UNMASK_BW_ALLOCATION_IRQ			(1 << 6)
+
+#define DP_REQUEST_BW					0xe0031
+#define  MAX_DP_REQUEST_BW				255
+
 /* LTTPR: Link Training (LT)-tunable PHY Repeaters */
 #define DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV 0xf0000 /* 1.3 */
 #define DP_MAX_LINK_RATE_PHY_REPEATER			    0xf0001 /* 1.4a */
include/drm/display/drm_dp_tunnel.h new file +270 -0
new file mode 100644
index 0000000000000..f6449b1b4e6e9
@@ -0,0 +1,270 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __DRM_DP_TUNNEL_H__
+#define __DRM_DP_TUNNEL_H__
+
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+
+struct drm_dp_aux;
+
+struct drm_device;
+
+struct drm_atomic_state;
+struct drm_dp_tunnel_mgr;
+struct drm_dp_tunnel_state;
+
+struct ref_tracker;
+
+struct drm_dp_tunnel_ref {
+	struct drm_dp_tunnel *tunnel;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker *tracker;
+#endif
+};
+
+#ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel);
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel);
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+
+void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+#else
+#define drm_dp_tunnel_get(tunnel, tracker) \
+	drm_dp_tunnel_get_untracked(tunnel)
+
+#define drm_dp_tunnel_put(tunnel, tracker) \
+	drm_dp_tunnel_put_untracked(tunnel)
+
+#endif
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	tunnel_ref->tunnel = drm_dp_tunnel_get(tunnel, &tunnel_ref->tracker);
+}
+
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	drm_dp_tunnel_put(tunnel_ref->tunnel, &tunnel_ref->tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux);
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel);
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw);
+int drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			     struct drm_dp_aux *aux);
+
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel);
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel);
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state);
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				       struct drm_dp_tunnel *tunnel,
+				       u8 stream_id, int bw);
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask);
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state);
+
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count);
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr);
+
+#else
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker) {}
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref) {}
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline int
+drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return false;
+}
+
+static inline int
+drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel) {}
+static inline int
+drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			 struct drm_dp_aux *aux)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return -1;
+}
+
+static inline const char *
+drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline void
+drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state) {}
+
+static inline int
+drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				   struct drm_dp_tunnel *tunnel,
+				   u8 stream_id, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						const struct drm_dp_tunnel *tunnel,
+						u32 *stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+				      u32 *failed_stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	return 0;
+}
+
+static inline struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr) {}
+
+
+#endif /* CONFIG_DRM_DISPLAY_DP_TUNNEL */
+
+#endif /* __DRM_DP_TUNNEL_H__ */
Signature · 1 line
2.39.2
Signature · 2 lines
Ville Syrjälä
Intel
author
#14 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 7 lines · Ville Syrjälä

drivers/gpu/drm/display/drm_dp_tunnel.c line 520

Quoted 4 lines · Imre Deak
drivers/gpu/drm/display/drm_dp_tunnel.c new file +4 -0
+static char yes_no_chr(int val)
+{
+	return val ? 'Y' : 'N';
+}

We have str_yes_no() already.

Ok, will use this.

Quoted 101 lines

v> +

Quoted 100 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +100 -0
+#define SKIP_DPRX_CAPS_CHECK		BIT(0)
+#define ALLOW_ALLOCATED_BW_CHANGE	BIT(1)
+
+static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr,
+				  const struct drm_dp_tunnel_regs *regs,
+				  unsigned int flags)
+{
+	int drv_group_id = tunnel_reg_drv_group_id(regs);
+	bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK);
+	bool ret = true;
+
+	if (!tunnel_reg_bw_alloc_supported(regs)) {
+		if (tunnel_group_id(drv_group_id)) {
+			drm_dbg_kms(mgr->dev,
+				    "DPTUN: A non-zero group ID is only allowed with BWA support\n");
+			ret = false;
+		}
+
+		if (tunnel_reg(regs, DP_ALLOCATED_BW)) {
+			drm_dbg_kms(mgr->dev,
+				    "DPTUN: BW is allocated without BWA support\n");
+			ret = false;
+		}
+
+		return ret;
+	}
+
+	if (!tunnel_group_id(drv_group_id)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: BWA support requires a non-zero group ID\n");
+		ret = false;
+	}
+
+	if (check_dprx && hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Invalid DPRX lane count: %d\n",
+			    tunnel_reg_max_dprx_lane_count(regs));
+
+		ret = false;
+	}
+
+	if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: DPRX rate is 0\n");
+
+		ret = false;
+	}
+
+	if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs, DP_ESTIMATED_BW)) {
+		drm_dbg_kms(mgr->dev,
+			    "DPTUN: Allocated BW %d > estimated BW %d Mb/s\n",
+			    DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) *
+					 tunnel_reg_bw_granularity(regs)),
+			    DPTUN_BW_ARG(tunnel_reg(regs, DP_ESTIMATED_BW) *
+					 tunnel_reg_bw_granularity(regs)));
+
+		ret = false;
+	}
+
+	return ret;
+}
+
+static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel *tunnel,
+					  const struct drm_dp_tunnel_regs *regs,
+					  unsigned int flags)
+{
+	int new_drv_group_id = tunnel_reg_drv_group_id(regs);
+	bool ret = true;
+
+	if (tunnel->bw_alloc_supported != tunnel_reg_bw_alloc_supported(regs)) {
+		tun_dbg(tunnel,
+			"BW alloc support has changed %c -> %c\n",
+			yes_no_chr(tunnel->bw_alloc_supported),
+			yes_no_chr(tunnel_reg_bw_alloc_supported(regs)));
+
+		ret = false;
+	}
+
+	if (tunnel->group->drv_group_id != new_drv_group_id) {
+		tun_dbg(tunnel,
+			"Driver/group ID has changed %d:%d:* -> %d:%d:*\n",
+			tunnel_group_drv_id(tunnel->group->drv_group_id),
+			tunnel_group_id(tunnel->group->drv_group_id),
+			tunnel_group_drv_id(new_drv_group_id),
+			tunnel_group_id(new_drv_group_id));
+
+		ret = false;
+	}
+
+	if (!tunnel->bw_alloc_supported)
+		return ret;
+
+	if (tunnel->bw_granularity != tunnel_reg_bw_granularity(regs)) {
+		tun_dbg(tunnel,
+			"BW granularity has changed: %d -> %d Mb/s\n",
+			DPTUN_BW_ARG(tunnel->bw_granularity),
+			DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs)));
+
+		ret = false;
+	}
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 626

Quoted 5 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+
+	/*
+	 * On some devices at least the BW alloc mode enabled status is always
+	 * reported as 0, so skip checking that here.
+	 */

So it's reported as supported and we enable it, but it's never reported back as being enabled?

Yes, at least using an engineering TBT (DP adapter) FW. I'll check if this is fixed already on released platforms/FWs.

Quoted 63 lines
Quoted 63 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +63 -0
+
+	if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) &&
+	    tunnel->allocated_bw !=
+	    tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity) {
+		tun_dbg(tunnel,
+			"Allocated BW has changed: %d -> %d Mb/s\n",
+			DPTUN_BW_ARG(tunnel->allocated_bw),
+			DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity));
+
+		ret = false;
+	}
+
+	return ret;
+}
+
+static int
+read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel,
+			    struct drm_dp_tunnel_regs *regs,
+			    unsigned int flags)
+{
+	int err;
+
+	err = read_tunnel_regs(tunnel->aux, regs);
+	if (err < 0) {
+		drm_dp_tunnel_set_io_error(tunnel);
+
+		return err;
+	}
+
+	if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags))
+		return -EINVAL;
+
+	if (!tunnel_info_changes_are_valid(tunnel, regs, flags))
+		return -EINVAL;
+
+	return 0;
+}
+
+static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const struct drm_dp_tunnel_regs *regs)
+{
+	bool changed = false;
+
+	if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate) {
+		tunnel->max_dprx_rate = tunnel_reg_max_dprx_rate(regs);
+		changed = true;
+	}
+
+	if (tunnel_reg_max_dprx_lane_count(regs) != tunnel->max_dprx_lane_count) {
+		tunnel->max_dprx_lane_count = tunnel_reg_max_dprx_lane_count(regs);
+		changed = true;
+	}
+
+	return changed;
+}
+
+static int dev_id_len(const u8 *dev_id, int max_len)
+{
+	while (max_len && dev_id[max_len - 1] == '\0')
+		max_len--;
+
+	return max_len;
+}
+
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 695

Quoted 6 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel)
+{
+	int bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate,
+					   tunnel->max_dprx_lane_count);
+
+	return min(roundup(bw, tunnel->bw_granularity),

Should this round down?

This should round up: whereas in general the TBT CM (thunderbolt driver) allocates exactly bw=C*bw_granularity in response to C written to DP_REQUEST_BW, if this bw is above the max_dprx_bw=max_dprx_rate * max_dprx_lane_count limit (also known to CM), the CM will allocate only max_dprx_bw. (This is the only way max_dprx_bw can be allocated even if it's not aligned to bw_granularity.) This needs a code comment.

Quoted 107 lines
Quoted 107 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +107 -0
+		   MAX_DP_REQUEST_BW * tunnel->bw_granularity);
+}
+
+static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return min(get_max_dprx_bw(tunnel), tunnel->group->available_bw);
+}
+
+/**
+ * drm_dp_tunnel_detect - Detect DP tunnel on the link
+ * @mgr: Tunnel manager
+ * @aux: DP AUX on which the tunnel will be detected
+ *
+ * Detect if there is any DP tunnel on the link and add it to the tunnel
+ * group's tunnel list.
+ *
+ * Returns 0 on success, negative error code on failure.
+ */
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		       struct drm_dp_aux *aux)
+{
+	struct drm_dp_tunnel_regs regs;
+	struct drm_dp_tunnel *tunnel;
+	int err;
+
+	err = read_tunnel_regs(aux, &regs);
+	if (err)
+		return ERR_PTR(err);
+
+	if (!(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+	      DP_TUNNELING_SUPPORT))
+		return ERR_PTR(-ENODEV);
+
+	/* The DPRX caps are valid only after enabling BW alloc mode. */
+	if (!tunnel_regs_are_valid(mgr, &regs, SKIP_DPRX_CAPS_CHECK))
+		return ERR_PTR(-EINVAL);
+
+	tunnel = create_tunnel(mgr, aux, &regs);
+	if (!tunnel)
+		return ERR_PTR(-ENOMEM);
+
+	tun_dbg(tunnel,
+		"OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%c BWA-Sup:%c BWA-En:%c\n",
+		DP_TUNNELING_OUI_BYTES,
+			tunnel_reg_ptr(&regs, DP_TUNNELING_OUI),
+		dev_id_len(tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES),
+			tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID),
+		(tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MAJOR_MASK) >>
+			DP_TUNNELING_HW_REV_MAJOR_SHIFT,
+		(tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MINOR_MASK) >>
+			DP_TUNNELING_HW_REV_MINOR_SHIFT,
+		tunnel_reg(&regs, DP_TUNNELING_SW_REV_MAJOR),
+		tunnel_reg(&regs, DP_TUNNELING_SW_REV_MINOR),
+		yes_no_chr(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
+			   DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT),
+		yes_no_chr(tunnel->bw_alloc_supported),
+		yes_no_chr(tunnel->bw_alloc_enabled));
+
+	return tunnel;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_detect);
+
+/**
+ * drm_dp_tunnel_destroy - Destroy tunnel object
+ * @tunnel: Tunnel object
+ *
+ * Remove the tunnel from the tunnel topology and destroy it.
+ */
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed))
+		return -ENODEV;
+
+	tun_dbg(tunnel, "destroying\n");
+
+	tunnel->destroyed = true;
+	destroy_tunnel(tunnel);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_destroy);
+
+static int check_tunnel(const struct drm_dp_tunnel *tunnel)
+{
+	if (tunnel->destroyed)
+		return -ENODEV;
+
+	if (tunnel->has_io_error)
+		return -EIO;
+
+	return 0;
+}
+
+static int group_allocated_bw(struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel *tunnel;
+	int group_allocated_bw = 0;
+
+	for_each_tunnel_in_group(group, tunnel) {
+		if (check_tunnel(tunnel) == 0 &&
+		    tunnel->bw_alloc_enabled)
+			group_allocated_bw += tunnel->allocated_bw;
+	}
+
+	return group_allocated_bw;
+}
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 808

Quoted 6 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+static int calc_group_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return group_allocated_bw(tunnel->group) -
+	       tunnel->allocated_bw +
+	       tunnel->estimated_bw;

Hmm. So the estimated_bw=actually_free_bw + tunnel->allocated_bw?

Yes.

Quoted 1 line

Ie. how much bw might be available for this tunnel right now?

Correct.

Quoted 2 lines

And here we're trying to deduce the total bandwidth available by adding in the allocated_bw of all the other tunnels in the group?

Yes.

Quoted 1 line

Rather weird that we can't just get that number directly...

It is. Imo this could be simply communicated via a DPCD register dedicated for this. Perhaps adding this should be requested from TBT architects.

I assume this could also use a code comment.

Quoted 786 lines
Quoted 786 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +786 -0
+}
+
+static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
+				     const struct drm_dp_tunnel_regs *regs)
+{
+	struct drm_dp_tunnel *tunnel_iter;
+	int group_available_bw;
+	bool changed;
+
+	tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) * tunnel->bw_granularity;
+
+	if (calc_group_available_bw(tunnel) == tunnel->group->available_bw)
+		return 0;
+
+	for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
+		int err;
+
+		if (tunnel_iter == tunnel)
+			continue;
+
+		if (check_tunnel(tunnel_iter) != 0 ||
+		    !tunnel_iter->bw_alloc_enabled)
+			continue;
+
+		err = drm_dp_dpcd_probe(tunnel_iter->aux, DP_DPCD_REV);
+		if (err) {
+			tun_dbg(tunnel_iter,
+				"Probe failed, assume disconnected (err %pe)\n",
+				ERR_PTR(err));
+			drm_dp_tunnel_set_io_error(tunnel_iter);
+		}
+	}
+
+	group_available_bw = calc_group_available_bw(tunnel);
+
+	tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
+		DPTUN_BW_ARG(tunnel->group->available_bw),
+		DPTUN_BW_ARG(group_available_bw));
+
+	changed = tunnel->group->available_bw != group_available_bw;
+
+	tunnel->group->available_bw = group_available_bw;
+
+	return changed ? 1 : 0;
+}
+
+static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool enable)
+{
+	u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)
+		goto out_err;
+
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)
+		goto out_err;
+
+	tunnel->bw_alloc_enabled = enable;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_enable_bw_alloc: Enable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	if (!tunnel->bw_alloc_supported)
+		return -EOPNOTSUPP;
+
+	if (!tunnel_group_id(tunnel->group->drv_group_id))
+		return -EINVAL;
+
+	err = set_bw_alloc_mode(tunnel, true);
+	if (err)
+		goto out;
+
+	err = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (err) {
+		set_bw_alloc_mode(tunnel, false);
+
+		goto out;
+	}
+
+	if (!tunnel->max_dprx_rate)
+		update_dprx_caps(tunnel, &regs);
+
+	if (tunnel->group->available_bw == -1) {
+		err = update_group_available_bw(tunnel, &regs);
+		if (err > 0)
+			err = 0;
+	}
+out:
+	tun_dbg_stat(tunnel, err,
+		     "Enabling BW alloc mode: DPRX:%dx%d Group alloc:%d/%d Mb/s",
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
+
+/**
+ * drm_dp_tunnel_disable_bw_alloc: Disable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Disable the DP tunnel BW allocation mode on @tunnel.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	err = set_bw_alloc_mode(tunnel, false);
+
+	tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
+
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->bw_alloc_enabled;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
+
+static int bw_req_complete(struct drm_dp_aux *aux, bool *status_changed)
+{
+	u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
+	u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	*status_changed = val & status_change_mask;
+
+	val &= bw_req_mask;
+
+	if (!val)
+		return -EAGAIN;
+
+	if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, val) < 0)
+		return -EIO;
+
+	return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
+}
+
+static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
+	int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
+	unsigned long wait_expires;
+	DEFINE_WAIT(wait);
+	int err;
+
+	/* Atomic check should prevent the following. */
+	if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
+		err = -EINVAL;
+		goto out;
+	}
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW, request_bw) < 0) {
+		err = -EIO;
+		goto out;
+	}
+
+	wait_expires = jiffies + msecs_to_jiffies(3000);
+
+	for (;;) {
+		bool status_changed;
+
+		err = bw_req_complete(tunnel->aux, &status_changed);
+		if (err != -EAGAIN)
+			break;
+
+		if (status_changed) {
+			struct drm_dp_tunnel_regs regs;
+
+			err = read_and_verify_tunnel_regs(tunnel, &regs,
+							  ALLOW_ALLOCATED_BW_CHANGE);
+			if (err)
+				break;
+		}
+
+		if (time_after(jiffies, wait_expires)) {
+			err = -ETIMEDOUT;
+			break;
+		}
+
+		prepare_to_wait(&mgr->bw_req_queue, &wait, TASK_UNINTERRUPTIBLE);
+		schedule_timeout(msecs_to_jiffies(200));
+	};
+
+	finish_wait(&mgr->bw_req_queue, &wait);
+
+	if (err)
+		goto out;
+
+	tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
+
+out:
+	tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel: Group alloc:%d/%d Mb/s",
+		     DPTUN_BW_ARG(request_bw * tunnel->bw_granularity),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (err == -EIO)
+		drm_dp_tunnel_set_io_error(tunnel);
+
+	return err;
+}
+
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	return allocate_tunnel_bw(tunnel, bw);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
+
+static int check_and_clear_status_change(struct drm_dp_tunnel *tunnel)
+{
+	u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0)
+		goto out_err;
+
+	val &= mask;
+
+	if (val) {
+		if (drm_dp_dpcd_writeb(tunnel->aux, DP_TUNNELING_STATUS, val) < 0)
+			goto out_err;
+
+		return 1;
+	}
+
+	if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
+		return 0;
+
+	/*
+	 * Check for estimated BW changes explicitly to account for lost
+	 * BW change notifications.
+	 */
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) < 0)
+		goto out_err;
+
+	if (val * tunnel->bw_granularity != tunnel->estimated_bw)
+		return 1;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_update_state: Update DP tunnel SW state with the HW state
+ * @tunnel: Tunnel object
+ *
+ * Update the SW state of @tunnel with the HW state.
+ *
+ * Returns 0 if the state has not changed, 1 if it has changed and got updated
+ * successfully and a negative error code otherwise.
+ */
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	bool changed = false;
+	int ret = check_tunnel(tunnel);
+
+	if (ret < 0)
+		return ret;
+
+	ret = check_and_clear_status_change(tunnel);
+	if (ret < 0)
+		goto out;
+
+	if (!ret)
+		return 0;
+
+	ret = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (ret)
+		goto out;
+
+	if (update_dprx_caps(tunnel, &regs))
+		changed = true;
+
+	ret = update_group_available_bw(tunnel, &regs);
+	if (ret == 1)
+		changed = true;
+
+out:
+	tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
+		     "State update: Changed:%c DPRX:%dx%d Tunnel alloc:%d/%d Group alloc:%d/%d Mb/s",
+		     yes_no_chr(changed),
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(tunnel->allocated_bw),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (ret < 0)
+		return ret;
+
+	if (changed)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_update_state);
+
+/*
+ * Returns 0 if no re-probe is needed, 1 if a re-probe is needed,
+ * a negative error code otherwise.
+ */
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_aux *aux)
+{
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
+		wake_up_all(&mgr->bw_req_queue);
+
+	if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED))
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
+
+/**
+ * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum link rate of the DPRX connected
+ * to @tunnel. Note that this rate will not be limited by the BW limit of the
+ * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
+ * registers.
+ *
+ * Returns the maximum link rate in 10 kbit/s units.
+ */
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_rate;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
+
+/**
+ * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum lane count of the DPRX connected
+ * to @tunnel. Note that this lane count will not be limited by the BW limit of
+ * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
+ * registers.
+ *
+ * Returns the maximum lane count.
+ */
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_lane_count;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
+
+/**
+ * drm_dp_tunnel_available_bw - Query the estimated total available BW of the tunnel
+ * @tunnel: Tunnel object
+ *
+ * This function is used to query the estimated total available BW of the
+ * tunnel. This includes the currently allocated and free BW for all the
+ * tunnels in @tunnel's group. The available BW is valid only after the BW
+ * allocation mode has been enabled for the tunnel and its state got updated
+ * calling drm_dp_tunnel_update_state().
+ *
+ * Returns the @tunnel group's estimated total available bandwidth in kB/s
+ * units, or -1 if the available BW isn't valid (the BW allocation mode is
+ * not enabled or the tunnel's state hasn't been updated).
+ */
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->group->available_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
+
+static struct drm_dp_tunnel_group_state *
+drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
+				     const struct drm_dp_tunnel *tunnel)
+{
+	return (struct drm_dp_tunnel_group_state *)
+		drm_atomic_get_private_obj_state(state,
+						 &tunnel->group->base);
+}
+
+static struct drm_dp_tunnel_state *
+add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tun_dbg_atomic(tunnel,
+		       "Adding state for tunnel %p to group state %p\n",
+		       tunnel, group_state);
+
+	tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
+	if (!tunnel_state)
+		return NULL;
+
+	tunnel_state->group_state = group_state;
+
+	drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
+
+	INIT_LIST_HEAD(&tunnel_state->node);
+	list_add(&tunnel_state->node, &group_state->tunnel_states);
+
+	return tunnel_state;
+}
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state)
+{
+	tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
+		       "Clearing state for tunnel %p\n",
+		       tunnel_state->tunnel_ref.tunnel);
+
+	list_del(&tunnel_state->node);
+
+	kfree(tunnel_state->stream_bw);
+	drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
+
+	kfree(tunnel_state);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_clear_state);
+
+static void clear_tunnel_group_state(struct drm_dp_tunnel_group_state *group_state)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+	struct drm_dp_tunnel_state *tunnel_state_tmp;
+
+	for_each_tunnel_state_safe(group_state, tunnel_state, tunnel_state_tmp)
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+}
+
+static struct drm_dp_tunnel_state *
+get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	for_each_tunnel_state(group_state, tunnel_state)
+		if (tunnel_state->tunnel_ref.tunnel == tunnel)
+			return tunnel_state;
+
+	return NULL;
+}
+
+static struct drm_dp_tunnel_state *
+get_or_add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+			struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tunnel_state = get_tunnel_state(group_state, tunnel);
+	if (tunnel_state)
+		return tunnel_state;
+
+	return add_tunnel_state(group_state, tunnel);
+}
+
+static struct drm_private_state *
+tunnel_group_duplicate_state(struct drm_private_obj *obj)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(obj->state);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+	if (!group_state)
+		return NULL;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	__drm_atomic_helper_private_obj_duplicate_state(obj, &group_state->base);
+
+	for_each_tunnel_state(to_group_state(obj->state), tunnel_state) {
+		struct drm_dp_tunnel_state *new_tunnel_state;
+
+		new_tunnel_state = get_or_add_tunnel_state(group_state,
+							   tunnel_state->tunnel_ref.tunnel);
+		if (!new_tunnel_state)
+			goto out_free_state;
+
+		new_tunnel_state->stream_mask = tunnel_state->stream_mask;
+		new_tunnel_state->stream_bw = kmemdup(tunnel_state->stream_bw,
+						      sizeof(*tunnel_state->stream_bw) *
+							hweight32(tunnel_state->stream_mask),
+						      GFP_KERNEL);
+
+		if (!new_tunnel_state->stream_bw)
+			goto out_free_state;
+	}
+
+	return &group_state->base;
+
+out_free_state:
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+
+	return NULL;
+}
+
+static void tunnel_group_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(state);
+
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+}
+
+static const struct drm_private_state_funcs tunnel_group_funcs = {
+	.atomic_duplicate_state = tunnel_group_duplicate_state,
+	.atomic_destroy_state = tunnel_group_destroy_state,
+};
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return ERR_CAST(group_state);
+
+	tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
+	if (!tunnel_state)
+		return ERR_PTR(-ENOMEM);
+
+	return tunnel_state;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i)
+		if (to_group(new_group_state->base.obj) == tunnel->group)
+			return get_tunnel_state(new_group_state, tunnel);
+
+	return NULL;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
+
+static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel_group_state *group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+
+	if (!group_state)
+		return false;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	group->mgr = mgr;
+	group->available_bw = -1;
+	INIT_LIST_HEAD(&group->tunnels);
+
+	drm_atomic_private_obj_init(mgr->dev, &group->base, &group_state->base,
+				    &tunnel_group_funcs);
+
+	return true;
+}
+
+static void cleanup_group(struct drm_dp_tunnel_group *group)
+{
+	drm_atomic_private_obj_fini(&group->base);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+	const struct drm_dp_tunnel_state *tunnel_state;
+	u32 stream_mask = 0;
+
+	for_each_tunnel_state(group_state, tunnel_state) {
+		drm_WARN(to_group(group_state->base.obj)->mgr->dev,
+			 tunnel_state->stream_mask & stream_mask,
+			 "[DPTUN %s]: conflicting stream IDs %x (IDs in other tunnels %x)\n",
+			 tunnel_state->tunnel_ref.tunnel->name,
+			 tunnel_state->stream_mask,
+			 stream_mask);
+
+		stream_mask |= tunnel_state->stream_mask;
+	}
+}
+#else
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+}
+#endif
+
+static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
+{
+	return hweight32(stream_mask & (BIT(stream_id) - 1));
+}
+
+static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
+			   unsigned long old_mask, unsigned long new_mask)
+{
+	unsigned long move_mask = old_mask & new_mask;
+	int *new_bws = NULL;
+	int id;
+
+	WARN_ON(!new_mask);
+
+	if (old_mask == new_mask)
+		return 0;
+
+	new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws), GFP_KERNEL);
+	if (!new_bws)
+		return -ENOMEM;
+
+	for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask))
+		new_bws[stream_id_to_idx(new_mask, id)] =
+			tunnel_state->stream_bw[stream_id_to_idx(old_mask, id)];
+
+	kfree(tunnel_state->stream_bw);
+	tunnel_state->stream_bw = new_bws;
+	tunnel_state->stream_mask = new_mask;
+
+	return 0;
+}
+
+static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			 u8 stream_id, int bw)
+{
+	int err;
+
+	err = resize_bw_array(tunnel_state,
+			      tunnel_state->stream_mask,
+			      tunnel_state->stream_mask | BIT(stream_id));
+	if (err)
+		return err;
+
+	tunnel_state->stream_bw[stream_id_to_idx(tunnel_state->stream_mask, stream_id)] = bw;
+
+	return 0;
+}
+
+static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			   u8 stream_id)
+{
+	if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+		return 0;
+	}
+
+	return resize_bw_array(tunnel_state,
+			       tunnel_state->stream_mask,
+			       tunnel_state->stream_mask & ~BIT(stream_id));
+}
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+					 struct drm_dp_tunnel *tunnel,
+					 u8 stream_id, int bw)
+{
+	struct drm_dp_tunnel_group_state *new_group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+	int err;
+
+	if (drm_WARN_ON(tunnel->group->mgr->dev,
+			stream_id > BITS_PER_TYPE(tunnel_state->stream_mask)))
+		return -EINVAL;
+
+	tun_dbg(tunnel,
+		"Setting %d Mb/s for stream %d\n",
+		DPTUN_BW_ARG(bw), stream_id);
+
+	if (bw == 0) {
+		tunnel_state = get_tunnel_state(new_group_state, tunnel);
+		if (!tunnel_state)
+			return 0;
+
+		return clear_stream_bw(tunnel_state, stream_id);
+	}
+
+	tunnel_state = get_or_add_tunnel_state(new_group_state, tunnel);
+	if (drm_WARN_ON(state->dev, !tunnel_state))
+		return -EINVAL;
+
+	err = set_stream_bw(tunnel_state, stream_id, bw);
+	if (err)
+		return err;
+
+	check_unique_stream_ids(new_group_state);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	int tunnel_bw = 0;
+	int i;
+
+	for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
+		tunnel_bw += tunnel_state->stream_bw[i];
+
+	return tunnel_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_tunnel_bw);
+
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return PTR_ERR(group_state);
+
+	*stream_mask = 0;
+	for_each_tunnel_state(group_state, tunnel_state)
+		*stream_mask |= tunnel_state->stream_mask;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
+
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
+				    u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
+	struct drm_dp_tunnel_state *new_tunnel_state;
+	u32 group_stream_mask = 0;
+	int group_bw = 0;
+
+	for_each_tunnel_state(new_group_state, new_tunnel_state) {
+		struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;
+		int max_dprx_bw = get_max_dprx_bw(tunnel);
+		int tunnel_bw = drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);
+
+		tun_dbg(tunnel,
+			"%sRequired %d/%d Mb/s total for tunnel.\n",
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 1599

Quoted 5 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+			tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
+			DPTUN_BW_ARG(tunnel_bw),
+			DPTUN_BW_ARG(max_dprx_bw));
+
+		if (tunnel_bw > max_dprx_bw) {

I'm a bit confused why we're checking this here. Aren't we already checking this somewhere else?

Ah, yes this should be checked already by the encoder compute config + the MST link BW check. It can be removed, thanks.

Quoted 469 lines
Quoted 465 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +116 -0
+			*failed_stream_mask = new_tunnel_state->stream_mask;
+			return -ENOSPC;
+		}
+
+		group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
+				max_dprx_bw);
+		group_stream_mask |= new_tunnel_state->stream_mask;
+	}
+
+	tun_grp_dbg(group,
+		    "%sRequired %d/%d Mb/s total for tunnel group.\n",
+		    group_bw > group->available_bw ? "Not enough BW: " : "",
+		    DPTUN_BW_ARG(group_bw),
+		    DPTUN_BW_ARG(group->available_bw));
+
+	if (group_bw > group->available_bw) {
+		*failed_stream_mask = group_stream_mask;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i) {
+		int ret;
+
+		ret = drm_dp_tunnel_atomic_check_group_bw(new_group_state,
+							  failed_stream_mask);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
+
+static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
+{
+	int i;
+
+	for (i = 0; i < mgr->group_count; i++) {
+		cleanup_group(&mgr->groups[i]);
+		drm_WARN_ON(mgr->dev, !list_empty(&mgr->groups[i].tunnels));
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_exit(&mgr->ref_tracker);
+#endif
+
+	kfree(mgr->groups);
+	kfree(mgr);
+}
+
+/**
+ * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
+ * @i915: i915 driver object
+ *
+ * Creates a DP tunnel manager.
+ *
+ * Returns a pointer to the tunnel manager if created successfully or NULL in
+ * case of an error.
+ */
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	struct drm_dp_tunnel_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
+	int i;
+
+	if (!mgr)
+		return NULL;
+
+	mgr->dev = dev;
+	init_waitqueue_head(&mgr->bw_req_queue);
+
+	mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups), GFP_KERNEL);
+	if (!mgr->groups) {
+		kfree(mgr);
+
+		return NULL;
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
+#endif
+
+	for (i = 0; i < max_group_count; i++) {
+		if (!init_group(mgr, &mgr->groups[i])) {
+			destroy_mgr(mgr);
+
+			return NULL;
+		}
+
+		mgr->group_count++;
+	}
+
+	return mgr;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
+
+/**
+ * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
+ * @mgr: Tunnel manager object
+ *
+ * Destroy the tunnel manager.
+ */
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
+{
+	destroy_mgr(mgr);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);
include/drm/display/drm_dp.h +60 -0
index 281afff6ee4e5..8bfd5d007be8d 100644
@@ -1382,6 +1382,66 @@
 #define DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET	0x69494
 #define DP_HDCP_2_2_REG_DBG_OFFSET		0x69518
 
+/* DP-tunneling */
+#define DP_TUNNELING_OUI				0xe0000
+#define  DP_TUNNELING_OUI_BYTES				3
+
+#define DP_TUNNELING_DEV_ID				0xe0003
+#define  DP_TUNNELING_DEV_ID_BYTES			6
+
+#define DP_TUNNELING_HW_REV				0xe0009
+#define  DP_TUNNELING_HW_REV_MAJOR_SHIFT		4
+#define  DP_TUNNELING_HW_REV_MAJOR_MASK			(0xf << DP_TUNNELING_HW_REV_MAJOR_SHIFT)
+#define  DP_TUNNELING_HW_REV_MINOR_SHIFT		0
+#define  DP_TUNNELING_HW_REV_MINOR_MASK			(0xf << DP_TUNNELING_HW_REV_MINOR_SHIFT)
+
+#define DP_TUNNELING_SW_REV_MAJOR			0xe000a
+#define DP_TUNNELING_SW_REV_MINOR			0xe000b
+
+#define DP_TUNNELING_CAPABILITIES			0xe000d
+#define  DP_IN_BW_ALLOCATION_MODE_SUPPORT		(1 << 7)
+#define  DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT		(1 << 6)
+#define  DP_TUNNELING_SUPPORT				(1 << 0)
+
+#define DP_IN_ADAPTER_INFO				0xe000e
+#define  DP_IN_ADAPTER_NUMBER_BITS			7
+#define  DP_IN_ADAPTER_NUMBER_MASK			((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1)
+
+#define DP_USB4_DRIVER_ID				0xe000f
+#define  DP_USB4_DRIVER_ID_BITS				4
+#define  DP_USB4_DRIVER_ID_MASK				((1 << DP_USB4_DRIVER_ID_BITS) - 1)
+
+#define DP_USB4_DRIVER_BW_CAPABILITY			0xe0020
+#define  DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT	(1 << 7)
+
+#define DP_IN_ADAPTER_TUNNEL_INFORMATION		0xe0021
+#define  DP_GROUP_ID_BITS				3
+#define  DP_GROUP_ID_MASK				((1 << DP_GROUP_ID_BITS) - 1)
+
+#define DP_BW_GRANULARITY				0xe0022
+#define  DP_BW_GRANULARITY_MASK				0x3
+
+#define DP_ESTIMATED_BW					0xe0023
+#define DP_ALLOCATED_BW					0xe0024
+
+#define DP_TUNNELING_STATUS				0xe0025
+#define  DP_BW_ALLOCATION_CAPABILITY_CHANGED		(1 << 3)
+#define  DP_ESTIMATED_BW_CHANGED			(1 << 2)
+#define  DP_BW_REQUEST_SUCCEEDED			(1 << 1)
+#define  DP_BW_REQUEST_FAILED				(1 << 0)
+
+#define DP_TUNNELING_MAX_LINK_RATE			0xe0028
+
+#define DP_TUNNELING_MAX_LANE_COUNT			0xe0029
+#define  DP_TUNNELING_MAX_LANE_COUNT_MASK		0x1f
+
+#define DP_DPTX_BW_ALLOCATION_MODE_CONTROL		0xe0030
+#define  DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE	(1 << 7)
+#define  DP_UNMASK_BW_ALLOCATION_IRQ			(1 << 6)
+
+#define DP_REQUEST_BW					0xe0031
+#define  MAX_DP_REQUEST_BW				255
+
 /* LTTPR: Link Training (LT)-tunable PHY Repeaters */
 #define DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV 0xf0000 /* 1.3 */
 #define DP_MAX_LINK_RATE_PHY_REPEATER			    0xf0001 /* 1.4a */
include/drm/display/drm_dp_tunnel.h new file +270 -0
new file mode 100644
index 0000000000000..f6449b1b4e6e9
@@ -0,0 +1,270 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __DRM_DP_TUNNEL_H__
+#define __DRM_DP_TUNNEL_H__
+
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+
+struct drm_dp_aux;
+
+struct drm_device;
+
+struct drm_atomic_state;
+struct drm_dp_tunnel_mgr;
+struct drm_dp_tunnel_state;
+
+struct ref_tracker;
+
+struct drm_dp_tunnel_ref {
+	struct drm_dp_tunnel *tunnel;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker *tracker;
+#endif
+};
+
+#ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel);
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel);
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+
+void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+#else
+#define drm_dp_tunnel_get(tunnel, tracker) \
+	drm_dp_tunnel_get_untracked(tunnel)
+
+#define drm_dp_tunnel_put(tunnel, tracker) \
+	drm_dp_tunnel_put_untracked(tunnel)
+
+#endif
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	tunnel_ref->tunnel = drm_dp_tunnel_get(tunnel, &tunnel_ref->tracker);
+}
+
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	drm_dp_tunnel_put(tunnel_ref->tunnel, &tunnel_ref->tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux);
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel);
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw);
+int drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			     struct drm_dp_aux *aux);
+
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel);
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel);
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state);
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				       struct drm_dp_tunnel *tunnel,
+				       u8 stream_id, int bw);
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask);
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state);
+
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count);
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr);
+
+#else
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker) {}
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref) {}
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline int
+drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return false;
+}
+
+static inline int
+drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel) {}
+static inline int
+drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			 struct drm_dp_aux *aux)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return -1;
+}
+
+static inline const char *
+drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline void
+drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state) {}
+
+static inline int
+drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				   struct drm_dp_tunnel *tunnel,
+				   u8 stream_id, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						const struct drm_dp_tunnel *tunnel,
+						u32 *stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+				      u32 *failed_stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	return 0;
+}
+
+static inline struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr) {}
+
+
+#endif /* CONFIG_DRM_DISPLAY_DP_TUNNEL */
+
+#endif /* __DRM_DP_TUNNEL_H__ */
Signature · 1 line
2.39.2
Signature · 2 lines
Ville Syrjälä
Intel
author
#15 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 27 lines · Imre Deak
Quoted 17 lines · Ville Syrjälä
Quoted 17 lines

[...]

drivers/gpu/drm/display/drm_dp_tunnel.c new file +16 -0
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
+				    u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
+	struct drm_dp_tunnel_state *new_tunnel_state;
+	u32 group_stream_mask = 0;
+	int group_bw = 0;
+
+	for_each_tunnel_state(new_group_state, new_tunnel_state) {
+		struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;
+		int max_dprx_bw = get_max_dprx_bw(tunnel);
+		int tunnel_bw = drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);
+
+		tun_dbg(tunnel,
+			"%sRequired %d/%d Mb/s total for tunnel.\n",
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 1599

Quoted 5 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+			tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
+			DPTUN_BW_ARG(tunnel_bw),
+			DPTUN_BW_ARG(max_dprx_bw));
+
+		if (tunnel_bw > max_dprx_bw) {

I'm a bit confused why we're checking this here. Aren't we already checking this somewhere else?

Quoted 2 lines

Ah, yes this should be checked already by the encoder compute config + the MST link BW check. It can be removed, thanks.

Though neither of that is guaranteed for drivers in general, so shouldn't it be here still?

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +23 -0
+			*failed_stream_mask = new_tunnel_state->stream_mask;
+			return -ENOSPC;
+		}
+
+		group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
+				max_dprx_bw);
+		group_stream_mask |= new_tunnel_state->stream_mask;
+	}
+
+	tun_grp_dbg(group,
+		    "%sRequired %d/%d Mb/s total for tunnel group.\n",
+		    group_bw > group->available_bw ? "Not enough BW: " : "",
+		    DPTUN_BW_ARG(group_bw),
+		    DPTUN_BW_ARG(group->available_bw));
+
+	if (group_bw > group->available_bw) {
+		*failed_stream_mask = group_stream_mask;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
#16 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

Quoted 28 lines · Imre Deak
Quoted 27 lines · Imre Deak
Quoted 25 lines · Ville Syrjälä
Quoted 12 lines

[...]

drivers/gpu/drm/display/drm_dp_tunnel.c new file +11 -0
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
+				    u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
+	struct drm_dp_tunnel_state *new_tunnel_state;
+	u32 group_stream_mask = 0;
+	int group_bw = 0;
+
+	for_each_tunnel_state(new_group_state, new_tunnel_state) {
+		struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;

drivers/gpu/drm/display/drm_dp_tunnel.c line 1599

Quoted 10 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +10 -0
+		int max_dprx_bw = get_max_dprx_bw(tunnel);
+		int tunnel_bw = drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);
+
+		tun_dbg(tunnel,
+			"%sRequired %d/%d Mb/s total for tunnel.\n",
+			tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
+			DPTUN_BW_ARG(tunnel_bw),
+			DPTUN_BW_ARG(max_dprx_bw));
+
+		if (tunnel_bw > max_dprx_bw) {

I'm a bit confused why we're checking this here. Aren't we already checking this somewhere else?

Quoted 5 lines
Quoted 2 lines

Ah, yes this should be checked already by the encoder compute config + the MST link BW check. It can be removed, thanks.

Though neither of that is guaranteed for drivers in general, so shouldn't it be here still?

I suppose there isn't any real harm in doing it here too.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +23 -0
+			*failed_stream_mask = new_tunnel_state->stream_mask;
+			return -ENOSPC;
+		}
+
+		group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
+				max_dprx_bw);
+		group_stream_mask |= new_tunnel_state->stream_mask;
+	}
+
+	tun_grp_dbg(group,
+		    "%sRequired %d/%d Mb/s total for tunnel group.\n",
+		    group_bw > group->available_bw ? "Not enough BW: " : "",
+		    DPTUN_BW_ARG(group_bw),
+		    DPTUN_BW_ARG(group->available_bw));
+
+	if (group_bw > group->available_bw) {
+		*failed_stream_mask = group_stream_mask;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
Signature · 2 lines
Ville Syrjälä
Intel
author
#17 lore raw

Re: [PATCH 02/19] drm/dp: Add support for DP tunneling

In reply to Imre Deak
Quoted 34 lines · Imre Deak
Quoted 15 lines · Ville Syrjälä
Quoted 14 lines · Imre Deak

+ [...]

drivers/gpu/drm/display/drm_dp_tunnel.c new file +13 -0
+static int group_allocated_bw(struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel *tunnel;
+	int group_allocated_bw = 0;
+
+	for_each_tunnel_in_group(group, tunnel) {
+		if (check_tunnel(tunnel) == 0 &&
+		    tunnel->bw_alloc_enabled)
+			group_allocated_bw += tunnel->allocated_bw;
+	}
+
+	return group_allocated_bw;
+}
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 808

Quoted 6 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +6 -0
+
+static int calc_group_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return group_allocated_bw(tunnel->group) -
+	       tunnel->allocated_bw +
+	       tunnel->estimated_bw;

Hmm. So the estimated_bw=actually_free_bw + tunnel->allocated_bw?

Yes.

Quoted 1 line

Ie. how much bw might be available for this tunnel right now?

Correct.

Quoted 2 lines

And here we're trying to deduce the total bandwidth available by adding in the allocated_bw of all the other tunnels in the group?

Quoted 7 lines

Yes.

Quoted 1 line

Rather weird that we can't just get that number directly...

It is. Imo this could be simply communicated via a DPCD register dedicated for this. Perhaps adding this should be requested from TBT architects.

One reason for this design can be that a host/driver may not see all the tunnels in the group. In that case the tunnel's current usable BW will be only its estimated_bw (that is it can't use the BW already allocated by other tunnels in the group, until those are released by the other host/driver).

Quoted 1,269 lines

I assume this could also use a code comment.

Quoted 786 lines
Quoted 786 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +786 -0
+}
+
+static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
+				     const struct drm_dp_tunnel_regs *regs)
+{
+	struct drm_dp_tunnel *tunnel_iter;
+	int group_available_bw;
+	bool changed;
+
+	tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) * tunnel->bw_granularity;
+
+	if (calc_group_available_bw(tunnel) == tunnel->group->available_bw)
+		return 0;
+
+	for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
+		int err;
+
+		if (tunnel_iter == tunnel)
+			continue;
+
+		if (check_tunnel(tunnel_iter) != 0 ||
+		    !tunnel_iter->bw_alloc_enabled)
+			continue;
+
+		err = drm_dp_dpcd_probe(tunnel_iter->aux, DP_DPCD_REV);
+		if (err) {
+			tun_dbg(tunnel_iter,
+				"Probe failed, assume disconnected (err %pe)\n",
+				ERR_PTR(err));
+			drm_dp_tunnel_set_io_error(tunnel_iter);
+		}
+	}
+
+	group_available_bw = calc_group_available_bw(tunnel);
+
+	tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
+		DPTUN_BW_ARG(tunnel->group->available_bw),
+		DPTUN_BW_ARG(group_available_bw));
+
+	changed = tunnel->group->available_bw != group_available_bw;
+
+	tunnel->group->available_bw = group_available_bw;
+
+	return changed ? 1 : 0;
+}
+
+static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool enable)
+{
+	u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)
+		goto out_err;
+
+	if (enable)
+		val |= mask;
+	else
+		val &= ~mask;
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)
+		goto out_err;
+
+	tunnel->bw_alloc_enabled = enable;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_enable_bw_alloc: Enable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	if (!tunnel->bw_alloc_supported)
+		return -EOPNOTSUPP;
+
+	if (!tunnel_group_id(tunnel->group->drv_group_id))
+		return -EINVAL;
+
+	err = set_bw_alloc_mode(tunnel, true);
+	if (err)
+		goto out;
+
+	err = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (err) {
+		set_bw_alloc_mode(tunnel, false);
+
+		goto out;
+	}
+
+	if (!tunnel->max_dprx_rate)
+		update_dprx_caps(tunnel, &regs);
+
+	if (tunnel->group->available_bw == -1) {
+		err = update_group_available_bw(tunnel, &regs);
+		if (err > 0)
+			err = 0;
+	}
+out:
+	tun_dbg_stat(tunnel, err,
+		     "Enabling BW alloc mode: DPRX:%dx%d Group alloc:%d/%d Mb/s",
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
+
+/**
+ * drm_dp_tunnel_disable_bw_alloc: Disable DP tunnel BW allocation mode
+ * @tunnel: Tunnel object
+ *
+ * Disable the DP tunnel BW allocation mode on @tunnel.
+ *
+ * Returns 0 in case of success, negative error code otherwise.
+ */
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	err = set_bw_alloc_mode(tunnel, false);
+
+	tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
+
+	return err;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
+
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->bw_alloc_enabled;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
+
+static int bw_req_complete(struct drm_dp_aux *aux, bool *status_changed)
+{
+	u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
+	u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	*status_changed = val & status_change_mask;
+
+	val &= bw_req_mask;
+
+	if (!val)
+		return -EAGAIN;
+
+	if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, val) < 0)
+		return -EIO;
+
+	return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
+}
+
+static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
+	int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
+	unsigned long wait_expires;
+	DEFINE_WAIT(wait);
+	int err;
+
+	/* Atomic check should prevent the following. */
+	if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
+		err = -EINVAL;
+		goto out;
+	}
+
+	if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW, request_bw) < 0) {
+		err = -EIO;
+		goto out;
+	}
+
+	wait_expires = jiffies + msecs_to_jiffies(3000);
+
+	for (;;) {
+		bool status_changed;
+
+		err = bw_req_complete(tunnel->aux, &status_changed);
+		if (err != -EAGAIN)
+			break;
+
+		if (status_changed) {
+			struct drm_dp_tunnel_regs regs;
+
+			err = read_and_verify_tunnel_regs(tunnel, &regs,
+							  ALLOW_ALLOCATED_BW_CHANGE);
+			if (err)
+				break;
+		}
+
+		if (time_after(jiffies, wait_expires)) {
+			err = -ETIMEDOUT;
+			break;
+		}
+
+		prepare_to_wait(&mgr->bw_req_queue, &wait, TASK_UNINTERRUPTIBLE);
+		schedule_timeout(msecs_to_jiffies(200));
+	};
+
+	finish_wait(&mgr->bw_req_queue, &wait);
+
+	if (err)
+		goto out;
+
+	tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
+
+out:
+	tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel: Group alloc:%d/%d Mb/s",
+		     DPTUN_BW_ARG(request_bw * tunnel->bw_granularity),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (err == -EIO)
+		drm_dp_tunnel_set_io_error(tunnel);
+
+	return err;
+}
+
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	int err = check_tunnel(tunnel);
+
+	if (err)
+		return err;
+
+	return allocate_tunnel_bw(tunnel, bw);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
+
+static int check_and_clear_status_change(struct drm_dp_tunnel *tunnel)
+{
+	u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
+	u8 val;
+
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0)
+		goto out_err;
+
+	val &= mask;
+
+	if (val) {
+		if (drm_dp_dpcd_writeb(tunnel->aux, DP_TUNNELING_STATUS, val) < 0)
+			goto out_err;
+
+		return 1;
+	}
+
+	if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
+		return 0;
+
+	/*
+	 * Check for estimated BW changes explicitly to account for lost
+	 * BW change notifications.
+	 */
+	if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) < 0)
+		goto out_err;
+
+	if (val * tunnel->bw_granularity != tunnel->estimated_bw)
+		return 1;
+
+	return 0;
+
+out_err:
+	drm_dp_tunnel_set_io_error(tunnel);
+
+	return -EIO;
+}
+
+/**
+ * drm_dp_tunnel_update_state: Update DP tunnel SW state with the HW state
+ * @tunnel: Tunnel object
+ *
+ * Update the SW state of @tunnel with the HW state.
+ *
+ * Returns 0 if the state has not changed, 1 if it has changed and got updated
+ * successfully and a negative error code otherwise.
+ */
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_regs regs;
+	bool changed = false;
+	int ret = check_tunnel(tunnel);
+
+	if (ret < 0)
+		return ret;
+
+	ret = check_and_clear_status_change(tunnel);
+	if (ret < 0)
+		goto out;
+
+	if (!ret)
+		return 0;
+
+	ret = read_and_verify_tunnel_regs(tunnel, &regs, 0);
+	if (ret)
+		goto out;
+
+	if (update_dprx_caps(tunnel, &regs))
+		changed = true;
+
+	ret = update_group_available_bw(tunnel, &regs);
+	if (ret == 1)
+		changed = true;
+
+out:
+	tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
+		     "State update: Changed:%c DPRX:%dx%d Tunnel alloc:%d/%d Group alloc:%d/%d Mb/s",
+		     yes_no_chr(changed),
+		     tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
+		     DPTUN_BW_ARG(tunnel->allocated_bw),
+		     DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
+		     DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
+		     DPTUN_BW_ARG(tunnel->group->available_bw));
+
+	if (ret < 0)
+		return ret;
+
+	if (changed)
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_update_state);
+
+/*
+ * Returns 0 if no re-probe is needed, 1 if a re-probe is needed,
+ * a negative error code otherwise.
+ */
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_aux *aux)
+{
+	u8 val;
+
+	if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
+		return -EIO;
+
+	if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
+		wake_up_all(&mgr->bw_req_queue);
+
+	if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED))
+		return 1;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
+
+/**
+ * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum link rate of the DPRX connected
+ * to @tunnel. Note that this rate will not be limited by the BW limit of the
+ * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
+ * registers.
+ *
+ * Returns the maximum link rate in 10 kbit/s units.
+ */
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_rate;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
+
+/**
+ * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count of the tunnel's DPRX
+ * @tunnel: Tunnel object
+ *
+ * The function is used to query the maximum lane count of the DPRX connected
+ * to @tunnel. Note that this lane count will not be limited by the BW limit of
+ * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
+ * registers.
+ *
+ * Returns the maximum lane count.
+ */
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->max_dprx_lane_count;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
+
+/**
+ * drm_dp_tunnel_available_bw - Query the estimated total available BW of the tunnel
+ * @tunnel: Tunnel object
+ *
+ * This function is used to query the estimated total available BW of the
+ * tunnel. This includes the currently allocated and free BW for all the
+ * tunnels in @tunnel's group. The available BW is valid only after the BW
+ * allocation mode has been enabled for the tunnel and its state got updated
+ * calling drm_dp_tunnel_update_state().
+ *
+ * Returns the @tunnel group's estimated total available bandwidth in kB/s
+ * units, or -1 if the available BW isn't valid (the BW allocation mode is
+ * not enabled or the tunnel's state hasn't been updated).
+ */
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return tunnel->group->available_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
+
+static struct drm_dp_tunnel_group_state *
+drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
+				     const struct drm_dp_tunnel *tunnel)
+{
+	return (struct drm_dp_tunnel_group_state *)
+		drm_atomic_get_private_obj_state(state,
+						 &tunnel->group->base);
+}
+
+static struct drm_dp_tunnel_state *
+add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tun_dbg_atomic(tunnel,
+		       "Adding state for tunnel %p to group state %p\n",
+		       tunnel, group_state);
+
+	tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
+	if (!tunnel_state)
+		return NULL;
+
+	tunnel_state->group_state = group_state;
+
+	drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
+
+	INIT_LIST_HEAD(&tunnel_state->node);
+	list_add(&tunnel_state->node, &group_state->tunnel_states);
+
+	return tunnel_state;
+}
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state)
+{
+	tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
+		       "Clearing state for tunnel %p\n",
+		       tunnel_state->tunnel_ref.tunnel);
+
+	list_del(&tunnel_state->node);
+
+	kfree(tunnel_state->stream_bw);
+	drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
+
+	kfree(tunnel_state);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_clear_state);
+
+static void clear_tunnel_group_state(struct drm_dp_tunnel_group_state *group_state)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+	struct drm_dp_tunnel_state *tunnel_state_tmp;
+
+	for_each_tunnel_state_safe(group_state, tunnel_state, tunnel_state_tmp)
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+}
+
+static struct drm_dp_tunnel_state *
+get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+		 const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	for_each_tunnel_state(group_state, tunnel_state)
+		if (tunnel_state->tunnel_ref.tunnel == tunnel)
+			return tunnel_state;
+
+	return NULL;
+}
+
+static struct drm_dp_tunnel_state *
+get_or_add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
+			struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	tunnel_state = get_tunnel_state(group_state, tunnel);
+	if (tunnel_state)
+		return tunnel_state;
+
+	return add_tunnel_state(group_state, tunnel);
+}
+
+static struct drm_private_state *
+tunnel_group_duplicate_state(struct drm_private_obj *obj)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(obj->state);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+	if (!group_state)
+		return NULL;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	__drm_atomic_helper_private_obj_duplicate_state(obj, &group_state->base);
+
+	for_each_tunnel_state(to_group_state(obj->state), tunnel_state) {
+		struct drm_dp_tunnel_state *new_tunnel_state;
+
+		new_tunnel_state = get_or_add_tunnel_state(group_state,
+							   tunnel_state->tunnel_ref.tunnel);
+		if (!new_tunnel_state)
+			goto out_free_state;
+
+		new_tunnel_state->stream_mask = tunnel_state->stream_mask;
+		new_tunnel_state->stream_bw = kmemdup(tunnel_state->stream_bw,
+						      sizeof(*tunnel_state->stream_bw) *
+							hweight32(tunnel_state->stream_mask),
+						      GFP_KERNEL);
+
+		if (!new_tunnel_state->stream_bw)
+			goto out_free_state;
+	}
+
+	return &group_state->base;
+
+out_free_state:
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+
+	return NULL;
+}
+
+static void tunnel_group_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state)
+{
+	struct drm_dp_tunnel_group_state *group_state = to_group_state(state);
+
+	clear_tunnel_group_state(group_state);
+	kfree(group_state);
+}
+
+static const struct drm_private_state_funcs tunnel_group_funcs = {
+	.atomic_duplicate_state = tunnel_group_duplicate_state,
+	.atomic_destroy_state = tunnel_group_destroy_state,
+};
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return ERR_CAST(group_state);
+
+	tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
+	if (!tunnel_state)
+		return ERR_PTR(-ENOMEM);
+
+	return tunnel_state;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i)
+		if (to_group(new_group_state->base.obj) == tunnel->group)
+			return get_tunnel_state(new_group_state, tunnel);
+
+	return NULL;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
+
+static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group)
+{
+	struct drm_dp_tunnel_group_state *group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
+
+	if (!group_state)
+		return false;
+
+	INIT_LIST_HEAD(&group_state->tunnel_states);
+
+	group->mgr = mgr;
+	group->available_bw = -1;
+	INIT_LIST_HEAD(&group->tunnels);
+
+	drm_atomic_private_obj_init(mgr->dev, &group->base, &group_state->base,
+				    &tunnel_group_funcs);
+
+	return true;
+}
+
+static void cleanup_group(struct drm_dp_tunnel_group *group)
+{
+	drm_atomic_private_obj_fini(&group->base);
+}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+	const struct drm_dp_tunnel_state *tunnel_state;
+	u32 stream_mask = 0;
+
+	for_each_tunnel_state(group_state, tunnel_state) {
+		drm_WARN(to_group(group_state->base.obj)->mgr->dev,
+			 tunnel_state->stream_mask & stream_mask,
+			 "[DPTUN %s]: conflicting stream IDs %x (IDs in other tunnels %x)\n",
+			 tunnel_state->tunnel_ref.tunnel->name,
+			 tunnel_state->stream_mask,
+			 stream_mask);
+
+		stream_mask |= tunnel_state->stream_mask;
+	}
+}
+#else
+static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
+{
+}
+#endif
+
+static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
+{
+	return hweight32(stream_mask & (BIT(stream_id) - 1));
+}
+
+static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
+			   unsigned long old_mask, unsigned long new_mask)
+{
+	unsigned long move_mask = old_mask & new_mask;
+	int *new_bws = NULL;
+	int id;
+
+	WARN_ON(!new_mask);
+
+	if (old_mask == new_mask)
+		return 0;
+
+	new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws), GFP_KERNEL);
+	if (!new_bws)
+		return -ENOMEM;
+
+	for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask))
+		new_bws[stream_id_to_idx(new_mask, id)] =
+			tunnel_state->stream_bw[stream_id_to_idx(old_mask, id)];
+
+	kfree(tunnel_state->stream_bw);
+	tunnel_state->stream_bw = new_bws;
+	tunnel_state->stream_mask = new_mask;
+
+	return 0;
+}
+
+static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			 u8 stream_id, int bw)
+{
+	int err;
+
+	err = resize_bw_array(tunnel_state,
+			      tunnel_state->stream_mask,
+			      tunnel_state->stream_mask | BIT(stream_id));
+	if (err)
+		return err;
+
+	tunnel_state->stream_bw[stream_id_to_idx(tunnel_state->stream_mask, stream_id)] = bw;
+
+	return 0;
+}
+
+static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
+			   u8 stream_id)
+{
+	if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
+		drm_dp_tunnel_atomic_clear_state(tunnel_state);
+		return 0;
+	}
+
+	return resize_bw_array(tunnel_state,
+			       tunnel_state->stream_mask,
+			       tunnel_state->stream_mask & ~BIT(stream_id));
+}
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+					 struct drm_dp_tunnel *tunnel,
+					 u8 stream_id, int bw)
+{
+	struct drm_dp_tunnel_group_state *new_group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+	int err;
+
+	if (drm_WARN_ON(tunnel->group->mgr->dev,
+			stream_id > BITS_PER_TYPE(tunnel_state->stream_mask)))
+		return -EINVAL;
+
+	tun_dbg(tunnel,
+		"Setting %d Mb/s for stream %d\n",
+		DPTUN_BW_ARG(bw), stream_id);
+
+	if (bw == 0) {
+		tunnel_state = get_tunnel_state(new_group_state, tunnel);
+		if (!tunnel_state)
+			return 0;
+
+		return clear_stream_bw(tunnel_state, stream_id);
+	}
+
+	tunnel_state = get_or_add_tunnel_state(new_group_state, tunnel);
+	if (drm_WARN_ON(state->dev, !tunnel_state))
+		return -EINVAL;
+
+	err = set_stream_bw(tunnel_state, stream_id, bw);
+	if (err)
+		return err;
+
+	check_unique_stream_ids(new_group_state);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	int tunnel_bw = 0;
+	int i;
+
+	for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
+		tunnel_bw += tunnel_state->stream_bw[i];
+
+	return tunnel_bw;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_tunnel_bw);
+
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask)
+{
+	struct drm_dp_tunnel_group_state *group_state =
+		drm_dp_tunnel_atomic_get_group_state(state, tunnel);
+	struct drm_dp_tunnel_state *tunnel_state;
+
+	if (IS_ERR(group_state))
+		return PTR_ERR(group_state);
+
+	*stream_mask = 0;
+	for_each_tunnel_state(group_state, tunnel_state)
+		*stream_mask |= tunnel_state->stream_mask;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
+
+static int
+drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
+				    u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
+	struct drm_dp_tunnel_state *new_tunnel_state;
+	u32 group_stream_mask = 0;
+	int group_bw = 0;
+
+	for_each_tunnel_state(new_group_state, new_tunnel_state) {
+		struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;
+		int max_dprx_bw = get_max_dprx_bw(tunnel);
+		int tunnel_bw = drm_dp_tunnel_atomic_get_tunnel_bw(new_tunnel_state);
+
+		tun_dbg(tunnel,
+			"%sRequired %d/%d Mb/s total for tunnel.\n",
Quoted 8 lines

drivers/gpu/drm/display/drm_dp_tunnel.c line 1599

Quoted 5 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +5 -0
+			tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
+			DPTUN_BW_ARG(tunnel_bw),
+			DPTUN_BW_ARG(max_dprx_bw));
+
+		if (tunnel_bw > max_dprx_bw) {

I'm a bit confused why we're checking this here. Aren't we already checking this somewhere else?

Ah, yes this should be checked already by the encoder compute config + the MST link BW check. It can be removed, thanks.

Quoted 469 lines
Quoted 465 lines
drivers/gpu/drm/display/drm_dp_tunnel.c new file +116 -0
+			*failed_stream_mask = new_tunnel_state->stream_mask;
+			return -ENOSPC;
+		}
+
+		group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
+				max_dprx_bw);
+		group_stream_mask |= new_tunnel_state->stream_mask;
+	}
+
+	tun_grp_dbg(group,
+		    "%sRequired %d/%d Mb/s total for tunnel group.\n",
+		    group_bw > group->available_bw ? "Not enough BW: " : "",
+		    DPTUN_BW_ARG(group_bw),
+		    DPTUN_BW_ARG(group->available_bw));
+
+	if (group_bw > group->available_bw) {
+		*failed_stream_mask = group_stream_mask;
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask)
+{
+	struct drm_dp_tunnel_group_state *new_group_state;
+	int i;
+
+	for_each_new_group_in_state(state, new_group_state, i) {
+		int ret;
+
+		ret = drm_dp_tunnel_atomic_check_group_bw(new_group_state,
+							  failed_stream_mask);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
+
+static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
+{
+	int i;
+
+	for (i = 0; i < mgr->group_count; i++) {
+		cleanup_group(&mgr->groups[i]);
+		drm_WARN_ON(mgr->dev, !list_empty(&mgr->groups[i].tunnels));
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_exit(&mgr->ref_tracker);
+#endif
+
+	kfree(mgr->groups);
+	kfree(mgr);
+}
+
+/**
+ * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
+ * @i915: i915 driver object
+ *
+ * Creates a DP tunnel manager.
+ *
+ * Returns a pointer to the tunnel manager if created successfully or NULL in
+ * case of an error.
+ */
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	struct drm_dp_tunnel_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
+	int i;
+
+	if (!mgr)
+		return NULL;
+
+	mgr->dev = dev;
+	init_waitqueue_head(&mgr->bw_req_queue);
+
+	mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups), GFP_KERNEL);
+	if (!mgr->groups) {
+		kfree(mgr);
+
+		return NULL;
+	}
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
+#endif
+
+	for (i = 0; i < max_group_count; i++) {
+		if (!init_group(mgr, &mgr->groups[i])) {
+			destroy_mgr(mgr);
+
+			return NULL;
+		}
+
+		mgr->group_count++;
+	}
+
+	return mgr;
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
+
+/**
+ * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
+ * @mgr: Tunnel manager object
+ *
+ * Destroy the tunnel manager.
+ */
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
+{
+	destroy_mgr(mgr);
+}
+EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);
include/drm/display/drm_dp.h +60 -0
index 281afff6ee4e5..8bfd5d007be8d 100644
@@ -1382,6 +1382,66 @@
 #define DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET	0x69494
 #define DP_HDCP_2_2_REG_DBG_OFFSET		0x69518
 
+/* DP-tunneling */
+#define DP_TUNNELING_OUI				0xe0000
+#define  DP_TUNNELING_OUI_BYTES				3
+
+#define DP_TUNNELING_DEV_ID				0xe0003
+#define  DP_TUNNELING_DEV_ID_BYTES			6
+
+#define DP_TUNNELING_HW_REV				0xe0009
+#define  DP_TUNNELING_HW_REV_MAJOR_SHIFT		4
+#define  DP_TUNNELING_HW_REV_MAJOR_MASK			(0xf << DP_TUNNELING_HW_REV_MAJOR_SHIFT)
+#define  DP_TUNNELING_HW_REV_MINOR_SHIFT		0
+#define  DP_TUNNELING_HW_REV_MINOR_MASK			(0xf << DP_TUNNELING_HW_REV_MINOR_SHIFT)
+
+#define DP_TUNNELING_SW_REV_MAJOR			0xe000a
+#define DP_TUNNELING_SW_REV_MINOR			0xe000b
+
+#define DP_TUNNELING_CAPABILITIES			0xe000d
+#define  DP_IN_BW_ALLOCATION_MODE_SUPPORT		(1 << 7)
+#define  DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT		(1 << 6)
+#define  DP_TUNNELING_SUPPORT				(1 << 0)
+
+#define DP_IN_ADAPTER_INFO				0xe000e
+#define  DP_IN_ADAPTER_NUMBER_BITS			7
+#define  DP_IN_ADAPTER_NUMBER_MASK			((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1)
+
+#define DP_USB4_DRIVER_ID				0xe000f
+#define  DP_USB4_DRIVER_ID_BITS				4
+#define  DP_USB4_DRIVER_ID_MASK				((1 << DP_USB4_DRIVER_ID_BITS) - 1)
+
+#define DP_USB4_DRIVER_BW_CAPABILITY			0xe0020
+#define  DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT	(1 << 7)
+
+#define DP_IN_ADAPTER_TUNNEL_INFORMATION		0xe0021
+#define  DP_GROUP_ID_BITS				3
+#define  DP_GROUP_ID_MASK				((1 << DP_GROUP_ID_BITS) - 1)
+
+#define DP_BW_GRANULARITY				0xe0022
+#define  DP_BW_GRANULARITY_MASK				0x3
+
+#define DP_ESTIMATED_BW					0xe0023
+#define DP_ALLOCATED_BW					0xe0024
+
+#define DP_TUNNELING_STATUS				0xe0025
+#define  DP_BW_ALLOCATION_CAPABILITY_CHANGED		(1 << 3)
+#define  DP_ESTIMATED_BW_CHANGED			(1 << 2)
+#define  DP_BW_REQUEST_SUCCEEDED			(1 << 1)
+#define  DP_BW_REQUEST_FAILED				(1 << 0)
+
+#define DP_TUNNELING_MAX_LINK_RATE			0xe0028
+
+#define DP_TUNNELING_MAX_LANE_COUNT			0xe0029
+#define  DP_TUNNELING_MAX_LANE_COUNT_MASK		0x1f
+
+#define DP_DPTX_BW_ALLOCATION_MODE_CONTROL		0xe0030
+#define  DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE	(1 << 7)
+#define  DP_UNMASK_BW_ALLOCATION_IRQ			(1 << 6)
+
+#define DP_REQUEST_BW					0xe0031
+#define  MAX_DP_REQUEST_BW				255
+
 /* LTTPR: Link Training (LT)-tunable PHY Repeaters */
 #define DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV 0xf0000 /* 1.3 */
 #define DP_MAX_LINK_RATE_PHY_REPEATER			    0xf0001 /* 1.4a */
include/drm/display/drm_dp_tunnel.h new file +270 -0
new file mode 100644
index 0000000000000..f6449b1b4e6e9
@@ -0,0 +1,270 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __DRM_DP_TUNNEL_H__
+#define __DRM_DP_TUNNEL_H__
+
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/types.h>
+
+struct drm_dp_aux;
+
+struct drm_device;
+
+struct drm_atomic_state;
+struct drm_dp_tunnel_mgr;
+struct drm_dp_tunnel_state;
+
+struct ref_tracker;
+
+struct drm_dp_tunnel_ref {
+	struct drm_dp_tunnel *tunnel;
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+	struct ref_tracker *tracker;
+#endif
+};
+
+#ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel);
+void drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel);
+
+#ifdef CONFIG_DRM_DISPLAY_DEBUG_DP_TUNNEL_STATE
+struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+
+void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker);
+#else
+#define drm_dp_tunnel_get(tunnel, tracker) \
+	drm_dp_tunnel_get_untracked(tunnel)
+
+#define drm_dp_tunnel_put(tunnel, tracker) \
+	drm_dp_tunnel_put_untracked(tunnel)
+
+#endif
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	tunnel_ref->tunnel = drm_dp_tunnel_get(tunnel, &tunnel_ref->tracker);
+}
+
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref)
+{
+	drm_dp_tunnel_put(tunnel_ref->tunnel, &tunnel_ref->tracker);
+}
+
+struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux);
+int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel);
+bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw);
+int drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel);
+
+int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			     struct drm_dp_aux *aux);
+
+int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel);
+int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel);
+
+const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel);
+
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel);
+struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel);
+
+void drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state);
+
+int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				       struct drm_dp_tunnel *tunnel,
+				       u8 stream_id, int bw);
+int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						    const struct drm_dp_tunnel *tunnel,
+						    u32 *stream_mask);
+
+int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+					  u32 *failed_stream_mask);
+
+int drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state);
+
+struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count);
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr);
+
+#else
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get_untracked(struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put_untracked(struct drm_dp_tunnel *tunnel) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker)
+{
+	return NULL;
+}
+
+static inline void
+drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, struct ref_tracker **tracker) {}
+
+static inline void drm_dp_tunnel_ref_get(struct drm_dp_tunnel *tunnel,
+					   struct drm_dp_tunnel_ref *tunnel_ref) {}
+static inline void drm_dp_tunnel_ref_put(struct drm_dp_tunnel_ref *tunnel_ref) {}
+
+static inline struct drm_dp_tunnel *
+drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
+		     struct drm_dp_aux *aux)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline int
+drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
+{
+	return false;
+}
+
+static inline int
+drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_check_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel) {}
+static inline int
+drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr,
+			 struct drm_dp_aux *aux)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
+{
+	return 0;
+}
+
+static inline int
+drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
+{
+	return -1;
+}
+
+static inline const char *
+drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
+{
+	return NULL;
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
+			       struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline struct drm_dp_tunnel_state *
+drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
+				   const struct drm_dp_tunnel *tunnel)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline void
+drm_dp_tunnel_atomic_clear_state(struct drm_dp_tunnel_state *tunnel_state) {}
+
+static inline int
+drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
+				   struct drm_dp_tunnel *tunnel,
+				   u8 stream_id, int bw)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
+						const struct drm_dp_tunnel *tunnel,
+						u32 *stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
+				      u32 *failed_stream_mask)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int
+drm_dp_tunnel_atomic_get_tunnel_bw(const struct drm_dp_tunnel_state *tunnel_state)
+{
+	return 0;
+}
+
+static inline struct drm_dp_tunnel_mgr *
+drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+
+static inline
+void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr) {}
+
+
+#endif /* CONFIG_DRM_DISPLAY_DP_TUNNEL */
+
+#endif /* __DRM_DP_TUNNEL_H__ */
Signature · 1 line
2.39.2
Signature · 2 lines
Ville Syrjälä
Intel
author
#18 lore raw

PATCH 3/19 drm/i915/dp: Add support to notify MST connectors to retry modesets

In reply to Imre Deak

On shared (Thunderbolt) links with DP tunnels, the modeset may need to be retried on all connectors on the link due to a link BW limitation arising only after the atomic check phase. To support this add a helper function queuing a work to retry the modeset on a given port's connector and at the same time any MST connector with streams through the same port. A follow-up change enabling the DP tunnel Bandwidth Allocation Mode will take this into use.

---
 drivers/gpu/drm/i915/display/intel_display.c  |  5 +-
 drivers/gpu/drm/i915/display/intel_dp.c       | 55 ++++++++++++++++++-
 drivers/gpu/drm/i915/display/intel_dp.h       |  8 +++
 .../drm/i915/display/intel_dp_link_training.c |  3 +-
 drivers/gpu/drm/i915/display/intel_dp_mst.c   |  2 +
 5 files changed, 67 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/display/intel_display.c +3 -2
index a92e959c8ac7b..0caebbb3e2dbb 100644
@@ -8060,8 +8060,9 @@ void intel_hpd_poll_fini(struct drm_i915_private *i915)
 	/* Kill all the work that may have been queued by hpd. */
 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
 	for_each_intel_connector_iter(connector, &conn_iter) {
-		if (connector->modeset_retry_work.func)
-			cancel_work_sync(&connector->modeset_retry_work);
+		if (connector->modeset_retry_work.func &&
+		    cancel_work_sync(&connector->modeset_retry_work))
+			drm_connector_put(&connector->base);
 		if (connector->hdcp.shim) {
 			cancel_delayed_work_sync(&connector->hdcp.check_work);
 			cancel_work_sync(&connector->hdcp.prop_work);
drivers/gpu/drm/i915/display/intel_dp.c +53 -2

73 lines not on this page · raw message

drivers/gpu/drm/i915/display/intel_dp.h +8 -0
index 530cc97bc42f4..105c2086310db 100644
@@ -23,6 +23,8 @@ struct intel_digital_port;
 struct intel_dp;
 struct intel_encoder;
 
+struct work_struct;
+
 struct link_config_limits {
 	int min_rate, max_rate;
 	int min_lane_count, max_lane_count;
@@ -43,6 +45,12 @@ void intel_dp_adjust_compliance_config(struct intel_dp *intel_dp,
 bool intel_dp_limited_color_range(const struct intel_crtc_state *crtc_state,
 				  const struct drm_connector_state *conn_state);
 int intel_dp_min_bpp(enum intel_output_format output_format);
+void intel_dp_init_modeset_retry_work(struct intel_connector *connector);
+void intel_dp_queue_modeset_retry_work(struct intel_connector *connector);
+void intel_dp_queue_modeset_retry_for_link(struct intel_atomic_state *state,
+					   struct intel_encoder *encoder,
+					   const struct intel_crtc_state *crtc_state,
+					   const struct drm_connector_state *conn_state);
 bool intel_dp_init_connector(struct intel_digital_port *dig_port,
 			     struct intel_connector *intel_connector);
 void intel_dp_set_link_params(struct intel_dp *intel_dp,
drivers/gpu/drm/i915/display/intel_dp_link_training.c +1 -2
index 1abfafbbfa757..7b140cbf8dd31 100644
@@ -1075,7 +1075,6 @@ static void intel_dp_schedule_fallback_link_training(struct intel_dp *intel_dp,
 						     const struct intel_crtc_state *crtc_state)
 {
 	struct intel_connector *intel_connector = intel_dp->attached_connector;
-	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
 
 	if (!intel_digital_port_connected(&dp_to_dig_port(intel_dp)->base)) {
 		lt_dbg(intel_dp, DP_PHY_DPRX, "Link Training failed on disconnected sink.\n");
@@ -1093,7 +1092,7 @@ static void intel_dp_schedule_fallback_link_training(struct intel_dp *intel_dp,
 	}
 
 	/* Schedule a Hotplug Uevent to userspace to start modeset */
-	queue_work(i915->unordered_wq, &intel_connector->modeset_retry_work);
+	intel_dp_queue_modeset_retry_work(intel_connector);
 }
 
 /* Perform the link training on all LTTPRs and the DPRX on a link. */
drivers/gpu/drm/i915/display/intel_dp_mst.c +2 -0
index 5fa25a5a36b55..b15e43ebf138b 100644
@@ -1542,6 +1542,8 @@ static struct drm_connector *intel_dp_add_mst_connector(struct drm_dp_mst_topolo
 	intel_connector->port = port;
 	drm_dp_mst_get_port_malloc(port);
 
+	intel_dp_init_modeset_retry_work(intel_connector);
+
 	intel_connector->dp.dsc_decompression_aux = drm_dp_mst_dsc_aux_for_port(port);
 	intel_dp_mst_read_decompression_port_dsc_caps(intel_dp, intel_connector);
 	intel_connector->dp.dsc_hblank_expansion_quirk =
Signature · 1 line
2.39.2
#19 lore raw

Re: [PATCH 03/19] drm/i915/dp: Add support to notify MST connectors to retry modesets

Quoted 95 lines · Imre Deak

On shared (Thunderbolt) links with DP tunnels, the modeset may need to
be retried on all connectors on the link due to a link BW limitation arising only after the atomic check phase. To support this add a helper
function queuing a work to retry the modeset on a given port's connector
and at the same time any MST connector with streams through the same port. A follow-up change enabling the DP tunnel Bandwidth Allocation Mode will take this into use.

---
 drivers/gpu/drm/i915/display/intel_display.c  |  5 +- drivers/gpu/drm/i915/display/intel_dp.c       | 55
++++++++++++++++++-
 drivers/gpu/drm/i915/display/intel_dp.h       |  8 +++ .../drm/i915/display/intel_dp_link_training.c |  3 +- drivers/gpu/drm/i915/display/intel_dp_mst.c   |  2 +

 5 files changed, 67 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/display/intel_display.c +3 -2
index a92e959c8ac7b..0caebbb3e2dbb 100644
@@ -8060,8 +8060,9 @@ void intel_hpd_poll_fini(struct
 drm_i915_private *i915)
         /* Kill all the work that may have been queued by hpd. */
         drm_connector_list_iter_begin(&i915->drm, &conn_iter);
         for_each_intel_connector_iter(connector, &conn_iter) {
-               if (connector->modeset_retry_work.func)
-                       cancel_work_sync(&connector-
 >modeset_retry_work);
+               if (connector->modeset_retry_work.func &&
+                   cancel_work_sync(&connector->modeset_retry_work))
+                       drm_connector_put(&connector->base);
                 if (connector->hdcp.shim) {

                        cancel_delayed_work_sync(&connector-

Quoted 1 line

hdcp.check_work);

                        cancel_work_sync(&connector->hdcp.prop_work);

drivers/gpu/drm/i915/display/intel_dp.c +0 -0
index ab415f41924d7..4e36c2c39888e 100644
@@ -2837,6 +2837,50 @@ intel_dp_audio_compute_config(struct
 intel_encoder *encoder,
                                         intel_dp_is_uhbr(pipe_config)
 ;
  }


+void intel_dp_queue_modeset_retry_work(struct intel_connector *connector)

drivers/gpu/drm/i915/display/intel_dp.c +5 -0
+{
+       struct drm_i915_private *i915 = to_i915(connector->base.dev);
+
+       drm_connector_get(&connector->base);
+       if (!queue_work(i915->unordered_wq, &connector-
Quoted 1 line

modeset_retry_work))

+               drm_connector_put(&connector->base);

drivers/gpu/drm/i915/display/intel_dp.c +4 -0
+}
+
+void
+intel_dp_queue_modeset_retry_for_link(struct intel_atomic_state

*state,
+                                     struct intel_encoder *encoder, +                                     const struct intel_crtc_state *crtc_state,
+                                     const struct drm_connector_state *conn_state)
+{
+       struct drm_i915_private *i915 = to_i915(crtc_state-

Quoted 1 line

uapi.crtc->dev);

+       struct intel_connector *connector;
+       struct intel_digital_connector_state *iter_conn_state; +       struct intel_dp *intel_dp;
+       int i;
+
+       if (conn_state) {
+               connector = to_intel_connector(conn_state-

Quoted 1 line

connector);

+               intel_dp_queue_modeset_retry_work(connector);

drivers/gpu/drm/i915/display/intel_dp.c +6 -0
+
+               return;
+       }
+
+       if (drm_WARN_ON(&i915->drm,
+                       !intel_crtc_has_type(crtc_state,

INTEL_OUTPUT_DP_MST)))
+               return;

drivers/gpu/drm/i915/display/intel_dp.c +4 -0
+
+       intel_dp = enc_to_intel_dp(encoder);
+
+       for_each_new_intel_connector_in_state(state, connector,
Quoted 2 lines

iter_conn_state, i) {
+               (void)iter_conn_state;

Checked iter_conn_state->base->crtc documentation:

@crtc: CRTC to connect connector to, NULL if disabled.

Do we need to check if connector is "disabled" or is it impossible scenario?

BR,

Jouni Högander

Quoted 125 lines
drivers/gpu/drm/i915/display/intel_dp.c +8 -0
+
+               if (connector->mst_port != intel_dp)
+                       continue;
+
+               intel_dp_queue_modeset_retry_work(connector);
+       }
+}
+

 int
 intel_dp_compute_config(struct intel_encoder *encoder, struct intel_crtc_state *pipe_config,

drivers/gpu/drm/i915/display/intel_dp.c +8 -0
@@ -6436,6 +6480,14 @@ static void
 intel_dp_modeset_retry_work_fn(struct work_struct *work)
         mutex_unlock(&connector->dev->mode_config.mutex);
         /* Send Hotplug uevent so userspace can reprobe */
         drm_kms_helper_connector_hotplug_event(connector);
+
+       drm_connector_put(connector);
+}
+
+void intel_dp_init_modeset_retry_work(struct intel_connector
 *connector)
+{
+       INIT_WORK(&connector->modeset_retry_work,
+                 intel_dp_modeset_retry_work_fn);
  }


 bool

drivers/gpu/drm/i915/display/intel_dp.c +0 -0
@@ -6452,8 +6504,7 @@ intel_dp_init_connector(struct
 intel_digital_port *dig_port,
         int type;


        /* Initialize the work for modeset in case of link train failure */
-       INIT_WORK(&intel_connector->modeset_retry_work, -                 intel_dp_modeset_retry_work_fn);
+       intel_dp_init_modeset_retry_work(intel_connector);
        if (drm_WARN(dev, dig_port->max_lanes < 1, "Not enough lanes (%d) for DP on [ENCODER:%d:%s]\n",

drivers/gpu/drm/i915/display/intel_dp.h +0 -0
index 530cc97bc42f4..105c2086310db 100644
@@ -23,6 +23,8 @@ struct intel_digital_port;
  struct intel_dp;
  struct intel_encoder;

drivers/gpu/drm/i915/display/intel_dp.h +2 -0
+struct work_struct;
+

 struct link_config_limits {
        int min_rate, max_rate;
        int min_lane_count, max_lane_count;

drivers/gpu/drm/i915/display/intel_dp.h +0 -0
@@ -43,6 +45,12 @@ void intel_dp_adjust_compliance_config(struct
 intel_dp *intel_dp,

 bool intel_dp_limited_color_range(const struct intel_crtc_state *crtc_state,
                                  const struct drm_connector_state *conn_state);
 int intel_dp_min_bpp(enum intel_output_format output_format); +void intel_dp_init_modeset_retry_work(struct intel_connector *connector);
+void intel_dp_queue_modeset_retry_work(struct intel_connector *connector);
+void intel_dp_queue_modeset_retry_for_link(struct intel_atomic_state *state,
+                                          struct intel_encoder *encoder,
+                                          const struct intel_crtc_state *crtc_state,
+                                          const struct drm_connector_state *conn_state);
 bool intel_dp_init_connector(struct intel_digital_port *dig_port, struct intel_connector *intel_connector);
 void intel_dp_set_link_params(struct intel_dp *intel_dp,

drivers/gpu/drm/i915/display/intel_dp_link_training.c +0 -0
index 1abfafbbfa757..7b140cbf8dd31 100644
@@ -1075,7 +1075,6 @@ static void
 intel_dp_schedule_fallback_link_training(struct intel_dp *intel_dp,

                                                     const struct intel_crtc_state *crtc_state)
 {
        struct intel_connector *intel_connector = intel_dp-

Quoted 1 line

attached_connector;

-       struct drm_i915_private *i915 = dp_to_i915(intel_dp);
        if (!intel_digital_port_connected(&dp_to_dig_port(intel_dp)-

Quoted 1 line

base)) {

                lt_dbg(intel_dp, DP_PHY_DPRX, "Link Training failed on disconnected sink.\n");

drivers/gpu/drm/i915/display/intel_dp_link_training.c +0 -0
@@ -1093,7 +1092,7 @@ static void
 intel_dp_schedule_fallback_link_training(struct intel_dp *intel_dp,
         }


        /* Schedule a Hotplug Uevent to userspace to start modeset */ -       queue_work(i915->unordered_wq, &intel_connector-

Quoted 1 line

modeset_retry_work);

+       intel_dp_queue_modeset_retry_work(intel_connector); }

 /* Perform the link training on all LTTPRs and the DPRX on a link. */

drivers/gpu/drm/i915/display/intel_dp_mst.c +0 -0
index 5fa25a5a36b55..b15e43ebf138b 100644
@@ -1542,6 +1542,8 @@ static struct drm_connector
 *intel_dp_add_mst_connector(struct drm_dp_mst_topolo
         intel_connector->port = port;
         drm_dp_mst_get_port_malloc(port);


+       intel_dp_init_modeset_retry_work(intel_connector); +
        intel_connector->dp.dsc_decompression_aux = drm_dp_mst_dsc_aux_for_port(port); intel_dp_mst_read_decompression_port_dsc_caps(intel_dp, intel_connector); intel_connector->dp.dsc_hblank_expansion_quirk =

author
#20 lore raw

Re: [PATCH 03/19] drm/i915/dp: Add support to notify MST connectors to retry modesets

Quoted 32 lines · Jouni
Quoted 18 lines · Imre Deak

[...]

drivers/gpu/drm/i915/display/intel_dp.c +17 -0
+void
+intel_dp_queue_modeset_retry_for_link(struct intel_atomic_state *state,
+                                     struct intel_encoder *encoder,
+                                     const struct intel_crtc_state *crtc_state,
+                                     const struct drm_connector_state *conn_state)
+{
+       struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
+       struct intel_connector *connector;
+       struct intel_digital_connector_state *iter_conn_state;
+       struct intel_dp *intel_dp;
+       int i;
+
+       if (conn_state) {
+               connector = to_intel_connector(conn_state->connector);
+               intel_dp_queue_modeset_retry_work(connector);
+
+               return;

drivers/gpu/drm/i915/display/intel_dp.c intel_dp_audio_compute_config(struct intel_encoder *encoder, · line 2875

Quoted 10 lines
drivers/gpu/drm/i915/display/intel_dp.c +10 -0
+       }
+
+       if (drm_WARN_ON(&i915->drm,
+                       !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
+               return;
+
+       intel_dp = enc_to_intel_dp(encoder);
+
+       for_each_new_intel_connector_in_state(state, connector, iter_conn_state, i) {
+               (void)iter_conn_state;

Checked iter_conn_state->base->crtc documentation:

Quoted 4 lines

@crtc: CRTC to connect connector to, NULL if disabled.

Do we need to check if connector is "disabled" or is it impossible scenario?

Yes, it does show if the connector is disabled and it would make sense to not notify those. However the check for that would be racy, at least during a non-blocking commit, but I think also in general where userspace could be in the middle of enabling this connector.

The point of the notification is that userspace re-checks the mode it wants on each MST connector to be enabled, so to prevent that it would miss the re-check on connectors with a pending enabling like above, the notification is simply sent to all the connectors in the MST topology.

Quoted 132 lines

BR,

Jouni Högander

Quoted 125 lines
drivers/gpu/drm/i915/display/intel_dp.c +16 -0
+
+               if (connector->mst_port != intel_dp)
+                       continue;
+
+               intel_dp_queue_modeset_retry_work(connector);
+       }
+}
+
 int
 intel_dp_compute_config(struct intel_encoder *encoder,
                        struct intel_crtc_state *pipe_config,
@@ -6436,6 +6480,14 @@ static void
 intel_dp_modeset_retry_work_fn(struct work_struct *work)
        mutex_unlock(&connector->dev->mode_config.mutex);
        /* Send Hotplug uevent so userspace can reprobe */
        drm_kms_helper_connector_hotplug_event(connector);
+
+       drm_connector_put(connector);
+}
+
+void intel_dp_init_modeset_retry_work(struct intel_connector
 *connector)
+{
+       INIT_WORK(&connector->modeset_retry_work,
+                 intel_dp_modeset_retry_work_fn);
 }

bool

drivers/gpu/drm/i915/display/intel_dp.c +0 -0
@@ -6452,8 +6504,7 @@ intel_dp_init_connector(struct
 intel_digital_port *dig_port,
        int type;
 
        /* Initialize the work for modeset in case of link train

failure */

drivers/gpu/drm/i915/display/intel_dp.c +1 -2
-       INIT_WORK(&intel_connector->modeset_retry_work,
-                 intel_dp_modeset_retry_work_fn);
+       intel_dp_init_modeset_retry_work(intel_connector);
 
        if (drm_WARN(dev, dig_port->max_lanes < 1,
                     "Not enough lanes (%d) for DP on

[ENCODER:%d:%s]\n",

drivers/gpu/drm/i915/display/intel_dp.h +3 -0
index 530cc97bc42f4..105c2086310db 100644
@@ -23,6 +23,8 @@ struct intel_digital_port;
 struct intel_dp;
 struct intel_encoder;
 
+struct work_struct;
+
 struct link_config_limits {
        int min_rate, max_rate;
        int min_lane_count, max_lane_count;
@@ -43,6 +45,12 @@ void intel_dp_adjust_compliance_config(struct
 intel_dp *intel_dp,
 bool intel_dp_limited_color_range(const struct intel_crtc_state
 *crtc_state,
                                  const struct drm_connector_state
 *conn_state);
 int intel_dp_min_bpp(enum intel_output_format output_format);
+void intel_dp_init_modeset_retry_work(struct intel_connector

*connector);
+void intel_dp_queue_modeset_retry_work(struct intel_connector *connector);
+void intel_dp_queue_modeset_retry_for_link(struct intel_atomic_state *state,
+ struct intel_encoder
*encoder,
+ const struct
intel_crtc_state *crtc_state,
+ const struct
drm_connector_state *conn_state);
bool intel_dp_init_connector(struct intel_digital_port *dig_port,
struct intel_connector
*intel_connector);
void intel_dp_set_link_params(struct intel_dp *intel_dp,

drivers/gpu/drm/i915/display/intel_dp_link_training.c +0 -1
index 1abfafbbfa757..7b140cbf8dd31 100644
@@ -1075,7 +1075,6 @@ static void
 intel_dp_schedule_fallback_link_training(struct intel_dp *intel_dp,
                                                     const struct
 intel_crtc_state *crtc_state)
 {
        struct intel_connector *intel_connector = intel_dp-
 >attached_connector;
-       struct drm_i915_private *i915 = dp_to_i915(intel_dp);

if (!intel_digital_port_connected(&dp_to_dig_port(intel_dp)-

Quoted 1 line

base)) {

lt_dbg(intel_dp, DP_PHY_DPRX, "Link Training failed
on disconnected sink.\n");

drivers/gpu/drm/i915/display/intel_dp_link_training.c +1 -1
@@ -1093,7 +1092,7 @@ static void
 intel_dp_schedule_fallback_link_training(struct intel_dp *intel_dp,
        }
 
        /* Schedule a Hotplug Uevent to userspace to start modeset */
-       queue_work(i915->unordered_wq, &intel_connector-
 >modeset_retry_work);
+       intel_dp_queue_modeset_retry_work(intel_connector);
 }

/* Perform the link training on all LTTPRs and the DPRX on a link.
*/

drivers/gpu/drm/i915/display/intel_dp_mst.c +2 -0
index 5fa25a5a36b55..b15e43ebf138b 100644
@@ -1542,6 +1542,8 @@ static struct drm_connector
 *intel_dp_add_mst_connector(struct drm_dp_mst_topolo
        intel_connector->port = port;
        drm_dp_mst_get_port_malloc(port);
 
+       intel_dp_init_modeset_retry_work(intel_connector);
+
        intel_connector->dp.dsc_decompression_aux =
 drm_dp_mst_dsc_aux_for_port(port);

intel_dp_mst_read_decompression_port_dsc_caps(intel_dp,

intel_connector);
        intel_connector->dp.dsc_hblank_expansion_quirk =
author
#21 lore raw

PATCH 4/19 drm/i915/dp: Use drm_dp_max_dprx_data_rate()

In reply to Imre Deak

Instead of intel_dp_max_data_rate() use the equivalent drm_dp_max_dprx_data_rate() which was copied from the former one in a previous patch.

---
 drivers/gpu/drm/i915/display/intel_display.c |  2 +-
 drivers/gpu/drm/i915/display/intel_dp.c      | 62 +++-----------------
 drivers/gpu/drm/i915/display/intel_dp.h      |  1 -
 drivers/gpu/drm/i915/display/intel_dp_mst.c  |  2 +-
 4 files changed, 10 insertions(+), 57 deletions(-)
drivers/gpu/drm/i915/display/intel_display.c +1 -1
index 0caebbb3e2dbb..b9f985a5e705b 100644
@@ -2478,7 +2478,7 @@ intel_link_compute_m_n(u16 bits_per_pixel_x16, int nlanes,
 	u32 link_symbol_clock = intel_dp_link_symbol_clock(link_clock);
 	u32 data_m = intel_dp_effective_data_rate(pixel_clock, bits_per_pixel_x16,
 						  bw_overhead);
-	u32 data_n = intel_dp_max_data_rate(link_clock, nlanes);
+	u32 data_n = drm_dp_max_dprx_data_rate(link_clock, nlanes);
 
 	/*
 	 * Windows/BIOS uses fixed M/N values always. Follow suit.
drivers/gpu/drm/i915/display/intel_dp.c +8 -54

98 lines not on this page · raw message

drivers/gpu/drm/i915/display/intel_dp.h +0 -1
index 105c2086310db..46f79747f807d 100644
@@ -113,7 +113,6 @@ bool intel_dp_get_colorimetry_status(struct intel_dp *intel_dp);
 int intel_dp_link_required(int pixel_clock, int bpp);
 int intel_dp_effective_data_rate(int pixel_clock, int bpp_x16,
 				 int bw_overhead);
-int intel_dp_max_data_rate(int max_link_rate, int max_lanes);
 bool intel_dp_can_bigjoiner(struct intel_dp *intel_dp);
 bool intel_dp_needs_vsc_sdp(const struct intel_crtc_state *crtc_state,
 			    const struct drm_connector_state *conn_state);
drivers/gpu/drm/i915/display/intel_dp_mst.c +1 -1
index b15e43ebf138b..cfcc157b7d41d 100644
@@ -1295,7 +1295,7 @@ intel_dp_mst_mode_valid_ctx(struct drm_connector *connector,
 	max_link_clock = intel_dp_max_link_rate(intel_dp);
 	max_lanes = intel_dp_max_lane_count(intel_dp);
 
-	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
+	max_rate = drm_dp_max_dprx_data_rate(max_link_clock, max_lanes);
 	mode_rate = intel_dp_link_required(mode->clock, min_bpp);
 
 	ret = drm_modeset_lock(&mgr->base.lock, ctx);
Signature · 1 line
2.39.2
Reviewed-by on v1 4/19
#22 lore raw

RE: [PATCH 04/19] drm/i915/dp: Use drm_dp_max_dprx_data_rate()

Quoted 10 lines

-----Original Message-----
From: Intel-gfx <[email protected]> On Behalf Of Imre Deak
Sent: Tuesday, January 23, 2024 3:59 PM
To: [email protected]
Cc: [email protected]
Subject: [PATCH 04/19] drm/i915/dp: Use drm_dp_max_dprx_data_rate()

Quoted 3 lines

Instead of intel_dp_max_data_rate() use the equivalent drm_dp_max_dprx_data_rate() which was copied from the former one in a previous patch.

Looks good to me.

Quoted 179 lines
---
 drivers/gpu/drm/i915/display/intel_display.c |  2 +-
 drivers/gpu/drm/i915/display/intel_dp.c      | 62 +++-----------------
 drivers/gpu/drm/i915/display/intel_dp.h      |  1 -
 drivers/gpu/drm/i915/display/intel_dp_mst.c  |  2 +-
 4 files changed, 10 insertions(+), 57 deletions(-)
drivers/gpu/drm/i915/display/intel_display.c +1 -1
index 0caebbb3e2dbb..b9f985a5e705b 100644
@@ -2478,7 +2478,7 @@ intel_link_compute_m_n(u16 bits_per_pixel_x16, int
 nlanes,
 	u32 link_symbol_clock = intel_dp_link_symbol_clock(link_clock);
 	u32 data_m = intel_dp_effective_data_rate(pixel_clock,
 bits_per_pixel_x16,
 						  bw_overhead);
-	u32 data_n = intel_dp_max_data_rate(link_clock, nlanes);
+	u32 data_n = drm_dp_max_dprx_data_rate(link_clock, nlanes);
 
drivers/gpu/drm/i915/display/intel_display.c +0 -0
 	/*
 	 * Windows/BIOS uses fixed M/N values always. Follow suit.
drivers/gpu/drm/i915/display/intel_dp.c +0 -16
index 4e36c2c39888e..c7b06a9b197cc 100644
@@ -383,52 +383,6 @@ int intel_dp_effective_data_rate(int pixel_clock, int
 bpp_x16,
 				1000000 * 16 * 8);
 }
 
-/*
- * Given a link rate and lanes, get the data bandwidth.
- *
- * Data bandwidth is the actual payload rate, which depends on the data
- * bandwidth efficiency and the link rate.
- *
- * For 8b/10b channel encoding, SST and non-FEC, the data bandwidth efficiency
- * is 80%. For example, for a 1.62 Gbps link, 1.62*10^9 bps * 0.80 * (1/8) =
- * 162000 kBps. With 8-bit symbols, we have 162000 kHz symbol clock. Just by
- * coincidence, the port clock in kHz matches the data bandwidth in kBps, and
- * they equal the link bit rate in Gbps multiplied by 100000. (Note that this no
- * longer holds for data bandwidth as soon as FEC or MST is taken into account!)
- *
- * For 128b/132b channel encoding, the data bandwidth efficiency is 96.71%. For
- * example, for a 10 Gbps link, 10*10^9 bps * 0.9671 * (1/8) = 1208875
- * kBps. With 32-bit symbols, we have 312500 kHz symbol clock. The value

1000000
- * does not match the symbol clock, the port clock (not even if you think in
- * terms of a byte clock), nor the data bandwidth. It only matches the link bit
- * rate in units of 10000 bps.
- */

drivers/gpu/drm/i915/display/intel_dp.c +0 -4
-int
-intel_dp_max_data_rate(int max_link_rate, int max_lanes) -{
-	int ch_coding_efficiency =
-

drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_
rate));

drivers/gpu/drm/i915/display/intel_dp.c +0 -14
-	int max_link_rate_kbps = max_link_rate * 10;
-
-	/*
-	 * UHBR rates always use 128b/132b channel encoding, and have
-	 * 97.71% data bandwidth efficiency. Consider max_link_rate the
-	 * link bit rate in units of 10000 bps.
-	 */
-	/*
-	 * Lower than UHBR rates always use 8b/10b channel encoding, and have
-	 * 80% data bandwidth efficiency for SST non-FEC. However, this turns
-	 * out to be a nop by coincidence:
-	 *
-	 *	int max_link_rate_kbps = max_link_rate * 10;
-	 *	max_link_rate_kbps =

DIV_ROUND_DOWN_ULL(max_link_rate_kbps * 8, 10);

drivers/gpu/drm/i915/display/intel_dp.c +0 -3
-	 *	max_link_rate = max_link_rate_kbps / 8;
-	 */
-	return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate_kbps *

max_lanes,

drivers/gpu/drm/i915/display/intel_dp.c +0 -5
-					      ch_coding_efficiency),
-				  1000000 * 8);
-}
-
 bool intel_dp_can_bigjoiner(struct intel_dp *intel_dp)  {
 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp); @@
-658,7 +612,7 @@ static bool intel_dp_can_link_train_fallback_for_edp(struct

intel_dp *intel_dp,

drivers/gpu/drm/i915/display/intel_dp.c +2 -2
 	int mode_rate, max_rate;
 
 	mode_rate = intel_dp_link_required(fixed_mode->clock, 18);
-	max_rate = intel_dp_max_data_rate(link_rate, lane_count);
+	max_rate = drm_dp_max_dprx_data_rate(link_rate, lane_count);
 	if (mode_rate > max_rate)
 		return false;
@@ -1260,7 +1214,7 @@ intel_dp_mode_valid(struct drm_connector
 *_connector,
 	max_link_clock = intel_dp_max_link_rate(intel_dp);
 	max_lanes = intel_dp_max_lane_count(intel_dp);
 
-	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
+	max_rate = drm_dp_max_dprx_data_rate(max_link_clock, max_lanes);
 	mode_rate = intel_dp_link_required(target_clock,
 

intel_dp_mode_min_output_bpp(connector, mode));

drivers/gpu/drm/i915/display/intel_dp.c +2 -2
@@ -1610,8 +1564,8 @@ intel_dp_compute_link_config_wide(struct intel_dp
 *intel_dp,
 			for (lane_count = limits->min_lane_count;
 			     lane_count <= limits->max_lane_count;
 			     lane_count <<= 1) {
-				link_avail = intel_dp_max_data_rate(link_rate,
-								    lane_count);
+				link_avail =
 drm_dp_max_dprx_data_rate(link_rate,
+
 lane_count);
drivers/gpu/drm/i915/display/intel_dp.c +2 -2
 				if (mode_rate <= link_avail) {
 					pipe_config->lane_count = lane_count;
@@ -2462,8 +2416,8 @@ intel_dp_compute_link_config(struct intel_encoder
 *encoder,
 			    "DP link rate required %i available %i\n",
 			    intel_dp_link_required(adjusted_mode->crtc_clock,
 
 to_bpp_int_roundup(pipe_config->dsc.compressed_bpp_x16)),
-			    intel_dp_max_data_rate(pipe_config->port_clock,
-						   pipe_config->lane_count));
+			    drm_dp_max_dprx_data_rate(pipe_config-
 >port_clock,
+						      pipe_config->lane_count));
drivers/gpu/drm/i915/display/intel_dp.c +0 -0
 	} else {
 		drm_dbg_kms(&i915->drm, "DP lane count %d clock %d bpp

%d\n",

drivers/gpu/drm/i915/display/intel_dp.c +2 -2
 			    pipe_config->lane_count, pipe_config->port_clock,
@@ -2473,8 +2427,8 @@ intel_dp_compute_link_config(struct intel_encoder
 *encoder,
 			    "DP link rate required %i available %i\n",
 			    intel_dp_link_required(adjusted_mode->crtc_clock,
 						   pipe_config->pipe_bpp),
-			    intel_dp_max_data_rate(pipe_config->port_clock,
-						   pipe_config->lane_count));
+			    drm_dp_max_dprx_data_rate(pipe_config-
 >port_clock,
+						      pipe_config->lane_count));
 	}
drivers/gpu/drm/i915/display/intel_dp.c +0 -0
 	return 0;
 }
drivers/gpu/drm/i915/display/intel_dp.h +0 -1
index 105c2086310db..46f79747f807d 100644
@@ -113,7 +113,6 @@ bool intel_dp_get_colorimetry_status(struct intel_dp
 *intel_dp);  int intel_dp_link_required(int pixel_clock, int bpp);  int
 intel_dp_effective_data_rate(int pixel_clock, int bpp_x16,
 				 int bw_overhead);
-int intel_dp_max_data_rate(int max_link_rate, int max_lanes);  bool

intel_dp_can_bigjoiner(struct intel_dp *intel_dp); bool intel_dp_needs_vsc_sdp(const struct intel_crtc_state *crtc_state,
const struct drm_connector_state *conn_state); diff --
git a/drivers/gpu/drm/i915/display/intel_dp_mst.c b/drivers/gpu/drm/i915/display/intel_dp_mst.c
index b15e43ebf138b..cfcc157b7d41d 100644

drivers/gpu/drm/i915/display/intel_dp_mst.c +1 -1
@@ -1295,7 +1295,7 @@ intel_dp_mst_mode_valid_ctx(struct drm_connector
 *connector,
 	max_link_clock = intel_dp_max_link_rate(intel_dp);
 	max_lanes = intel_dp_max_lane_count(intel_dp);
 
-	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
+	max_rate = drm_dp_max_dprx_data_rate(max_link_clock, max_lanes);
 	mode_rate = intel_dp_link_required(mode->clock, min_bpp);
 

ret = drm_modeset_lock(&mgr->base.lock, ctx);
--
2.39.2

author
#23 lore raw

PATCH 5/19 drm/i915/dp: Factor out intel_dp_config_required_rate()

In reply to Imre Deak

Factor out intel_dp_config_required_rate() used by a follow-up patch enabling the DP tunnel BW allocation mode.

---
 drivers/gpu/drm/i915/display/intel_dp.c | 43 +++++++++++--------------
 drivers/gpu/drm/i915/display/intel_dp.h |  1 +
 2 files changed, 20 insertions(+), 24 deletions(-)
drivers/gpu/drm/i915/display/intel_dp.c +19 -24
index c7b06a9b197cc..0a5c60428ffb7 100644
@@ -2338,6 +2338,17 @@ intel_dp_compute_config_limits(struct intel_dp *intel_dp,
 						       limits);
 }
 
+int intel_dp_config_required_rate(const struct intel_crtc_state *crtc_state)
+{
+	const struct drm_display_mode *adjusted_mode =
+		&crtc_state->hw.adjusted_mode;
+	int bpp = crtc_state->dsc.compression_enable ?
+		to_bpp_int_roundup(crtc_state->dsc.compressed_bpp_x16) :
+		crtc_state->pipe_bpp;
+
+	return intel_dp_link_required(adjusted_mode->crtc_clock, bpp);
+}
+
 static int
 intel_dp_compute_link_config(struct intel_encoder *encoder,
 			     struct intel_crtc_state *pipe_config,
@@ -2405,31 +2416,15 @@ intel_dp_compute_link_config(struct intel_encoder *encoder,
 			return ret;
 	}
 
-	if (pipe_config->dsc.compression_enable) {
-		drm_dbg_kms(&i915->drm,
-			    "DP lane count %d clock %d Input bpp %d Compressed bpp " BPP_X16_FMT "\n",
-			    pipe_config->lane_count, pipe_config->port_clock,
-			    pipe_config->pipe_bpp,
-			    BPP_X16_ARGS(pipe_config->dsc.compressed_bpp_x16));
+	drm_dbg_kms(&i915->drm,
+		    "DP lane count %d clock %d bpp input %d compressed " BPP_X16_FMT " link rate required %d available %d\n",
+		    pipe_config->lane_count, pipe_config->port_clock,
+		    pipe_config->pipe_bpp,
+		    BPP_X16_ARGS(pipe_config->dsc.compressed_bpp_x16),
+		    intel_dp_config_required_rate(pipe_config),
+		    drm_dp_max_dprx_data_rate(pipe_config->port_clock,
+					      pipe_config->lane_count));
 
-		drm_dbg_kms(&i915->drm,
-			    "DP link rate required %i available %i\n",
-			    intel_dp_link_required(adjusted_mode->crtc_clock,
-						   to_bpp_int_roundup(pipe_config->dsc.compressed_bpp_x16)),
-			    drm_dp_max_dprx_data_rate(pipe_config->port_clock,
-						      pipe_config->lane_count));
-	} else {
-		drm_dbg_kms(&i915->drm, "DP lane count %d clock %d bpp %d\n",
-			    pipe_config->lane_count, pipe_config->port_clock,
-			    pipe_config->pipe_bpp);
-
-		drm_dbg_kms(&i915->drm,
-			    "DP link rate required %i available %i\n",
-			    intel_dp_link_required(adjusted_mode->crtc_clock,
-						   pipe_config->pipe_bpp),
-			    drm_dp_max_dprx_data_rate(pipe_config->port_clock,
-						      pipe_config->lane_count));
-	}
 	return 0;
 }
 
drivers/gpu/drm/i915/display/intel_dp.h +1 -0
index 46f79747f807d..37274e3c2902f 100644
@@ -102,6 +102,7 @@ void intel_dp_mst_suspend(struct drm_i915_private *dev_priv);
 void intel_dp_mst_resume(struct drm_i915_private *dev_priv);
 int intel_dp_max_link_rate(struct intel_dp *intel_dp);
 int intel_dp_max_lane_count(struct intel_dp *intel_dp);
+int intel_dp_config_required_rate(const struct intel_crtc_state *crtc_state);
 int intel_dp_rate_select(struct intel_dp *intel_dp, int rate);
 
 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
Signature · 1 line
2.39.2
Reviewed-by on v1 5/19
#24 lore raw

RE: [PATCH 05/19] drm/i915/dp: Factor out intel_dp_config_required_rate()

Quoted 10 lines

-----Original Message-----
From: dri-devel <[email protected]> On Behalf Of Imre Deak
Sent: Tuesday, January 23, 2024 3:59 PM
To: [email protected]
Cc: [email protected]
Subject: [PATCH 05/19] drm/i915/dp: Factor out intel_dp_config_required_rate()

Quoted 2 lines

Factor out intel_dp_config_required_rate() used by a follow-up patch enabling the DP tunnel BW allocation mode.

Looks good to me.

Quoted 92 lines
---
 drivers/gpu/drm/i915/display/intel_dp.c | 43 +++++++++++--------------
drivers/gpu/drm/i915/display/intel_dp.h |  1 +
 2 files changed, 20 insertions(+), 24 deletions(-)
drivers/gpu/drm/i915/display/intel_dp.c +11 -3
index c7b06a9b197cc..0a5c60428ffb7 100644
@@ -2338,6 +2338,17 @@ intel_dp_compute_config_limits(struct intel_dp
 *intel_dp,
 						       limits);
 }
 
+int intel_dp_config_required_rate(const struct intel_crtc_state
+*crtc_state) {
+	const struct drm_display_mode *adjusted_mode =
+		&crtc_state->hw.adjusted_mode;
+	int bpp = crtc_state->dsc.compression_enable ?
+		to_bpp_int_roundup(crtc_state->dsc.compressed_bpp_x16) :
+		crtc_state->pipe_bpp;
+
+	return intel_dp_link_required(adjusted_mode->crtc_clock, bpp); }
+
 static int
 intel_dp_compute_link_config(struct intel_encoder *encoder,
 			     struct intel_crtc_state *pipe_config, @@ -2405,31
+2416,15 @@ intel_dp_compute_link_config(struct intel_encoder *encoder,
 			return ret;
 	}
 
-	if (pipe_config->dsc.compression_enable) {
-		drm_dbg_kms(&i915->drm,
-			    "DP lane count %d clock %d Input bpp %d Compressed

bpp " BPP_X16_FMT "\n",

drivers/gpu/drm/i915/display/intel_dp.c +0 -3
-			    pipe_config->lane_count, pipe_config->port_clock,
-			    pipe_config->pipe_bpp,
-			    BPP_X16_ARGS(pipe_config-
Quoted 1 line

dsc.compressed_bpp_x16));

drivers/gpu/drm/i915/display/intel_dp.c +2 -0
+	drm_dbg_kms(&i915->drm,
+		    "DP lane count %d clock %d bpp input %d compressed "

BPP_X16_FMT " link rate required %d available %d\n",

drivers/gpu/drm/i915/display/intel_dp.c +6 -4
+		    pipe_config->lane_count, pipe_config->port_clock,
+		    pipe_config->pipe_bpp,
+		    BPP_X16_ARGS(pipe_config->dsc.compressed_bpp_x16),
+		    intel_dp_config_required_rate(pipe_config),
+		    drm_dp_max_dprx_data_rate(pipe_config->port_clock,
+					      pipe_config->lane_count));
 
-		drm_dbg_kms(&i915->drm,
-			    "DP link rate required %i available %i\n",
-			    intel_dp_link_required(adjusted_mode->crtc_clock,
-

to_bpp_int_roundup(pipe_config->dsc.compressed_bpp_x16)),

drivers/gpu/drm/i915/display/intel_dp.c +0 -1
-			    drm_dp_max_dprx_data_rate(pipe_config-
Quoted 1 line

port_clock,

drivers/gpu/drm/i915/display/intel_dp.c +0 -3
-						      pipe_config->lane_count));
-	} else {
-		drm_dbg_kms(&i915->drm, "DP lane count %d clock %d bpp

%d\n",

drivers/gpu/drm/i915/display/intel_dp.c +0 -8
-			    pipe_config->lane_count, pipe_config->port_clock,
-			    pipe_config->pipe_bpp);
-
-		drm_dbg_kms(&i915->drm,
-			    "DP link rate required %i available %i\n",
-			    intel_dp_link_required(adjusted_mode->crtc_clock,
-						   pipe_config->pipe_bpp),
-			    drm_dp_max_dprx_data_rate(pipe_config-
Quoted 1 line

port_clock,

drivers/gpu/drm/i915/display/intel_dp.c +0 -2
-						      pipe_config->lane_count));
-	}
 	return 0;
 }
drivers/gpu/drm/i915/display/intel_dp.h +0 -0
index 46f79747f807d..37274e3c2902f 100644
@@ -102,6 +102,7 @@ void intel_dp_mst_suspend(struct drm_i915_private
 *dev_priv);  void intel_dp_mst_resume(struct drm_i915_private *dev_priv);  int

intel_dp_max_link_rate(struct intel_dp *intel_dp); int intel_dp_max_lane_count(struct intel_dp *intel_dp);

drivers/gpu/drm/i915/display/intel_dp.h +2 -1
+int intel_dp_config_required_rate(const struct intel_crtc_state
+*crtc_state);
 int intel_dp_rate_select(struct intel_dp *intel_dp, int rate);
 
 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
--

2.39.2

author
#25 lore raw

PATCH 6/19 drm/i915/dp: Export intel_dp_max_common_rate/lane_count()

In reply to Imre Deak

Export intel_dp_max_common_rate() and intel_dp_max_lane_count() used by a follow-up patch enabling the DP tunnel BW allocation mode.

---
 drivers/gpu/drm/i915/display/intel_dp.c | 4 ++--
 drivers/gpu/drm/i915/display/intel_dp.h | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/display/intel_dp.c +2 -2
index 0a5c60428ffb7..f40706c5d1aad 100644
@@ -309,7 +309,7 @@ static int intel_dp_common_rate(struct intel_dp *intel_dp, int index)
 }
 
 /* Theoretical max between source and sink */
-static int intel_dp_max_common_rate(struct intel_dp *intel_dp)
+int intel_dp_max_common_rate(struct intel_dp *intel_dp)
 {
 	return intel_dp_common_rate(intel_dp, intel_dp->num_common_rates - 1);
 }
@@ -326,7 +326,7 @@ static int intel_dp_max_source_lane_count(struct intel_digital_port *dig_port)
 }
 
 /* Theoretical max between source and sink */
-static int intel_dp_max_common_lane_count(struct intel_dp *intel_dp)
+int intel_dp_max_common_lane_count(struct intel_dp *intel_dp)
 {
 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
 	int source_max = intel_dp_max_source_lane_count(dig_port);
drivers/gpu/drm/i915/display/intel_dp.h +2 -0
index 37274e3c2902f..a7906d8738c4a 100644
@@ -104,6 +104,8 @@ int intel_dp_max_link_rate(struct intel_dp *intel_dp);
 int intel_dp_max_lane_count(struct intel_dp *intel_dp);
 int intel_dp_config_required_rate(const struct intel_crtc_state *crtc_state);
 int intel_dp_rate_select(struct intel_dp *intel_dp, int rate);
+int intel_dp_max_common_rate(struct intel_dp *intel_dp);
+int intel_dp_max_common_lane_count(struct intel_dp *intel_dp);
 
 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
 			   u8 *link_bw, u8 *rate_select);
Signature · 1 line
2.39.2
Reviewed-by on v1 6/19
#26 lore raw

RE: [PATCH 06/19] drm/i915/dp: Export intel_dp_max_common_rate/lane_count()

Quoted 11 lines

-----Original Message-----
From: Intel-gfx <[email protected]> On Behalf Of Imre Deak
Sent: Tuesday, January 23, 2024 3:59 PM
To: [email protected]
Cc: [email protected]
Subject: [PATCH 06/19] drm/i915/dp: Export intel_dp_max_common_rate/lane_count()

Quoted 2 lines

Export intel_dp_max_common_rate() and intel_dp_max_lane_count() used by a follow-up patch enabling the DP tunnel BW allocation mode.

Looks good to me.

Quoted 44 lines
---
 drivers/gpu/drm/i915/display/intel_dp.c | 4 ++--
drivers/gpu/drm/i915/display/intel_dp.h | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/display/intel_dp.c +1 -0
index 0a5c60428ffb7..f40706c5d1aad 100644
@@ -309,7 +309,7 @@ static int intel_dp_common_rate(struct intel_dp
 *intel_dp, int index)  }
 
 /* Theoretical max between source and sink */ -static int
 intel_dp_max_common_rate(struct intel_dp *intel_dp)
+int intel_dp_max_common_rate(struct intel_dp *intel_dp)
 {
 	return intel_dp_common_rate(intel_dp, intel_dp->num_common_rates -

1); } @@ -326,7 +326,7 @@ static int intel_dp_max_source_lane_count(struct intel_digital_port *dig_port) }

/* Theoretical max between source and sink */ -static int
intel_dp_max_common_lane_count(struct intel_dp *intel_dp)

drivers/gpu/drm/i915/display/intel_dp.c +1 -0
+int intel_dp_max_common_lane_count(struct intel_dp *intel_dp)
 {
 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
 	int source_max = intel_dp_max_source_lane_count(dig_port);
drivers/gpu/drm/i915/display/intel_dp.h +0 -0
index 37274e3c2902f..a7906d8738c4a 100644
@@ -104,6 +104,8 @@ int intel_dp_max_link_rate(struct intel_dp *intel_dp);  int

intel_dp_max_lane_count(struct intel_dp *intel_dp); int intel_dp_config_required_rate(const struct intel_crtc_state *crtc_state); int intel_dp_rate_select(struct intel_dp *intel_dp, int rate);

drivers/gpu/drm/i915/display/intel_dp.h +2 -1
+int intel_dp_max_common_rate(struct intel_dp *intel_dp); int
+intel_dp_max_common_lane_count(struct intel_dp *intel_dp);
 
 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
 			   u8 *link_bw, u8 *rate_select);
--

2.39.2

author
#27 lore raw

PATCH 7/19 drm/i915/dp: Factor out intel_dp_update_sink_caps()

In reply to Imre Deak

Factor out a function updating the sink's link rate and lane count capabilities, used by a follow-up patch enabling the DP tunnel BW allocation mode.

---
 drivers/gpu/drm/i915/display/intel_dp.c | 11 ++++++++---
 drivers/gpu/drm/i915/display/intel_dp.h |  1 +
 2 files changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/display/intel_dp.c +8 -3
index f40706c5d1aad..23434d0aba188 100644
@@ -3949,6 +3949,13 @@ intel_dp_has_sink_count(struct intel_dp *intel_dp)
 					  &intel_dp->desc);
 }
 
+void intel_dp_update_sink_caps(struct intel_dp *intel_dp)
+{
+	intel_dp_set_sink_rates(intel_dp);
+	intel_dp_set_max_sink_lane_count(intel_dp);
+	intel_dp_set_common_rates(intel_dp);
+}
+
 static bool
 intel_dp_get_dpcd(struct intel_dp *intel_dp)
 {
@@ -3965,9 +3972,7 @@ intel_dp_get_dpcd(struct intel_dp *intel_dp)
 		drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
 				 drm_dp_is_branch(intel_dp->dpcd));
 
-		intel_dp_set_sink_rates(intel_dp);
-		intel_dp_set_max_sink_lane_count(intel_dp);
-		intel_dp_set_common_rates(intel_dp);
+		intel_dp_update_sink_caps(intel_dp);
 	}
 
 	if (intel_dp_has_sink_count(intel_dp)) {
drivers/gpu/drm/i915/display/intel_dp.h +1 -0
index a7906d8738c4a..49553e43add22 100644
@@ -106,6 +106,7 @@ int intel_dp_config_required_rate(const struct intel_crtc_state *crtc_state);
 int intel_dp_rate_select(struct intel_dp *intel_dp, int rate);
 int intel_dp_max_common_rate(struct intel_dp *intel_dp);
 int intel_dp_max_common_lane_count(struct intel_dp *intel_dp);
+void intel_dp_update_sink_caps(struct intel_dp *intel_dp);
 
 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
 			   u8 *link_bw, u8 *rate_select);
Signature · 1 line
2.39.2
Reviewed-by on v1 7/19
#28 lore raw

RE: [PATCH 07/19] drm/i915/dp: Factor out intel_dp_update_sink_caps()

Quoted 10 lines

-----Original Message-----
From: Intel-gfx <[email protected]> On Behalf Of Imre Deak
Sent: Tuesday, January 23, 2024 3:59 PM
To: [email protected]
Cc: [email protected]
Subject: [PATCH 07/19] drm/i915/dp: Factor out intel_dp_update_sink_caps()

Quoted 2 lines

Factor out a function updating the sink's link rate and lane count capabilities, used by a follow-up patch enabling the DP tunnel BW allocation mode.

Looks good to me.

Quoted 49 lines
---
 drivers/gpu/drm/i915/display/intel_dp.c | 11 ++++++++---
drivers/gpu/drm/i915/display/intel_dp.h |  1 +
 2 files changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/display/intel_dp.c +7 -3
index f40706c5d1aad..23434d0aba188 100644
@@ -3949,6 +3949,13 @@ intel_dp_has_sink_count(struct intel_dp *intel_dp)
 					  &intel_dp->desc);
 }
 
+void intel_dp_update_sink_caps(struct intel_dp *intel_dp) {
+	intel_dp_set_sink_rates(intel_dp);
+	intel_dp_set_max_sink_lane_count(intel_dp);
+	intel_dp_set_common_rates(intel_dp);
+}
+
 static bool
 intel_dp_get_dpcd(struct intel_dp *intel_dp)  { @@ -3965,9 +3972,7 @@
 intel_dp_get_dpcd(struct intel_dp *intel_dp)
 		drm_dp_read_desc(&intel_dp->aux, &intel_dp->desc,
 				 drm_dp_is_branch(intel_dp->dpcd));
 
-		intel_dp_set_sink_rates(intel_dp);
-		intel_dp_set_max_sink_lane_count(intel_dp);
-		intel_dp_set_common_rates(intel_dp);
+		intel_dp_update_sink_caps(intel_dp);
 	}
 
 	if (intel_dp_has_sink_count(intel_dp)) { diff --git

a/drivers/gpu/drm/i915/display/intel_dp.h
b/drivers/gpu/drm/i915/display/intel_dp.h
index a7906d8738c4a..49553e43add22 100644

drivers/gpu/drm/i915/display/intel_dp.h +0 -0
@@ -106,6 +106,7 @@ int intel_dp_config_required_rate(const struct
 intel_crtc_state *crtc_state);  int intel_dp_rate_select(struct intel_dp *intel_dp,

int rate); int intel_dp_max_common_rate(struct intel_dp *intel_dp); int intel_dp_max_common_lane_count(struct intel_dp *intel_dp);

drivers/gpu/drm/i915/display/intel_dp.h +1 -1
+void intel_dp_update_sink_caps(struct intel_dp *intel_dp);
 
 void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
 			   u8 *link_bw, u8 *rate_select);
--

2.39.2

author
#29 lore raw

PATCH 8/19 drm/i915/dp: Factor out intel_dp_read_dprx_caps()

In reply to Imre Deak

Factor out a function to read the sink's DPRX capabilities used by a follow-up patch enabling the DP tunnel BW allocation mode.

---
 .../drm/i915/display/intel_dp_link_training.c | 30 +++++++++++++++----
 .../drm/i915/display/intel_dp_link_training.h |  1 +
 2 files changed, 26 insertions(+), 5 deletions(-)
drivers/gpu/drm/i915/display/intel_dp_link_training.c +25 -5
index 7b140cbf8dd31..fb84ca98bb7ab 100644
@@ -162,6 +162,28 @@ static int intel_dp_init_lttpr(struct intel_dp *intel_dp, const u8 dpcd[DP_RECEI
 	return lttpr_count;
 }
 
+int intel_dp_read_dprx_caps(struct intel_dp *intel_dp, u8 dpcd[DP_RECEIVER_CAP_SIZE])
+{
+	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
+
+	if (intel_dp_is_edp(intel_dp))
+		return 0;
+
+	/*
+	 * Detecting LTTPRs must be avoided on platforms with an AUX timeout
+	 * period < 3.2ms. (see DP Standard v2.0, 2.11.2, 3.6.6.1).
+	 */
+	if (DISPLAY_VER(i915) >= 10 && !IS_GEMINILAKE(i915))
+		if (drm_dp_dpcd_probe(&intel_dp->aux,
+				      DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV))
+			return -EIO;
+
+	if (drm_dp_read_dpcd_caps(&intel_dp->aux, dpcd))
+		return -EIO;
+
+	return 0;
+}
+
 /**
  * intel_dp_init_lttpr_and_dprx_caps - detect LTTPR and DPRX caps, init the LTTPR link training mode
  * @intel_dp: Intel DP struct
@@ -192,12 +214,10 @@ int intel_dp_init_lttpr_and_dprx_caps(struct intel_dp *intel_dp)
 	if (!intel_dp_is_edp(intel_dp) &&
 	    (DISPLAY_VER(i915) >= 10 && !IS_GEMINILAKE(i915))) {
 		u8 dpcd[DP_RECEIVER_CAP_SIZE];
+		int err = intel_dp_read_dprx_caps(intel_dp, dpcd);
 
-		if (drm_dp_dpcd_probe(&intel_dp->aux, DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV))
-			return -EIO;
-
-		if (drm_dp_read_dpcd_caps(&intel_dp->aux, dpcd))
-			return -EIO;
+		if (err != 0)
+			return err;
 
 		lttpr_count = intel_dp_init_lttpr(intel_dp, dpcd);
 	}
drivers/gpu/drm/i915/display/intel_dp_link_training.h +1 -0
index 2c8f2775891b0..19836a8a4f904 100644
@@ -11,6 +11,7 @@
 struct intel_crtc_state;
 struct intel_dp;
 
+int intel_dp_read_dprx_caps(struct intel_dp *intel_dp, u8 dpcd[DP_RECEIVER_CAP_SIZE]);
 int intel_dp_init_lttpr_and_dprx_caps(struct intel_dp *intel_dp);
 
 void intel_dp_get_adjust_train(struct intel_dp *intel_dp,
Signature · 1 line
2.39.2
Reviewed-by on v1 8/19
#30 lore raw

RE: [PATCH 08/19] drm/i915/dp: Factor out intel_dp_read_dprx_caps()

Quoted 10 lines

-----Original Message-----
From: dri-devel <[email protected]> On Behalf Of Imre Deak
Sent: Tuesday, January 23, 2024 3:59 PM
To: [email protected]
Cc: [email protected]
Subject: [PATCH 08/19] drm/i915/dp: Factor out intel_dp_read_dprx_caps()

Quoted 2 lines

Factor out a function to read the sink's DPRX capabilities used by a follow-up patch enabling the DP tunnel BW allocation mode.

Looks good to me.

Quoted 77 lines
---
 .../drm/i915/display/intel_dp_link_training.c | 30 +++++++++++++++----
.../drm/i915/display/intel_dp_link_training.h |  1 +
 2 files changed, 26 insertions(+), 5 deletions(-)
drivers/gpu/drm/i915/display/intel_dp_link_training.c +0 -0
index 7b140cbf8dd31..fb84ca98bb7ab 100644
@@ -162,6 +162,28 @@ static int intel_dp_init_lttpr(struct intel_dp *intel_dp,

const u8 dpcd[DP_RECEI

drivers/gpu/drm/i915/display/intel_dp_link_training.c +14 -0
 	return lttpr_count;
 }
 
+int intel_dp_read_dprx_caps(struct intel_dp *intel_dp, u8
+dpcd[DP_RECEIVER_CAP_SIZE]) {
+	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
+
+	if (intel_dp_is_edp(intel_dp))
+		return 0;
+
+	/*
+	 * Detecting LTTPRs must be avoided on platforms with an AUX timeout
+	 * period < 3.2ms. (see DP Standard v2.0, 2.11.2, 3.6.6.1).
+	 */
+	if (DISPLAY_VER(i915) >= 10 && !IS_GEMINILAKE(i915))
+		if (drm_dp_dpcd_probe(&intel_dp->aux,
+

DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV))

drivers/gpu/drm/i915/display/intel_dp_link_training.c +8 -0
+			return -EIO;
+
+	if (drm_dp_read_dpcd_caps(&intel_dp->aux, dpcd))
+		return -EIO;
+
+	return 0;
+}
+
 /**
  * intel_dp_init_lttpr_and_dprx_caps - detect LTTPR and DPRX caps, init the

LTTPR link training mode
* @intel_dp: Intel DP struct

drivers/gpu/drm/i915/display/intel_dp_link_training.c +3 -5
@@ -192,12 +214,10 @@ int intel_dp_init_lttpr_and_dprx_caps(struct intel_dp
 *intel_dp)
 	if (!intel_dp_is_edp(intel_dp) &&
 	    (DISPLAY_VER(i915) >= 10 && !IS_GEMINILAKE(i915))) {
 		u8 dpcd[DP_RECEIVER_CAP_SIZE];
+		int err = intel_dp_read_dprx_caps(intel_dp, dpcd);
 
-		if (drm_dp_dpcd_probe(&intel_dp->aux,
 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV))
-			return -EIO;
-
-		if (drm_dp_read_dpcd_caps(&intel_dp->aux, dpcd))
-			return -EIO;
+		if (err != 0)
+			return err;
 
drivers/gpu/drm/i915/display/intel_dp_link_training.c +0 -0
 		lttpr_count = intel_dp_init_lttpr(intel_dp, dpcd);
 	}
drivers/gpu/drm/i915/display/intel_dp_link_training.h +2 -1
index 2c8f2775891b0..19836a8a4f904 100644
@@ -11,6 +11,7 @@
 struct intel_crtc_state;
 struct intel_dp;
 
+int intel_dp_read_dprx_caps(struct intel_dp *intel_dp, u8
+dpcd[DP_RECEIVER_CAP_SIZE]);
 int intel_dp_init_lttpr_and_dprx_caps(struct intel_dp *intel_dp);
 
 void intel_dp_get_adjust_train(struct intel_dp *intel_dp,
--

2.39.2