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