1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #define MAX_NUM_DISPLAYS 24
27 
28 
29 #include "hdcp.h"
30 
31 #include "amdgpu.h"
32 #include "hdcp_psp.h"
33 
hdcp2_message_init(struct mod_hdcp * hdcp,struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 * in)34 static void hdcp2_message_init(struct mod_hdcp *hdcp,
35 			       struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *in)
36 {
37 	in->session_handle = hdcp->auth.id;
38 	in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
39 	in->prepare.msg2_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
40 	in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
41 	in->process.msg1_desc.msg_size = 0;
42 	in->process.msg2_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
43 	in->process.msg2_desc.msg_size = 0;
44 	in->process.msg3_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__NULL_MESSAGE;
45 	in->process.msg3_desc.msg_size = 0;
46 }
47 
remove_display_from_topology_v2(struct mod_hdcp * hdcp,uint8_t index)48 static enum mod_hdcp_status remove_display_from_topology_v2(
49 		struct mod_hdcp *hdcp, uint8_t index)
50 {
51 	struct psp_context *psp = hdcp->config.psp.handle;
52 	struct ta_dtm_shared_memory *dtm_cmd;
53 	struct mod_hdcp_display *display =
54 			get_active_display_at_index(hdcp, index);
55 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
56 
57 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
58 
59 	if (!display || !is_display_active(display))
60 		return MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
61 
62 	mutex_lock(&psp->dtm_context.mutex);
63 
64 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
65 
66 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_UPDATE_V2;
67 	dtm_cmd->dtm_in_message.topology_update_v2.display_handle = display->index;
68 	dtm_cmd->dtm_in_message.topology_update_v2.is_active = 0;
69 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
70 
71 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
72 
73 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
74 		status = MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE;
75 	} else {
76 		display->state = MOD_HDCP_DISPLAY_ACTIVE;
77 		HDCP_TOP_REMOVE_DISPLAY_TRACE(hdcp, display->index);
78 	}
79 
80 	mutex_unlock(&psp->dtm_context.mutex);
81 	return status;
82 }
83 
remove_display_from_topology_v3(struct mod_hdcp * hdcp,uint8_t index)84 static enum mod_hdcp_status remove_display_from_topology_v3(
85 		struct mod_hdcp *hdcp, uint8_t index)
86 {
87 	struct psp_context *psp = hdcp->config.psp.handle;
88 	struct ta_dtm_shared_memory *dtm_cmd;
89 	struct mod_hdcp_display *display =
90 		get_active_display_at_index(hdcp, index);
91 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
92 
93 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
94 
95 	if (!display || !is_display_active(display))
96 		return MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
97 
98 	mutex_lock(&psp->dtm_context.mutex);
99 
100 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
101 
102 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_UPDATE_V3;
103 	dtm_cmd->dtm_in_message.topology_update_v3.display_handle = display->index;
104 	dtm_cmd->dtm_in_message.topology_update_v3.is_active = 0;
105 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
106 
107 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
108 	mutex_unlock(&psp->dtm_context.mutex);
109 
110 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
111 		status = remove_display_from_topology_v2(hdcp, index);
112 		if (status != MOD_HDCP_STATUS_SUCCESS)
113 			display->state = MOD_HDCP_DISPLAY_INACTIVE;
114 	} else {
115 		display->state = MOD_HDCP_DISPLAY_ACTIVE;
116 		HDCP_TOP_REMOVE_DISPLAY_TRACE(hdcp, display->index);
117 	}
118 
119 	return status;
120 }
121 
add_display_to_topology_v2(struct mod_hdcp * hdcp,struct mod_hdcp_display * display)122 static enum mod_hdcp_status add_display_to_topology_v2(
123 		struct mod_hdcp *hdcp, struct mod_hdcp_display *display)
124 {
125 	struct psp_context *psp = hdcp->config.psp.handle;
126 	struct ta_dtm_shared_memory *dtm_cmd;
127 	struct mod_hdcp_link *link = &hdcp->connection.link;
128 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
129 
130 	if (!psp->dtm_context.context.initialized) {
131 		DRM_INFO("Failed to add display topology, DTM TA is not initialized.");
132 		display->state = MOD_HDCP_DISPLAY_INACTIVE;
133 		return MOD_HDCP_STATUS_FAILURE;
134 	}
135 
136 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
137 
138 	mutex_lock(&psp->dtm_context.mutex);
139 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
140 
141 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_UPDATE_V2;
142 	dtm_cmd->dtm_in_message.topology_update_v2.display_handle = display->index;
143 	dtm_cmd->dtm_in_message.topology_update_v2.is_active = 1;
144 	dtm_cmd->dtm_in_message.topology_update_v2.controller = display->controller;
145 	dtm_cmd->dtm_in_message.topology_update_v2.ddc_line = link->ddc_line;
146 	dtm_cmd->dtm_in_message.topology_update_v2.dig_be = link->dig_be;
147 	dtm_cmd->dtm_in_message.topology_update_v2.dig_fe = display->dig_fe;
148 	if (is_dp_hdcp(hdcp))
149 		dtm_cmd->dtm_in_message.topology_update_v2.is_assr = link->dp.assr_enabled;
150 
151 	dtm_cmd->dtm_in_message.topology_update_v2.dp_mst_vcid = display->vc_id;
152 	dtm_cmd->dtm_in_message.topology_update_v2.max_hdcp_supported_version =
153 			TA_DTM_HDCP_VERSION_MAX_SUPPORTED__2_2;
154 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
155 
156 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
157 
158 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
159 		display->state = MOD_HDCP_DISPLAY_INACTIVE;
160 		status = MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE;
161 	} else {
162 		HDCP_TOP_ADD_DISPLAY_TRACE(hdcp, display->index);
163 	}
164 
165 	mutex_unlock(&psp->dtm_context.mutex);
166 	return status;
167 }
168 
add_display_to_topology_v3(struct mod_hdcp * hdcp,struct mod_hdcp_display * display)169 static enum mod_hdcp_status add_display_to_topology_v3(
170 		struct mod_hdcp *hdcp, struct mod_hdcp_display *display)
171 {
172 	struct psp_context *psp = hdcp->config.psp.handle;
173 	struct ta_dtm_shared_memory *dtm_cmd;
174 	struct mod_hdcp_link *link = &hdcp->connection.link;
175 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
176 
177 	if (!psp->dtm_context.context.initialized) {
178 		DRM_INFO("Failed to add display topology, DTM TA is not initialized.");
179 		display->state = MOD_HDCP_DISPLAY_INACTIVE;
180 		return MOD_HDCP_STATUS_FAILURE;
181 	}
182 
183 	dtm_cmd = (struct ta_dtm_shared_memory *)psp->dtm_context.context.mem_context.shared_buf;
184 
185 	mutex_lock(&psp->dtm_context.mutex);
186 	memset(dtm_cmd, 0, sizeof(struct ta_dtm_shared_memory));
187 
188 	dtm_cmd->cmd_id = TA_DTM_COMMAND__TOPOLOGY_UPDATE_V3;
189 	dtm_cmd->dtm_in_message.topology_update_v3.display_handle = display->index;
190 	dtm_cmd->dtm_in_message.topology_update_v3.is_active = 1;
191 	dtm_cmd->dtm_in_message.topology_update_v3.controller = display->controller;
192 	dtm_cmd->dtm_in_message.topology_update_v3.ddc_line = link->ddc_line;
193 	dtm_cmd->dtm_in_message.topology_update_v3.link_enc = link->link_enc_idx;
194 	dtm_cmd->dtm_in_message.topology_update_v3.stream_enc = display->stream_enc_idx;
195 	if (is_dp_hdcp(hdcp))
196 		dtm_cmd->dtm_in_message.topology_update_v3.is_assr = link->dp.assr_enabled;
197 
198 	dtm_cmd->dtm_in_message.topology_update_v3.dp_mst_vcid = display->vc_id;
199 	dtm_cmd->dtm_in_message.topology_update_v3.max_hdcp_supported_version =
200 			TA_DTM_HDCP_VERSION_MAX_SUPPORTED__2_3;
201 	dtm_cmd->dtm_in_message.topology_update_v3.encoder_type = TA_DTM_ENCODER_TYPE__DIG;
202 	dtm_cmd->dtm_status = TA_DTM_STATUS__GENERIC_FAILURE;
203 	dtm_cmd->dtm_in_message.topology_update_v3.phy_id = link->phy_idx;
204 	dtm_cmd->dtm_in_message.topology_update_v3.link_hdcp_cap = link->hdcp_supported_informational;
205 
206 	psp_dtm_invoke(psp, dtm_cmd->cmd_id);
207 	mutex_unlock(&psp->dtm_context.mutex);
208 
209 	if (dtm_cmd->dtm_status != TA_DTM_STATUS__SUCCESS) {
210 		status = add_display_to_topology_v2(hdcp, display);
211 		if (status != MOD_HDCP_STATUS_SUCCESS)
212 			display->state = MOD_HDCP_DISPLAY_INACTIVE;
213 	} else {
214 		HDCP_TOP_ADD_DISPLAY_TRACE(hdcp, display->index);
215 	}
216 
217 	return status;
218 }
219 
mod_hdcp_remove_display_from_topology(struct mod_hdcp * hdcp,uint8_t index)220 enum mod_hdcp_status mod_hdcp_remove_display_from_topology(
221 		struct mod_hdcp *hdcp, uint8_t index)
222 {
223 	enum mod_hdcp_status status = MOD_HDCP_STATUS_UPDATE_TOPOLOGY_FAILURE;
224 
225 	if (hdcp->config.psp.caps.dtm_v3_supported)
226 		status = remove_display_from_topology_v3(hdcp, index);
227 	else
228 		status = remove_display_from_topology_v2(hdcp, index);
229 
230 	return status;
231 }
232 
mod_hdcp_add_display_to_topology(struct mod_hdcp * hdcp,struct mod_hdcp_display * display)233 enum mod_hdcp_status mod_hdcp_add_display_to_topology(struct mod_hdcp *hdcp,
234 					       struct mod_hdcp_display *display)
235 {
236 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
237 
238 	if (hdcp->config.psp.caps.dtm_v3_supported)
239 		status = add_display_to_topology_v3(hdcp, display);
240 	else
241 		status = add_display_to_topology_v2(hdcp, display);
242 
243 	return status;
244 }
245 
mod_hdcp_hdcp1_create_session(struct mod_hdcp * hdcp)246 enum mod_hdcp_status mod_hdcp_hdcp1_create_session(struct mod_hdcp *hdcp)
247 {
248 
249 	struct psp_context *psp = hdcp->config.psp.handle;
250 	struct mod_hdcp_display *display = get_first_active_display(hdcp);
251 	struct ta_hdcp_shared_memory *hdcp_cmd;
252 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
253 
254 	if (!psp->hdcp_context.context.initialized) {
255 		DRM_ERROR("Failed to create hdcp session. HDCP TA is not initialized.");
256 		return MOD_HDCP_STATUS_FAILURE;
257 	}
258 
259 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
260 
261 	mutex_lock(&psp->hdcp_context.mutex);
262 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
263 
264 	hdcp_cmd->in_msg.hdcp1_create_session.display_handle = display->index;
265 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_CREATE_SESSION;
266 
267 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
268 
269 	hdcp->auth.id = hdcp_cmd->out_msg.hdcp1_create_session.session_handle;
270 
271 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
272 		status = MOD_HDCP_STATUS_HDCP1_CREATE_SESSION_FAILURE;
273 	} else {
274 		hdcp->auth.msg.hdcp1.ainfo = hdcp_cmd->out_msg.hdcp1_create_session.ainfo_primary;
275 		memcpy(hdcp->auth.msg.hdcp1.aksv, hdcp_cmd->out_msg.hdcp1_create_session.aksv_primary,
276 		       sizeof(hdcp->auth.msg.hdcp1.aksv));
277 		memcpy(hdcp->auth.msg.hdcp1.an, hdcp_cmd->out_msg.hdcp1_create_session.an_primary,
278 		       sizeof(hdcp->auth.msg.hdcp1.an));
279 	}
280 
281 	mutex_unlock(&psp->hdcp_context.mutex);
282 	return status;
283 }
284 
mod_hdcp_hdcp1_destroy_session(struct mod_hdcp * hdcp)285 enum mod_hdcp_status mod_hdcp_hdcp1_destroy_session(struct mod_hdcp *hdcp)
286 {
287 
288 	struct psp_context *psp = hdcp->config.psp.handle;
289 	struct ta_hdcp_shared_memory *hdcp_cmd;
290 	uint8_t i = 0;
291 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
292 
293 	mutex_lock(&psp->hdcp_context.mutex);
294 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
295 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
296 
297 	hdcp_cmd->in_msg.hdcp1_destroy_session.session_handle = hdcp->auth.id;
298 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_DESTROY_SESSION;
299 
300 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
301 
302 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
303 		status = MOD_HDCP_STATUS_HDCP1_DESTROY_SESSION_FAILURE;
304 	} else {
305 		HDCP_TOP_HDCP1_DESTROY_SESSION_TRACE(hdcp);
306 		for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
307 			if (is_display_encryption_enabled(&hdcp->displays[i])) {
308 				hdcp->displays[i].state =
309 							MOD_HDCP_DISPLAY_ACTIVE;
310 				HDCP_HDCP1_DISABLED_TRACE(
311 					hdcp, hdcp->displays[i].index);
312 			}
313 	}
314 
315 	mutex_unlock(&psp->hdcp_context.mutex);
316 	return status;
317 }
318 
mod_hdcp_hdcp1_validate_rx(struct mod_hdcp * hdcp)319 enum mod_hdcp_status mod_hdcp_hdcp1_validate_rx(struct mod_hdcp *hdcp)
320 {
321 	struct psp_context *psp = hdcp->config.psp.handle;
322 	struct ta_hdcp_shared_memory *hdcp_cmd;
323 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
324 
325 	mutex_lock(&psp->hdcp_context.mutex);
326 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
327 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
328 
329 	hdcp_cmd->in_msg.hdcp1_first_part_authentication.session_handle = hdcp->auth.id;
330 
331 	memcpy(hdcp_cmd->in_msg.hdcp1_first_part_authentication.bksv_primary, hdcp->auth.msg.hdcp1.bksv,
332 		TA_HDCP__HDCP1_KSV_SIZE);
333 
334 	hdcp_cmd->in_msg.hdcp1_first_part_authentication.r0_prime_primary = hdcp->auth.msg.hdcp1.r0p;
335 	hdcp_cmd->in_msg.hdcp1_first_part_authentication.bcaps = hdcp->auth.msg.hdcp1.bcaps;
336 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_FIRST_PART_AUTHENTICATION;
337 
338 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
339 
340 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
341 		status = MOD_HDCP_STATUS_HDCP1_VALIDATE_RX_FAILURE;
342 	} else if (hdcp_cmd->out_msg.hdcp1_first_part_authentication.authentication_status ==
343 	    TA_HDCP_AUTHENTICATION_STATUS__HDCP1_FIRST_PART_COMPLETE) {
344 		/* needs second part of authentication */
345 		hdcp->connection.is_repeater = 1;
346 	} else if (hdcp_cmd->out_msg.hdcp1_first_part_authentication.authentication_status ==
347 		   TA_HDCP_AUTHENTICATION_STATUS__HDCP1_AUTHENTICATED) {
348 		hdcp->connection.is_repeater = 0;
349 	} else if (hdcp_cmd->out_msg.hdcp1_first_part_authentication.authentication_status ==
350 		   TA_HDCP_AUTHENTICATION_STATUS__HDCP1_KSV_REVOKED) {
351 		hdcp->connection.is_hdcp1_revoked = 1;
352 		status = MOD_HDCP_STATUS_HDCP1_BKSV_REVOKED;
353 	} else
354 		status = MOD_HDCP_STATUS_HDCP1_VALIDATE_RX_FAILURE;
355 
356 	mutex_unlock(&psp->hdcp_context.mutex);
357 	return status;
358 }
359 
mod_hdcp_hdcp1_enable_encryption(struct mod_hdcp * hdcp)360 enum mod_hdcp_status mod_hdcp_hdcp1_enable_encryption(struct mod_hdcp *hdcp)
361 {
362 	struct psp_context *psp = hdcp->config.psp.handle;
363 	struct ta_hdcp_shared_memory *hdcp_cmd;
364 	struct mod_hdcp_display *display = get_first_active_display(hdcp);
365 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
366 
367 	mutex_lock(&psp->hdcp_context.mutex);
368 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
369 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
370 
371 	hdcp_cmd->in_msg.hdcp1_enable_encryption.session_handle = hdcp->auth.id;
372 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_ENABLE_ENCRYPTION;
373 
374 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
375 
376 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
377 		status = MOD_HDCP_STATUS_HDCP1_ENABLE_ENCRYPTION_FAILURE;
378 	} else if (!is_dp_mst_hdcp(hdcp)) {
379 		display->state = MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
380 		HDCP_HDCP1_ENABLED_TRACE(hdcp, display->index);
381 	}
382 
383 	mutex_unlock(&psp->hdcp_context.mutex);
384 	return status;
385 }
386 
mod_hdcp_hdcp1_validate_ksvlist_vp(struct mod_hdcp * hdcp)387 enum mod_hdcp_status mod_hdcp_hdcp1_validate_ksvlist_vp(struct mod_hdcp *hdcp)
388 {
389 	struct psp_context *psp = hdcp->config.psp.handle;
390 	struct ta_hdcp_shared_memory *hdcp_cmd;
391 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
392 
393 	mutex_lock(&psp->hdcp_context.mutex);
394 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
395 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
396 
397 	hdcp_cmd->in_msg.hdcp1_second_part_authentication.session_handle = hdcp->auth.id;
398 
399 	hdcp_cmd->in_msg.hdcp1_second_part_authentication.ksv_list_size = hdcp->auth.msg.hdcp1.ksvlist_size;
400 	memcpy(hdcp_cmd->in_msg.hdcp1_second_part_authentication.ksv_list, hdcp->auth.msg.hdcp1.ksvlist,
401 	       hdcp->auth.msg.hdcp1.ksvlist_size);
402 
403 	memcpy(hdcp_cmd->in_msg.hdcp1_second_part_authentication.v_prime, hdcp->auth.msg.hdcp1.vp,
404 	       sizeof(hdcp->auth.msg.hdcp1.vp));
405 
406 	hdcp_cmd->in_msg.hdcp1_second_part_authentication.bstatus_binfo =
407 		is_dp_hdcp(hdcp) ? hdcp->auth.msg.hdcp1.binfo_dp : hdcp->auth.msg.hdcp1.bstatus;
408 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_SECOND_PART_AUTHENTICATION;
409 
410 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
411 
412 	if (hdcp_cmd->hdcp_status == TA_HDCP_STATUS__SUCCESS &&
413 	    hdcp_cmd->out_msg.hdcp1_second_part_authentication.authentication_status ==
414 		    TA_HDCP_AUTHENTICATION_STATUS__HDCP1_AUTHENTICATED) {
415 		status = MOD_HDCP_STATUS_SUCCESS;
416 	} else if (hdcp_cmd->out_msg.hdcp1_second_part_authentication.authentication_status ==
417 		   TA_HDCP_AUTHENTICATION_STATUS__HDCP1_KSV_REVOKED) {
418 		hdcp->connection.is_hdcp1_revoked = 1;
419 		status = MOD_HDCP_STATUS_HDCP1_KSV_LIST_REVOKED;
420 	} else {
421 		status = MOD_HDCP_STATUS_HDCP1_VALIDATE_KSV_LIST_FAILURE;
422 	}
423 
424 	mutex_unlock(&psp->hdcp_context.mutex);
425 	return status;
426 }
427 
mod_hdcp_hdcp1_enable_dp_stream_encryption(struct mod_hdcp * hdcp)428 enum mod_hdcp_status mod_hdcp_hdcp1_enable_dp_stream_encryption(struct mod_hdcp *hdcp)
429 {
430 
431 	struct psp_context *psp = hdcp->config.psp.handle;
432 	struct ta_hdcp_shared_memory *hdcp_cmd;
433 	int i = 0;
434 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
435 
436 	mutex_lock(&psp->hdcp_context.mutex);
437 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
438 
439 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++) {
440 
441 		if (hdcp->displays[i].adjust.disable || hdcp->displays[i].state != MOD_HDCP_DISPLAY_ACTIVE)
442 				continue;
443 
444 		memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
445 
446 		hdcp_cmd->in_msg.hdcp1_enable_dp_stream_encryption.session_handle = hdcp->auth.id;
447 		hdcp_cmd->in_msg.hdcp1_enable_dp_stream_encryption.display_handle = hdcp->displays[i].index;
448 		hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_ENABLE_DP_STREAM_ENCRYPTION;
449 
450 		psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
451 
452 		if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
453 			status = MOD_HDCP_STATUS_HDCP1_ENABLE_STREAM_ENCRYPTION_FAILURE;
454 			break;
455 		}
456 
457 		hdcp->displays[i].state = MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
458 		HDCP_HDCP1_ENABLED_TRACE(hdcp, hdcp->displays[i].index);
459 	}
460 
461 	mutex_unlock(&psp->hdcp_context.mutex);
462 	return status;
463 }
464 
mod_hdcp_hdcp1_link_maintenance(struct mod_hdcp * hdcp)465 enum mod_hdcp_status mod_hdcp_hdcp1_link_maintenance(struct mod_hdcp *hdcp)
466 {
467 	struct psp_context *psp = hdcp->config.psp.handle;
468 	struct ta_hdcp_shared_memory *hdcp_cmd;
469 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
470 
471 	mutex_lock(&psp->hdcp_context.mutex);
472 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
473 
474 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
475 
476 	hdcp_cmd->in_msg.hdcp1_get_encryption_status.session_handle = hdcp->auth.id;
477 
478 	hdcp_cmd->out_msg.hdcp1_get_encryption_status.protection_level = 0;
479 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP1_GET_ENCRYPTION_STATUS;
480 
481 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
482 
483 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS ||
484 			hdcp_cmd->out_msg.hdcp1_get_encryption_status.protection_level != 1)
485 		status = MOD_HDCP_STATUS_HDCP1_LINK_MAINTENANCE_FAILURE;
486 
487 	mutex_unlock(&psp->hdcp_context.mutex);
488 	return status;
489 }
490 
mod_hdcp_hdcp2_create_session(struct mod_hdcp * hdcp)491 enum mod_hdcp_status mod_hdcp_hdcp2_create_session(struct mod_hdcp *hdcp)
492 {
493 	struct psp_context *psp = hdcp->config.psp.handle;
494 	struct ta_hdcp_shared_memory *hdcp_cmd;
495 	struct mod_hdcp_display *display = get_first_active_display(hdcp);
496 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
497 
498 
499 	if (!psp->hdcp_context.context.initialized) {
500 		DRM_ERROR("Failed to create hdcp session, HDCP TA is not initialized");
501 		return MOD_HDCP_STATUS_FAILURE;
502 	}
503 
504 	if (!display)
505 		return MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
506 
507 	mutex_lock(&psp->hdcp_context.mutex);
508 
509 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
510 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
511 
512 	hdcp_cmd->in_msg.hdcp2_create_session_v2.display_handle = display->index;
513 
514 	if (hdcp->connection.link.adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_0)
515 		hdcp_cmd->in_msg.hdcp2_create_session_v2.negotiate_content_type =
516 			TA_HDCP2_CONTENT_TYPE_NEGOTIATION_TYPE__FORCE_TYPE0;
517 	else if (hdcp->connection.link.adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_1)
518 		hdcp_cmd->in_msg.hdcp2_create_session_v2.negotiate_content_type =
519 			TA_HDCP2_CONTENT_TYPE_NEGOTIATION_TYPE__FORCE_TYPE1;
520 	else if (hdcp->connection.link.adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_MAX)
521 		hdcp_cmd->in_msg.hdcp2_create_session_v2.negotiate_content_type =
522 			TA_HDCP2_CONTENT_TYPE_NEGOTIATION_TYPE__MAX_SUPPORTED;
523 
524 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_CREATE_SESSION_V2;
525 
526 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
527 
528 
529 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
530 		status = MOD_HDCP_STATUS_HDCP2_CREATE_SESSION_FAILURE;
531 	else
532 		hdcp->auth.id = hdcp_cmd->out_msg.hdcp2_create_session_v2.session_handle;
533 
534 	mutex_unlock(&psp->hdcp_context.mutex);
535 	return status;
536 }
537 
mod_hdcp_hdcp2_destroy_session(struct mod_hdcp * hdcp)538 enum mod_hdcp_status mod_hdcp_hdcp2_destroy_session(struct mod_hdcp *hdcp)
539 {
540 	struct psp_context *psp = hdcp->config.psp.handle;
541 	struct ta_hdcp_shared_memory *hdcp_cmd;
542 	uint8_t i = 0;
543 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
544 
545 	mutex_lock(&psp->hdcp_context.mutex);
546 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
547 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
548 
549 	hdcp_cmd->in_msg.hdcp2_destroy_session.session_handle = hdcp->auth.id;
550 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_DESTROY_SESSION;
551 
552 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
553 
554 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
555 		status = MOD_HDCP_STATUS_HDCP2_DESTROY_SESSION_FAILURE;
556 	} else {
557 		HDCP_TOP_HDCP2_DESTROY_SESSION_TRACE(hdcp);
558 		for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++)
559 			if (is_display_encryption_enabled(&hdcp->displays[i])) {
560 				hdcp->displays[i].state =
561 							MOD_HDCP_DISPLAY_ACTIVE;
562 				HDCP_HDCP2_DISABLED_TRACE(
563 					hdcp, hdcp->displays[i].index);
564 			}
565 	}
566 
567 	mutex_unlock(&psp->hdcp_context.mutex);
568 	return status;
569 }
570 
mod_hdcp_hdcp2_prepare_ake_init(struct mod_hdcp * hdcp)571 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_ake_init(struct mod_hdcp *hdcp)
572 {
573 	struct psp_context *psp = hdcp->config.psp.handle;
574 	struct ta_hdcp_shared_memory *hdcp_cmd;
575 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
576 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
577 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
578 
579 	mutex_lock(&psp->hdcp_context.mutex);
580 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
581 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
582 
583 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
584 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
585 
586 	hdcp2_message_init(hdcp, msg_in);
587 
588 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
589 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__AKE_INIT;
590 
591 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
592 
593 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
594 		status = MOD_HDCP_STATUS_HDCP2_PREP_AKE_INIT_FAILURE;
595 	else
596 		memcpy(&hdcp->auth.msg.hdcp2.ake_init[0], &msg_out->prepare.transmitter_message[0],
597 		       sizeof(hdcp->auth.msg.hdcp2.ake_init));
598 
599 	mutex_unlock(&psp->hdcp_context.mutex);
600 	return status;
601 }
602 
mod_hdcp_hdcp2_validate_ake_cert(struct mod_hdcp * hdcp)603 enum mod_hdcp_status mod_hdcp_hdcp2_validate_ake_cert(struct mod_hdcp *hdcp)
604 {
605 	struct psp_context *psp = hdcp->config.psp.handle;
606 	struct ta_hdcp_shared_memory *hdcp_cmd;
607 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
608 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
609 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
610 
611 	mutex_lock(&psp->hdcp_context.mutex);
612 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
613 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
614 
615 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
616 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
617 
618 	hdcp2_message_init(hdcp, msg_in);
619 
620 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__AKE_SEND_CERT;
621 	msg_in->process.msg1_desc.msg_size = TA_HDCP_HDCP2_MSG_ID_MAX_SIZE__AKE_SEND_CERT;
622 
623 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.ake_cert,
624 	       sizeof(hdcp->auth.msg.hdcp2.ake_cert));
625 
626 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__AKE_NO_STORED_KM;
627 	msg_in->prepare.msg2_id = TA_HDCP_HDCP2_MSG_ID__AKE_STORED_KM;
628 
629 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
630 
631 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
632 
633 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
634 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_AKE_CERT_FAILURE;
635 	} else {
636 		memcpy(hdcp->auth.msg.hdcp2.ake_no_stored_km,
637 		       &msg_out->prepare.transmitter_message[0],
638 		       sizeof(hdcp->auth.msg.hdcp2.ake_no_stored_km));
639 
640 		memcpy(hdcp->auth.msg.hdcp2.ake_stored_km,
641 		       &msg_out->prepare.transmitter_message[sizeof(hdcp->auth.msg.hdcp2.ake_no_stored_km)],
642 		       sizeof(hdcp->auth.msg.hdcp2.ake_stored_km));
643 
644 		if (msg_out->process.msg1_status ==
645 		    TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS) {
646 			hdcp->connection.is_km_stored =
647 				msg_out->process.is_km_stored ? 1 : 0;
648 			hdcp->connection.is_repeater =
649 				msg_out->process.is_repeater ? 1 : 0;
650 			status = MOD_HDCP_STATUS_SUCCESS;
651 		} else if (msg_out->process.msg1_status ==
652 			   TA_HDCP2_MSG_AUTHENTICATION_STATUS__RECEIVERID_REVOKED) {
653 			hdcp->connection.is_hdcp2_revoked = 1;
654 			status = MOD_HDCP_STATUS_HDCP2_AKE_CERT_REVOKED;
655 		}  else {
656 			status = MOD_HDCP_STATUS_HDCP2_VALIDATE_AKE_CERT_FAILURE;
657 		}
658 	}
659 	mutex_unlock(&psp->hdcp_context.mutex);
660 	return status;
661 }
662 
mod_hdcp_hdcp2_validate_h_prime(struct mod_hdcp * hdcp)663 enum mod_hdcp_status mod_hdcp_hdcp2_validate_h_prime(struct mod_hdcp *hdcp)
664 {
665 	struct psp_context *psp = hdcp->config.psp.handle;
666 	struct ta_hdcp_shared_memory *hdcp_cmd;
667 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
668 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
669 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
670 
671 	mutex_lock(&psp->hdcp_context.mutex);
672 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
673 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
674 
675 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
676 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
677 
678 	hdcp2_message_init(hdcp, msg_in);
679 
680 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__AKE_SEND_H_PRIME;
681 	msg_in->process.msg1_desc.msg_size = TA_HDCP_HDCP2_MSG_ID_MAX_SIZE__AKE_SEND_H_PRIME;
682 
683 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.ake_h_prime,
684 	       sizeof(hdcp->auth.msg.hdcp2.ake_h_prime));
685 
686 	if (!hdcp->connection.is_km_stored) {
687 		msg_in->process.msg2_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__AKE_SEND_PAIRING_INFO;
688 		msg_in->process.msg2_desc.msg_size = TA_HDCP_HDCP2_MSG_ID_MAX_SIZE__AKE_SEND_PAIRING_INFO;
689 		memcpy(&msg_in->process.receiver_message[sizeof(hdcp->auth.msg.hdcp2.ake_h_prime)],
690 		       hdcp->auth.msg.hdcp2.ake_pairing_info, sizeof(hdcp->auth.msg.hdcp2.ake_pairing_info));
691 	}
692 
693 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
694 
695 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
696 
697 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
698 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_H_PRIME_FAILURE;
699 	else if (msg_out->process.msg1_status != TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS)
700 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_H_PRIME_FAILURE;
701 	else if (!hdcp->connection.is_km_stored &&
702 		   msg_out->process.msg2_status != TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS)
703 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_PAIRING_INFO_FAILURE;
704 
705 	mutex_unlock(&psp->hdcp_context.mutex);
706 	return status;
707 }
708 
mod_hdcp_hdcp2_prepare_lc_init(struct mod_hdcp * hdcp)709 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_lc_init(struct mod_hdcp *hdcp)
710 {
711 	struct psp_context *psp = hdcp->config.psp.handle;
712 	struct ta_hdcp_shared_memory *hdcp_cmd;
713 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
714 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
715 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
716 
717 	mutex_lock(&psp->hdcp_context.mutex);
718 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
719 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
720 
721 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
722 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
723 
724 	hdcp2_message_init(hdcp, msg_in);
725 
726 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__LC_INIT;
727 
728 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
729 
730 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
731 
732 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
733 		status = MOD_HDCP_STATUS_HDCP2_PREP_LC_INIT_FAILURE;
734 	else
735 		memcpy(hdcp->auth.msg.hdcp2.lc_init, &msg_out->prepare.transmitter_message[0],
736 		       sizeof(hdcp->auth.msg.hdcp2.lc_init));
737 
738 	mutex_unlock(&psp->hdcp_context.mutex);
739 	return status;
740 }
741 
mod_hdcp_hdcp2_validate_l_prime(struct mod_hdcp * hdcp)742 enum mod_hdcp_status mod_hdcp_hdcp2_validate_l_prime(struct mod_hdcp *hdcp)
743 {
744 	struct psp_context *psp = hdcp->config.psp.handle;
745 	struct ta_hdcp_shared_memory *hdcp_cmd;
746 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
747 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
748 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
749 
750 	mutex_lock(&psp->hdcp_context.mutex);
751 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
752 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
753 
754 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
755 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
756 
757 	hdcp2_message_init(hdcp, msg_in);
758 
759 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__LC_SEND_L_PRIME;
760 	msg_in->process.msg1_desc.msg_size = TA_HDCP_HDCP2_MSG_ID_MAX_SIZE__LC_SEND_L_PRIME;
761 
762 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.lc_l_prime,
763 	       sizeof(hdcp->auth.msg.hdcp2.lc_l_prime));
764 
765 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
766 
767 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
768 
769 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS ||
770 			msg_out->process.msg1_status != TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS)
771 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_L_PRIME_FAILURE;
772 
773 	mutex_unlock(&psp->hdcp_context.mutex);
774 	return status;
775 }
776 
mod_hdcp_hdcp2_prepare_eks(struct mod_hdcp * hdcp)777 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_eks(struct mod_hdcp *hdcp)
778 {
779 	struct psp_context *psp = hdcp->config.psp.handle;
780 	struct ta_hdcp_shared_memory *hdcp_cmd;
781 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
782 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
783 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
784 
785 	mutex_lock(&psp->hdcp_context.mutex);
786 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
787 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
788 
789 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
790 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
791 
792 	hdcp2_message_init(hdcp, msg_in);
793 
794 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__SKE_SEND_EKS;
795 
796 	if (is_dp_hdcp(hdcp))
797 		msg_in->prepare.msg2_id = TA_HDCP_HDCP2_MSG_ID__SIGNAL_CONTENT_STREAM_TYPE_DP;
798 
799 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
800 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
801 
802 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
803 		status = MOD_HDCP_STATUS_HDCP2_PREP_EKS_FAILURE;
804 	} else {
805 		memcpy(hdcp->auth.msg.hdcp2.ske_eks,
806 		       &msg_out->prepare.transmitter_message[0],
807 		       sizeof(hdcp->auth.msg.hdcp2.ske_eks));
808 		msg_out->prepare.msg1_desc.msg_size =
809 			sizeof(hdcp->auth.msg.hdcp2.ske_eks);
810 
811 		if (is_dp_hdcp(hdcp)) {
812 			memcpy(hdcp->auth.msg.hdcp2.content_stream_type_dp,
813 			       &msg_out->prepare.transmitter_message[sizeof(hdcp->auth.msg.hdcp2.ske_eks)],
814 			       sizeof(hdcp->auth.msg.hdcp2.content_stream_type_dp));
815 		}
816 	}
817 	mutex_unlock(&psp->hdcp_context.mutex);
818 
819 	return status;
820 }
821 
mod_hdcp_hdcp2_enable_encryption(struct mod_hdcp * hdcp)822 enum mod_hdcp_status mod_hdcp_hdcp2_enable_encryption(struct mod_hdcp *hdcp)
823 {
824 	struct psp_context *psp = hdcp->config.psp.handle;
825 	struct ta_hdcp_shared_memory *hdcp_cmd;
826 	struct mod_hdcp_display *display = get_first_active_display(hdcp);
827 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
828 
829 	if (!display)
830 		return MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
831 
832 	mutex_lock(&psp->hdcp_context.mutex);
833 
834 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
835 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
836 
837 	hdcp_cmd->in_msg.hdcp2_set_encryption.session_handle = hdcp->auth.id;
838 
839 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_SET_ENCRYPTION;
840 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
841 
842 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
843 		status = MOD_HDCP_STATUS_HDCP2_ENABLE_ENCRYPTION_FAILURE;
844 	} else if (!is_dp_mst_hdcp(hdcp)) {
845 		display->state = MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
846 		HDCP_HDCP2_ENABLED_TRACE(hdcp, display->index);
847 	}
848 
849 	mutex_unlock(&psp->hdcp_context.mutex);
850 	return status;
851 }
852 
mod_hdcp_hdcp2_validate_rx_id_list(struct mod_hdcp * hdcp)853 enum mod_hdcp_status mod_hdcp_hdcp2_validate_rx_id_list(struct mod_hdcp *hdcp)
854 {
855 	struct psp_context *psp = hdcp->config.psp.handle;
856 	struct ta_hdcp_shared_memory *hdcp_cmd;
857 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
858 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
859 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
860 
861 	mutex_lock(&psp->hdcp_context.mutex);
862 
863 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
864 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
865 
866 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
867 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
868 
869 	hdcp2_message_init(hdcp, msg_in);
870 
871 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__REPEATERAUTH_SEND_RECEIVERID_LIST;
872 	msg_in->process.msg1_desc.msg_size = sizeof(hdcp->auth.msg.hdcp2.rx_id_list);
873 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.rx_id_list,
874 	       sizeof(hdcp->auth.msg.hdcp2.rx_id_list));
875 
876 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__REPEATERAUTH_SEND_ACK;
877 
878 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
879 
880 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
881 
882 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
883 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_RX_ID_LIST_FAILURE;
884 	} else {
885 		memcpy(hdcp->auth.msg.hdcp2.repeater_auth_ack,
886 		       &msg_out->prepare.transmitter_message[0],
887 		       sizeof(hdcp->auth.msg.hdcp2.repeater_auth_ack));
888 
889 		if (msg_out->process.msg1_status ==
890 		    TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS) {
891 			hdcp->connection.is_km_stored = msg_out->process.is_km_stored ? 1 : 0;
892 			hdcp->connection.is_repeater = msg_out->process.is_repeater ? 1 : 0;
893 			status = MOD_HDCP_STATUS_SUCCESS;
894 		} else if (msg_out->process.msg1_status ==
895 			   TA_HDCP2_MSG_AUTHENTICATION_STATUS__RECEIVERID_REVOKED) {
896 			hdcp->connection.is_hdcp2_revoked = 1;
897 			status = MOD_HDCP_STATUS_HDCP2_RX_ID_LIST_REVOKED;
898 		} else {
899 			status = MOD_HDCP_STATUS_HDCP2_VALIDATE_RX_ID_LIST_FAILURE;
900 		}
901 	}
902 	mutex_unlock(&psp->hdcp_context.mutex);
903 	return status;
904 }
905 
mod_hdcp_hdcp2_enable_dp_stream_encryption(struct mod_hdcp * hdcp)906 enum mod_hdcp_status mod_hdcp_hdcp2_enable_dp_stream_encryption(struct mod_hdcp *hdcp)
907 {
908 	struct psp_context *psp = hdcp->config.psp.handle;
909 	struct ta_hdcp_shared_memory *hdcp_cmd;
910 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
911 	uint8_t i;
912 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
913 
914 	mutex_lock(&psp->hdcp_context.mutex);
915 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
916 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
917 
918 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
919 
920 	hdcp2_message_init(hdcp, msg_in);
921 
922 
923 	for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++) {
924 		if (hdcp->displays[i].adjust.disable || hdcp->displays[i].state != MOD_HDCP_DISPLAY_ACTIVE)
925 				continue;
926 
927 		hdcp_cmd->in_msg.hdcp2_enable_dp_stream_encryption.display_handle = hdcp->displays[i].index;
928 		hdcp_cmd->in_msg.hdcp2_enable_dp_stream_encryption.session_handle = hdcp->auth.id;
929 
930 		hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_ENABLE_DP_STREAM_ENCRYPTION;
931 		psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
932 
933 		if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS)
934 			break;
935 
936 		hdcp->displays[i].state = MOD_HDCP_DISPLAY_ENCRYPTION_ENABLED;
937 		HDCP_HDCP2_ENABLED_TRACE(hdcp, hdcp->displays[i].index);
938 	}
939 
940 	if (hdcp_cmd->hdcp_status == TA_HDCP_STATUS__SUCCESS)
941 		status = MOD_HDCP_STATUS_SUCCESS;
942 	else
943 		status = MOD_HDCP_STATUS_HDCP2_ENABLE_STREAM_ENCRYPTION_FAILURE;
944 
945 	mutex_unlock(&psp->hdcp_context.mutex);
946 	return status;
947 }
948 
mod_hdcp_hdcp2_prepare_stream_management(struct mod_hdcp * hdcp)949 enum mod_hdcp_status mod_hdcp_hdcp2_prepare_stream_management(struct mod_hdcp *hdcp)
950 {
951 
952 	struct psp_context *psp = hdcp->config.psp.handle;
953 	struct ta_hdcp_shared_memory *hdcp_cmd;
954 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
955 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
956 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
957 
958 	mutex_lock(&psp->hdcp_context.mutex);
959 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
960 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
961 
962 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
963 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
964 
965 	hdcp2_message_init(hdcp, msg_in);
966 
967 	msg_in->prepare.msg1_id = TA_HDCP_HDCP2_MSG_ID__REPEATERAUTH_STREAM_MANAGE;
968 
969 
970 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
971 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
972 
973 	if (hdcp_cmd->hdcp_status != TA_HDCP_STATUS__SUCCESS) {
974 		status = MOD_HDCP_STATUS_HDCP2_PREPARE_STREAM_MANAGEMENT_FAILURE;
975 	} else {
976 		hdcp->auth.msg.hdcp2.stream_manage_size = msg_out->prepare.msg1_desc.msg_size;
977 
978 		memcpy(hdcp->auth.msg.hdcp2.repeater_auth_stream_manage,
979 		       &msg_out->prepare.transmitter_message[0],
980 		       sizeof(hdcp->auth.msg.hdcp2.repeater_auth_stream_manage));
981 	}
982 	mutex_unlock(&psp->hdcp_context.mutex);
983 	return status;
984 }
985 
mod_hdcp_hdcp2_validate_stream_ready(struct mod_hdcp * hdcp)986 enum mod_hdcp_status mod_hdcp_hdcp2_validate_stream_ready(struct mod_hdcp *hdcp)
987 {
988 	struct psp_context *psp = hdcp->config.psp.handle;
989 	struct ta_hdcp_shared_memory *hdcp_cmd;
990 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_input_v2 *msg_in;
991 	struct ta_hdcp_cmd_hdcp2_process_prepare_authentication_message_output_v2 *msg_out;
992 	enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
993 
994 	mutex_lock(&psp->hdcp_context.mutex);
995 	hdcp_cmd = (struct ta_hdcp_shared_memory *)psp->hdcp_context.context.mem_context.shared_buf;
996 	memset(hdcp_cmd, 0, sizeof(struct ta_hdcp_shared_memory));
997 
998 	msg_in = &hdcp_cmd->in_msg.hdcp2_prepare_process_authentication_message_v2;
999 	msg_out = &hdcp_cmd->out_msg.hdcp2_prepare_process_authentication_message_v2;
1000 
1001 	hdcp2_message_init(hdcp, msg_in);
1002 
1003 	msg_in->process.msg1_desc.msg_id = TA_HDCP_HDCP2_MSG_ID__REPEATERAUTH_STREAM_READY;
1004 
1005 	msg_in->process.msg1_desc.msg_size = sizeof(hdcp->auth.msg.hdcp2.repeater_auth_stream_ready);
1006 
1007 	memcpy(&msg_in->process.receiver_message[0], hdcp->auth.msg.hdcp2.repeater_auth_stream_ready,
1008 	       sizeof(hdcp->auth.msg.hdcp2.repeater_auth_stream_ready));
1009 
1010 	hdcp_cmd->cmd_id = TA_HDCP_COMMAND__HDCP2_PREPARE_PROCESS_AUTHENTICATION_MSG_V2;
1011 	psp_hdcp_invoke(psp, hdcp_cmd->cmd_id);
1012 
1013 	if (hdcp_cmd->hdcp_status == TA_HDCP_STATUS__SUCCESS &&
1014 	    msg_out->process.msg1_status == TA_HDCP2_MSG_AUTHENTICATION_STATUS__SUCCESS)
1015 		status = MOD_HDCP_STATUS_SUCCESS;
1016 	else
1017 		status = MOD_HDCP_STATUS_HDCP2_VALIDATE_STREAM_READY_FAILURE;
1018 
1019 	mutex_unlock(&psp->hdcp_context.mutex);
1020 	return status;
1021 }
1022