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