1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2021 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: AMD
24  *
25  */
26 
27 #include "dc.h"
28 #include "inc/core_status.h"
29 #include "dpcd_defs.h"
30 
31 #include "link_dp_dpia.h"
32 #include "link_hwss.h"
33 #include "dm_helpers.h"
34 #include "dmub/inc/dmub_cmd.h"
35 #include "link_dpcd.h"
36 #include "link_dp_training.h"
37 #include "dc_dmub_srv.h"
38 
39 #define DC_LOGGER \
40 	link->ctx->logger
41 
42 /** @note Can remove once DP tunneling registers in upstream include/drm/drm_dp_helper.h */
43 /* DPCD DP Tunneling over USB4 */
44 #define DP_TUNNELING_CAPABILITIES_SUPPORT 0xe000d
45 #define DP_IN_ADAPTER_INFO                0xe000e
46 #define DP_USB4_DRIVER_ID                 0xe000f
47 #define DP_USB4_ROUTER_TOPOLOGY_ID        0xe001b
48 
dpcd_get_tunneling_device_data(struct dc_link * link)49 enum dc_status dpcd_get_tunneling_device_data(struct dc_link *link)
50 {
51 	enum dc_status status = DC_OK;
52 	uint8_t dpcd_dp_tun_data[3] = {0};
53 	uint8_t dpcd_topology_data[DPCD_USB4_TOPOLOGY_ID_LEN] = {0};
54 	uint8_t i = 0;
55 
56 	status = core_link_read_dpcd(
57 			link,
58 			DP_TUNNELING_CAPABILITIES_SUPPORT,
59 			dpcd_dp_tun_data,
60 			sizeof(dpcd_dp_tun_data));
61 
62 	if (status != DC_OK)
63 		goto err;
64 
65 	link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.raw =
66 			dpcd_dp_tun_data[DP_TUNNELING_CAPABILITIES_SUPPORT - DP_TUNNELING_CAPABILITIES_SUPPORT];
67 
68 	if (link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.bits.dp_tunneling == false)
69 		goto err;
70 
71 	link->dpcd_caps.usb4_dp_tun_info.dpia_info.raw =
72 			dpcd_dp_tun_data[DP_IN_ADAPTER_INFO - DP_TUNNELING_CAPABILITIES_SUPPORT];
73 	link->dpcd_caps.usb4_dp_tun_info.usb4_driver_id =
74 			dpcd_dp_tun_data[DP_USB4_DRIVER_ID - DP_TUNNELING_CAPABILITIES_SUPPORT];
75 
76 	if (link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.bits.dpia_bw_alloc) {
77 		status = core_link_read_dpcd(link, USB4_DRIVER_BW_CAPABILITY,
78 				dpcd_dp_tun_data, 2);
79 
80 		if (status != DC_OK)
81 			goto err;
82 
83 		link->dpcd_caps.usb4_dp_tun_info.driver_bw_cap.raw =
84 			dpcd_dp_tun_data[USB4_DRIVER_BW_CAPABILITY - USB4_DRIVER_BW_CAPABILITY];
85 		link->dpcd_caps.usb4_dp_tun_info.dpia_tunnel_info.raw =
86 			dpcd_dp_tun_data[DP_IN_ADAPTER_TUNNEL_INFO - USB4_DRIVER_BW_CAPABILITY];
87 	}
88 
89 	DC_LOG_DEBUG("%s: Link[%d]  DP tunneling support  (RouterId=%d  AdapterId=%d)  "
90 			"DPIA_BW_Alloc_support=%d "
91 			"CM_BW_Alloc_support=%d ",
92 			__func__, link->link_index,
93 			link->dpcd_caps.usb4_dp_tun_info.usb4_driver_id,
94 			link->dpcd_caps.usb4_dp_tun_info.dpia_info.bits.dpia_num,
95 			link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.bits.dpia_bw_alloc,
96 			link->dpcd_caps.usb4_dp_tun_info.driver_bw_cap.bits.driver_bw_alloc_support);
97 
98 	status = core_link_read_dpcd(
99 			link,
100 			DP_USB4_ROUTER_TOPOLOGY_ID,
101 			dpcd_topology_data,
102 			sizeof(dpcd_topology_data));
103 
104 	if (status != DC_OK)
105 		goto err;
106 
107 	for (i = 0; i < DPCD_USB4_TOPOLOGY_ID_LEN; i++)
108 		link->dpcd_caps.usb4_dp_tun_info.usb4_topology_id[i] = dpcd_topology_data[i];
109 
110 err:
111 	return status;
112 }
113 
dpia_query_hpd_status(struct dc_link * link)114 bool dpia_query_hpd_status(struct dc_link *link)
115 {
116 	union dmub_rb_cmd cmd = {0};
117 	struct dc_dmub_srv *dmub_srv = link->ctx->dmub_srv;
118 
119 	/* prepare QUERY_HPD command */
120 	cmd.query_hpd.header.type = DMUB_CMD__QUERY_HPD_STATE;
121 	cmd.query_hpd.header.payload_bytes = sizeof(cmd.query_hpd.data);
122 	cmd.query_hpd.data.instance = link->link_id.enum_id - ENUM_ID_1;
123 	cmd.query_hpd.data.ch_type = AUX_CHANNEL_DPIA;
124 
125 	/* Query dpia hpd status from dmub */
126 	if (dc_wake_and_execute_dmub_cmd(dmub_srv->ctx, &cmd,
127 		DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY) &&
128 	    cmd.query_hpd.data.status == AUX_RET_SUCCESS) {
129 		DC_LOG_DEBUG("%s: for link(%d) dpia(%d) success, current_hpd_status(%d) new_hpd_status(%d)\n",
130 			__func__,
131 			link->link_index,
132 			link->link_id.enum_id - ENUM_ID_1,
133 			link->hpd_status,
134 			cmd.query_hpd.data.result);
135 		link->hpd_status = cmd.query_hpd.data.result;
136 	} else {
137 		DC_LOG_ERROR("%s: for link(%d) dpia(%d) failed with status(%d), current_hpd_status(%d) new_hpd_status(0)\n",
138 			__func__,
139 			link->link_index,
140 			link->link_id.enum_id - ENUM_ID_1,
141 			cmd.query_hpd.data.status,
142 			link->hpd_status);
143 		link->hpd_status = false;
144 	}
145 
146 	return link->hpd_status;
147 }
148 
link_decide_dp_tunnel_settings(struct dc_stream_state * stream,struct dc_tunnel_settings * dp_tunnel_setting)149 void link_decide_dp_tunnel_settings(struct dc_stream_state *stream,
150 			struct dc_tunnel_settings *dp_tunnel_setting)
151 {
152 	struct dc_link *link = stream->link;
153 
154 	memset(dp_tunnel_setting, 0, sizeof(*dp_tunnel_setting));
155 
156 	if ((stream->signal == SIGNAL_TYPE_DISPLAY_PORT) || (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST)) {
157 		dp_tunnel_setting->should_enable_dp_tunneling =
158 					link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.bits.dp_tunneling;
159 
160 		if (link->dpcd_caps.usb4_dp_tun_info.dp_tun_cap.bits.dpia_bw_alloc
161 				&& link->dpcd_caps.usb4_dp_tun_info.driver_bw_cap.bits.driver_bw_alloc_support) {
162 			dp_tunnel_setting->should_use_dp_bw_allocation = true;
163 			dp_tunnel_setting->cm_id = link->dpcd_caps.usb4_dp_tun_info.usb4_driver_id & 0x0F;
164 			dp_tunnel_setting->group_id = link->dpcd_caps.usb4_dp_tun_info.dpia_tunnel_info.bits.group_id;
165 			dp_tunnel_setting->estimated_bw = link->dpia_bw_alloc_config.estimated_bw;
166 			dp_tunnel_setting->allocated_bw = link->dpia_bw_alloc_config.allocated_bw;
167 			dp_tunnel_setting->bw_granularity = link->dpia_bw_alloc_config.bw_granularity;
168 		}
169 	}
170 }
171 
172