1 /*
2 * Copyright 2019 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 #include "hdcp.h"
27
push_error_status(struct mod_hdcp * hdcp,enum mod_hdcp_status status)28 static void push_error_status(struct mod_hdcp *hdcp,
29 enum mod_hdcp_status status)
30 {
31 struct mod_hdcp_trace *trace = &hdcp->connection.trace;
32
33 if (trace->error_count < MAX_NUM_OF_ERROR_TRACE) {
34 trace->errors[trace->error_count].status = status;
35 trace->errors[trace->error_count].state_id = hdcp->state.id;
36 trace->error_count++;
37 HDCP_ERROR_TRACE(hdcp, status);
38 }
39
40 if (is_hdcp1(hdcp)) {
41 hdcp->connection.hdcp1_retry_count++;
42 } else if (is_hdcp2(hdcp)) {
43 hdcp->connection.hdcp2_retry_count++;
44 }
45 }
46
is_cp_desired_hdcp1(struct mod_hdcp * hdcp)47 static uint8_t is_cp_desired_hdcp1(struct mod_hdcp *hdcp)
48 {
49 int i, is_auth_needed = 0;
50
51 /* if all displays on the link don't need authentication,
52 * hdcp is not desired
53 */
54 for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++) {
55 if (hdcp->displays[i].state != MOD_HDCP_DISPLAY_INACTIVE &&
56 !hdcp->displays[i].adjust.disable) {
57 is_auth_needed = 1;
58 break;
59 }
60 }
61
62 return (hdcp->connection.hdcp1_retry_count < MAX_NUM_OF_ATTEMPTS) &&
63 is_auth_needed &&
64 !hdcp->connection.link.adjust.hdcp1.disable &&
65 !hdcp->connection.is_hdcp1_revoked;
66 }
67
is_cp_desired_hdcp2(struct mod_hdcp * hdcp)68 static uint8_t is_cp_desired_hdcp2(struct mod_hdcp *hdcp)
69 {
70 int i, is_auth_needed = 0;
71
72 /* if all displays on the link don't need authentication,
73 * hdcp is not desired
74 */
75 for (i = 0; i < MAX_NUM_OF_DISPLAYS; i++) {
76 if (hdcp->displays[i].state != MOD_HDCP_DISPLAY_INACTIVE &&
77 !hdcp->displays[i].adjust.disable) {
78 is_auth_needed = 1;
79 break;
80 }
81 }
82
83 return (hdcp->connection.hdcp2_retry_count < MAX_NUM_OF_ATTEMPTS) &&
84 is_auth_needed &&
85 !hdcp->connection.link.adjust.hdcp2.disable &&
86 !hdcp->connection.is_hdcp2_revoked;
87 }
88
execution(struct mod_hdcp * hdcp,struct mod_hdcp_event_context * event_ctx,union mod_hdcp_transition_input * input)89 static enum mod_hdcp_status execution(struct mod_hdcp *hdcp,
90 struct mod_hdcp_event_context *event_ctx,
91 union mod_hdcp_transition_input *input)
92 {
93 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
94
95 if (is_in_initialized_state(hdcp)) {
96 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
97 event_ctx->unexpected_event = 1;
98 goto out;
99 }
100 /* initialize transition input */
101 memset(input, 0, sizeof(union mod_hdcp_transition_input));
102 } else if (is_in_cp_not_desired_state(hdcp)) {
103 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) {
104 event_ctx->unexpected_event = 1;
105 goto out;
106 }
107 } else if (is_in_hdcp1_states(hdcp)) {
108 status = mod_hdcp_hdcp1_execution(hdcp, event_ctx, &input->hdcp1);
109 } else if (is_in_hdcp1_dp_states(hdcp)) {
110 status = mod_hdcp_hdcp1_dp_execution(hdcp,
111 event_ctx, &input->hdcp1);
112 } else if (is_in_hdcp2_states(hdcp)) {
113 status = mod_hdcp_hdcp2_execution(hdcp, event_ctx, &input->hdcp2);
114 } else if (is_in_hdcp2_dp_states(hdcp)) {
115 status = mod_hdcp_hdcp2_dp_execution(hdcp,
116 event_ctx, &input->hdcp2);
117 } else {
118 event_ctx->unexpected_event = 1;
119 goto out;
120 }
121 out:
122 return status;
123 }
124
transition(struct mod_hdcp * hdcp,struct mod_hdcp_event_context * event_ctx,union mod_hdcp_transition_input * input,struct mod_hdcp_output * output)125 static enum mod_hdcp_status transition(struct mod_hdcp *hdcp,
126 struct mod_hdcp_event_context *event_ctx,
127 union mod_hdcp_transition_input *input,
128 struct mod_hdcp_output *output)
129 {
130 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
131
132 if (event_ctx->unexpected_event)
133 goto out;
134
135 if (is_in_initialized_state(hdcp)) {
136 if (is_dp_hdcp(hdcp))
137 if (is_cp_desired_hdcp2(hdcp)) {
138 callback_in_ms(0, output);
139 set_state_id(hdcp, output, D2_A0_DETERMINE_RX_HDCP_CAPABLE);
140 } else if (is_cp_desired_hdcp1(hdcp)) {
141 callback_in_ms(0, output);
142 set_state_id(hdcp, output, D1_A0_DETERMINE_RX_HDCP_CAPABLE);
143 } else {
144 callback_in_ms(0, output);
145 set_state_id(hdcp, output, HDCP_CP_NOT_DESIRED);
146 }
147 else if (is_hdmi_dvi_sl_hdcp(hdcp))
148 if (is_cp_desired_hdcp2(hdcp)) {
149 callback_in_ms(0, output);
150 set_state_id(hdcp, output, H2_A0_KNOWN_HDCP2_CAPABLE_RX);
151 } else if (is_cp_desired_hdcp1(hdcp)) {
152 callback_in_ms(0, output);
153 set_state_id(hdcp, output, H1_A0_WAIT_FOR_ACTIVE_RX);
154 } else {
155 callback_in_ms(0, output);
156 set_state_id(hdcp, output, HDCP_CP_NOT_DESIRED);
157 }
158 else {
159 callback_in_ms(0, output);
160 set_state_id(hdcp, output, HDCP_CP_NOT_DESIRED);
161 }
162 } else if (is_in_cp_not_desired_state(hdcp)) {
163 increment_stay_counter(hdcp);
164 } else if (is_in_hdcp1_states(hdcp)) {
165 status = mod_hdcp_hdcp1_transition(hdcp,
166 event_ctx, &input->hdcp1, output);
167 } else if (is_in_hdcp1_dp_states(hdcp)) {
168 status = mod_hdcp_hdcp1_dp_transition(hdcp,
169 event_ctx, &input->hdcp1, output);
170 } else if (is_in_hdcp2_states(hdcp)) {
171 status = mod_hdcp_hdcp2_transition(hdcp,
172 event_ctx, &input->hdcp2, output);
173 } else if (is_in_hdcp2_dp_states(hdcp)) {
174 status = mod_hdcp_hdcp2_dp_transition(hdcp,
175 event_ctx, &input->hdcp2, output);
176 } else {
177 status = MOD_HDCP_STATUS_INVALID_STATE;
178 }
179 out:
180 return status;
181 }
182
reset_authentication(struct mod_hdcp * hdcp,struct mod_hdcp_output * output)183 static enum mod_hdcp_status reset_authentication(struct mod_hdcp *hdcp,
184 struct mod_hdcp_output *output)
185 {
186 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
187
188 if (is_hdcp1(hdcp)) {
189 if (hdcp->auth.trans_input.hdcp1.create_session != UNKNOWN) {
190 /* TODO - update psp to unify create session failure
191 * recovery between hdcp1 and 2.
192 */
193 mod_hdcp_hdcp1_destroy_session(hdcp);
194
195 }
196
197 HDCP_TOP_RESET_AUTH_TRACE(hdcp);
198 memset(&hdcp->auth, 0, sizeof(struct mod_hdcp_authentication));
199 memset(&hdcp->state, 0, sizeof(struct mod_hdcp_state));
200 set_state_id(hdcp, output, HDCP_INITIALIZED);
201 } else if (is_hdcp2(hdcp)) {
202 if (hdcp->auth.trans_input.hdcp2.create_session == PASS) {
203 status = mod_hdcp_hdcp2_destroy_session(hdcp);
204 if (status != MOD_HDCP_STATUS_SUCCESS) {
205 output->callback_needed = 0;
206 output->watchdog_timer_needed = 0;
207 goto out;
208 }
209 }
210
211 HDCP_TOP_RESET_AUTH_TRACE(hdcp);
212 memset(&hdcp->auth, 0, sizeof(struct mod_hdcp_authentication));
213 memset(&hdcp->state, 0, sizeof(struct mod_hdcp_state));
214 set_state_id(hdcp, output, HDCP_INITIALIZED);
215 } else if (is_in_cp_not_desired_state(hdcp)) {
216 HDCP_TOP_RESET_AUTH_TRACE(hdcp);
217 memset(&hdcp->auth, 0, sizeof(struct mod_hdcp_authentication));
218 memset(&hdcp->state, 0, sizeof(struct mod_hdcp_state));
219 set_state_id(hdcp, output, HDCP_INITIALIZED);
220 }
221
222 out:
223 /* stop callback and watchdog requests from previous authentication*/
224 output->watchdog_timer_stop = 1;
225 output->callback_stop = 1;
226 return status;
227 }
228
reset_connection(struct mod_hdcp * hdcp,struct mod_hdcp_output * output)229 static enum mod_hdcp_status reset_connection(struct mod_hdcp *hdcp,
230 struct mod_hdcp_output *output)
231 {
232 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
233
234 memset(output, 0, sizeof(struct mod_hdcp_output));
235
236 status = reset_authentication(hdcp, output);
237 if (status != MOD_HDCP_STATUS_SUCCESS)
238 goto out;
239
240 if (current_state(hdcp) != HDCP_UNINITIALIZED) {
241 HDCP_TOP_RESET_CONN_TRACE(hdcp);
242 set_state_id(hdcp, output, HDCP_UNINITIALIZED);
243 }
244 memset(&hdcp->connection, 0, sizeof(hdcp->connection));
245 out:
246 return status;
247 }
248
249 /*
250 * Implementation of functions in mod_hdcp.h
251 */
mod_hdcp_get_memory_size(void)252 size_t mod_hdcp_get_memory_size(void)
253 {
254 return sizeof(struct mod_hdcp);
255 }
256
mod_hdcp_setup(struct mod_hdcp * hdcp,struct mod_hdcp_config * config)257 enum mod_hdcp_status mod_hdcp_setup(struct mod_hdcp *hdcp,
258 struct mod_hdcp_config *config)
259 {
260 struct mod_hdcp_output output;
261 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
262
263 memset(&output, 0, sizeof(output));
264 hdcp->config = *config;
265 HDCP_TOP_INTERFACE_TRACE(hdcp);
266 status = reset_connection(hdcp, &output);
267 if (status != MOD_HDCP_STATUS_SUCCESS)
268 push_error_status(hdcp, status);
269 return status;
270 }
271
mod_hdcp_teardown(struct mod_hdcp * hdcp)272 enum mod_hdcp_status mod_hdcp_teardown(struct mod_hdcp *hdcp)
273 {
274 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
275 struct mod_hdcp_output output;
276
277 HDCP_TOP_INTERFACE_TRACE(hdcp);
278 memset(&output, 0, sizeof(output));
279 status = reset_connection(hdcp, &output);
280 if (status == MOD_HDCP_STATUS_SUCCESS)
281 memset(hdcp, 0, sizeof(struct mod_hdcp));
282 else
283 push_error_status(hdcp, status);
284 return status;
285 }
286
mod_hdcp_add_display(struct mod_hdcp * hdcp,struct mod_hdcp_link * link,struct mod_hdcp_display * display,struct mod_hdcp_output * output)287 enum mod_hdcp_status mod_hdcp_add_display(struct mod_hdcp *hdcp,
288 struct mod_hdcp_link *link, struct mod_hdcp_display *display,
289 struct mod_hdcp_output *output)
290 {
291 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
292 struct mod_hdcp_display *display_container = NULL;
293
294 HDCP_TOP_INTERFACE_TRACE_WITH_INDEX(hdcp, display->index);
295 memset(output, 0, sizeof(struct mod_hdcp_output));
296
297 /* skip inactive display */
298 if (display->state != MOD_HDCP_DISPLAY_ACTIVE) {
299 status = MOD_HDCP_STATUS_SUCCESS;
300 goto out;
301 }
302
303 /* check existing display container */
304 if (get_active_display_at_index(hdcp, display->index)) {
305 status = MOD_HDCP_STATUS_SUCCESS;
306 goto out;
307 }
308
309 /* find an empty display container */
310 display_container = get_empty_display_container(hdcp);
311 if (!display_container) {
312 status = MOD_HDCP_STATUS_DISPLAY_OUT_OF_BOUND;
313 goto out;
314 }
315
316 /* reset existing authentication status */
317 status = reset_authentication(hdcp, output);
318 if (status != MOD_HDCP_STATUS_SUCCESS)
319 goto out;
320
321 /* reset retry counters */
322 reset_retry_counts(hdcp);
323
324 /* reset error trace */
325 memset(&hdcp->connection.trace, 0, sizeof(hdcp->connection.trace));
326
327 /* add display to connection */
328 hdcp->connection.link = *link;
329 *display_container = *display;
330 status = mod_hdcp_add_display_to_topology(hdcp, display_container);
331
332 if (status != MOD_HDCP_STATUS_SUCCESS)
333 goto out;
334
335 /* request authentication */
336 if (current_state(hdcp) != HDCP_INITIALIZED)
337 set_state_id(hdcp, output, HDCP_INITIALIZED);
338 callback_in_ms(hdcp->connection.link.adjust.auth_delay * 1000, output);
339 out:
340 if (status != MOD_HDCP_STATUS_SUCCESS)
341 push_error_status(hdcp, status);
342
343 return status;
344 }
345
mod_hdcp_remove_display(struct mod_hdcp * hdcp,uint8_t index,struct mod_hdcp_output * output)346 enum mod_hdcp_status mod_hdcp_remove_display(struct mod_hdcp *hdcp,
347 uint8_t index, struct mod_hdcp_output *output)
348 {
349 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
350 struct mod_hdcp_display *display = NULL;
351
352 HDCP_TOP_INTERFACE_TRACE_WITH_INDEX(hdcp, index);
353 memset(output, 0, sizeof(struct mod_hdcp_output));
354
355 /* find display in connection */
356 display = get_active_display_at_index(hdcp, index);
357 if (!display) {
358 status = MOD_HDCP_STATUS_SUCCESS;
359 goto out;
360 }
361
362 /* stop current authentication */
363 status = reset_authentication(hdcp, output);
364 if (status != MOD_HDCP_STATUS_SUCCESS)
365 goto out;
366
367 /* clear retry counters */
368 reset_retry_counts(hdcp);
369
370 /* reset error trace */
371 memset(&hdcp->connection.trace, 0, sizeof(hdcp->connection.trace));
372
373 /* remove display */
374 status = mod_hdcp_remove_display_from_topology(hdcp, index);
375 if (status != MOD_HDCP_STATUS_SUCCESS)
376 goto out;
377 memset(display, 0, sizeof(struct mod_hdcp_display));
378
379 /* request authentication when connection is not reset */
380 if (current_state(hdcp) != HDCP_UNINITIALIZED)
381 callback_in_ms(hdcp->connection.link.adjust.auth_delay * 1000,
382 output);
383 out:
384 if (status != MOD_HDCP_STATUS_SUCCESS)
385 push_error_status(hdcp, status);
386 return status;
387 }
388
mod_hdcp_query_display(struct mod_hdcp * hdcp,uint8_t index,struct mod_hdcp_display_query * query)389 enum mod_hdcp_status mod_hdcp_query_display(struct mod_hdcp *hdcp,
390 uint8_t index, struct mod_hdcp_display_query *query)
391 {
392 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
393 struct mod_hdcp_display *display = NULL;
394
395 /* find display in connection */
396 display = get_active_display_at_index(hdcp, index);
397 if (!display) {
398 status = MOD_HDCP_STATUS_DISPLAY_NOT_FOUND;
399 goto out;
400 }
401
402 /* populate query */
403 query->link = &hdcp->connection.link;
404 query->display = display;
405 query->trace = &hdcp->connection.trace;
406 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
407
408 if (is_display_encryption_enabled(display)) {
409 if (is_hdcp1(hdcp)) {
410 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP1_ON;
411 } else if (is_hdcp2(hdcp)) {
412 if (query->link->adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_0)
413 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE0_ON;
414 else if (query->link->adjust.hdcp2.force_type == MOD_HDCP_FORCE_TYPE_1)
415 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP2_TYPE1_ON;
416 else
417 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP2_ON;
418 }
419 } else {
420 query->encryption_status = MOD_HDCP_ENCRYPTION_STATUS_HDCP_OFF;
421 }
422
423 out:
424 return status;
425 }
426
mod_hdcp_reset_connection(struct mod_hdcp * hdcp,struct mod_hdcp_output * output)427 enum mod_hdcp_status mod_hdcp_reset_connection(struct mod_hdcp *hdcp,
428 struct mod_hdcp_output *output)
429 {
430 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS;
431
432 HDCP_TOP_INTERFACE_TRACE(hdcp);
433 status = reset_connection(hdcp, output);
434 if (status != MOD_HDCP_STATUS_SUCCESS)
435 push_error_status(hdcp, status);
436
437 return status;
438 }
439
mod_hdcp_process_event(struct mod_hdcp * hdcp,enum mod_hdcp_event event,struct mod_hdcp_output * output)440 enum mod_hdcp_status mod_hdcp_process_event(struct mod_hdcp *hdcp,
441 enum mod_hdcp_event event, struct mod_hdcp_output *output)
442 {
443 enum mod_hdcp_status exec_status, trans_status, reset_status, status;
444 struct mod_hdcp_event_context event_ctx;
445
446 HDCP_EVENT_TRACE(hdcp, event);
447 memset(output, 0, sizeof(struct mod_hdcp_output));
448 memset(&event_ctx, 0, sizeof(struct mod_hdcp_event_context));
449 event_ctx.event = event;
450
451 /* execute and transition */
452 exec_status = execution(hdcp, &event_ctx, &hdcp->auth.trans_input);
453 trans_status = transition(
454 hdcp, &event_ctx, &hdcp->auth.trans_input, output);
455 if (trans_status == MOD_HDCP_STATUS_SUCCESS) {
456 status = MOD_HDCP_STATUS_SUCCESS;
457 } else if (exec_status == MOD_HDCP_STATUS_SUCCESS) {
458 status = MOD_HDCP_STATUS_INTERNAL_POLICY_FAILURE;
459 push_error_status(hdcp, status);
460 } else {
461 status = exec_status;
462 push_error_status(hdcp, status);
463 }
464
465 /* reset authentication if needed */
466 if (trans_status == MOD_HDCP_STATUS_RESET_NEEDED) {
467 HDCP_FULL_DDC_TRACE(hdcp);
468 reset_status = reset_authentication(hdcp, output);
469 if (reset_status != MOD_HDCP_STATUS_SUCCESS)
470 push_error_status(hdcp, reset_status);
471 }
472
473 /* Clear CP_IRQ status if needed */
474 if (event_ctx.event == MOD_HDCP_EVENT_CPIRQ) {
475 status = mod_hdcp_clear_cp_irq_status(hdcp);
476 if (status != MOD_HDCP_STATUS_SUCCESS)
477 push_error_status(hdcp, status);
478 }
479
480 return status;
481 }
482
mod_hdcp_signal_type_to_operation_mode(enum signal_type signal)483 enum mod_hdcp_operation_mode mod_hdcp_signal_type_to_operation_mode(
484 enum signal_type signal)
485 {
486 enum mod_hdcp_operation_mode mode = MOD_HDCP_MODE_OFF;
487
488 switch (signal) {
489 case SIGNAL_TYPE_DVI_SINGLE_LINK:
490 case SIGNAL_TYPE_HDMI_TYPE_A:
491 mode = MOD_HDCP_MODE_DEFAULT;
492 break;
493 case SIGNAL_TYPE_EDP:
494 case SIGNAL_TYPE_DISPLAY_PORT:
495 case SIGNAL_TYPE_DISPLAY_PORT_MST:
496 mode = MOD_HDCP_MODE_DP;
497 break;
498 default:
499 break;
500 }
501
502 return mode;
503 }
504