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 inline enum mod_hdcp_status validate_bksv(struct mod_hdcp *hdcp) 29 { 30 uint64_t n = 0; 31 uint8_t count = 0; 32 u8 bksv[sizeof(n)] = { }; 33 34 memcpy(bksv, hdcp->auth.msg.hdcp1.bksv, sizeof(hdcp->auth.msg.hdcp1.bksv)); 35 n = *(uint64_t *)bksv; 36 37 while (n) { 38 count++; 39 n &= (n - 1); 40 } 41 return (count == 20) ? MOD_HDCP_STATUS_SUCCESS : 42 MOD_HDCP_STATUS_HDCP1_INVALID_BKSV; 43 } 44 45 static inline enum mod_hdcp_status check_ksv_ready(struct mod_hdcp *hdcp) 46 { 47 if (is_dp_hdcp(hdcp)) 48 return (hdcp->auth.msg.hdcp1.bstatus & DP_BSTATUS_READY) ? 49 MOD_HDCP_STATUS_SUCCESS : 50 MOD_HDCP_STATUS_HDCP1_KSV_LIST_NOT_READY; 51 return (hdcp->auth.msg.hdcp1.bcaps & DRM_HDCP_DDC_BCAPS_KSV_FIFO_READY) ? 52 MOD_HDCP_STATUS_SUCCESS : 53 MOD_HDCP_STATUS_HDCP1_KSV_LIST_NOT_READY; 54 } 55 56 static inline enum mod_hdcp_status check_hdcp_capable_dp(struct mod_hdcp *hdcp) 57 { 58 return (hdcp->auth.msg.hdcp1.bcaps & DP_BCAPS_HDCP_CAPABLE) ? 59 MOD_HDCP_STATUS_SUCCESS : 60 MOD_HDCP_STATUS_HDCP1_NOT_CAPABLE; 61 } 62 63 static inline enum mod_hdcp_status check_r0p_available_dp(struct mod_hdcp *hdcp) 64 { 65 enum mod_hdcp_status status; 66 if (is_dp_hdcp(hdcp)) { 67 status = (hdcp->auth.msg.hdcp1.bstatus & 68 DP_BSTATUS_R0_PRIME_READY) ? 69 MOD_HDCP_STATUS_SUCCESS : 70 MOD_HDCP_STATUS_HDCP1_R0_PRIME_PENDING; 71 } else { 72 status = MOD_HDCP_STATUS_INVALID_OPERATION; 73 } 74 return status; 75 } 76 77 static inline enum mod_hdcp_status check_link_integrity_dp( 78 struct mod_hdcp *hdcp) 79 { 80 return (hdcp->auth.msg.hdcp1.bstatus & 81 DP_BSTATUS_LINK_FAILURE) ? 82 MOD_HDCP_STATUS_HDCP1_LINK_INTEGRITY_FAILURE : 83 MOD_HDCP_STATUS_SUCCESS; 84 } 85 86 static inline enum mod_hdcp_status check_no_reauthentication_request_dp( 87 struct mod_hdcp *hdcp) 88 { 89 return (hdcp->auth.msg.hdcp1.bstatus & DP_BSTATUS_REAUTH_REQ) ? 90 MOD_HDCP_STATUS_HDCP1_REAUTH_REQUEST_ISSUED : 91 MOD_HDCP_STATUS_SUCCESS; 92 } 93 94 static inline enum mod_hdcp_status check_no_max_cascade(struct mod_hdcp *hdcp) 95 { 96 enum mod_hdcp_status status; 97 98 if (is_dp_hdcp(hdcp)) 99 status = DRM_HDCP_MAX_CASCADE_EXCEEDED(hdcp->auth.msg.hdcp1.binfo_dp >> 8) 100 ? MOD_HDCP_STATUS_HDCP1_MAX_CASCADE_EXCEEDED_FAILURE 101 : MOD_HDCP_STATUS_SUCCESS; 102 else 103 status = DRM_HDCP_MAX_CASCADE_EXCEEDED(hdcp->auth.msg.hdcp1.bstatus >> 8) 104 ? MOD_HDCP_STATUS_HDCP1_MAX_CASCADE_EXCEEDED_FAILURE 105 : MOD_HDCP_STATUS_SUCCESS; 106 return status; 107 } 108 109 static inline enum mod_hdcp_status check_no_max_devs(struct mod_hdcp *hdcp) 110 { 111 enum mod_hdcp_status status; 112 113 if (is_dp_hdcp(hdcp)) 114 status = DRM_HDCP_MAX_DEVICE_EXCEEDED(hdcp->auth.msg.hdcp1.binfo_dp) ? 115 MOD_HDCP_STATUS_HDCP1_MAX_DEVS_EXCEEDED_FAILURE : 116 MOD_HDCP_STATUS_SUCCESS; 117 else 118 status = DRM_HDCP_MAX_DEVICE_EXCEEDED(hdcp->auth.msg.hdcp1.bstatus) ? 119 MOD_HDCP_STATUS_HDCP1_MAX_DEVS_EXCEEDED_FAILURE : 120 MOD_HDCP_STATUS_SUCCESS; 121 return status; 122 } 123 124 static inline uint8_t get_device_count(struct mod_hdcp *hdcp) 125 { 126 return is_dp_hdcp(hdcp) ? 127 DRM_HDCP_NUM_DOWNSTREAM(hdcp->auth.msg.hdcp1.binfo_dp) : 128 DRM_HDCP_NUM_DOWNSTREAM(hdcp->auth.msg.hdcp1.bstatus); 129 } 130 131 static inline enum mod_hdcp_status check_device_count(struct mod_hdcp *hdcp) 132 { 133 /* Avoid device count == 0 to do authentication */ 134 if (0 == get_device_count(hdcp)) { 135 return MOD_HDCP_STATUS_HDCP1_DEVICE_COUNT_MISMATCH_FAILURE; 136 } 137 138 /* Some MST display may choose to report the internal panel as an HDCP RX. 139 * To update this condition with 1(because the immediate repeater's internal 140 * panel is possibly not included in DEVICE_COUNT) + get_device_count(hdcp). 141 * Device count must be greater than or equal to tracked hdcp displays. 142 */ 143 return ((1 + get_device_count(hdcp)) < get_active_display_count(hdcp)) ? 144 MOD_HDCP_STATUS_HDCP1_DEVICE_COUNT_MISMATCH_FAILURE : 145 MOD_HDCP_STATUS_SUCCESS; 146 } 147 148 static enum mod_hdcp_status wait_for_active_rx(struct mod_hdcp *hdcp, 149 struct mod_hdcp_event_context *event_ctx, 150 struct mod_hdcp_transition_input_hdcp1 *input) 151 { 152 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 153 154 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) { 155 event_ctx->unexpected_event = 1; 156 goto out; 157 } 158 159 if (!mod_hdcp_execute_and_set(mod_hdcp_read_bksv, 160 &input->bksv_read, &status, 161 hdcp, "bksv_read")) 162 goto out; 163 if (!mod_hdcp_execute_and_set(mod_hdcp_read_bcaps, 164 &input->bcaps_read, &status, 165 hdcp, "bcaps_read")) 166 goto out; 167 out: 168 return status; 169 } 170 171 static enum mod_hdcp_status exchange_ksvs(struct mod_hdcp *hdcp, 172 struct mod_hdcp_event_context *event_ctx, 173 struct mod_hdcp_transition_input_hdcp1 *input) 174 { 175 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 176 177 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) { 178 event_ctx->unexpected_event = 1; 179 goto out; 180 } 181 182 if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp1_create_session, 183 &input->create_session, &status, 184 hdcp, "create_session")) 185 goto out; 186 if (!mod_hdcp_execute_and_set(mod_hdcp_write_an, 187 &input->an_write, &status, 188 hdcp, "an_write")) 189 goto out; 190 if (!mod_hdcp_execute_and_set(mod_hdcp_write_aksv, 191 &input->aksv_write, &status, 192 hdcp, "aksv_write")) 193 goto out; 194 if (!mod_hdcp_execute_and_set(mod_hdcp_read_bksv, 195 &input->bksv_read, &status, 196 hdcp, "bksv_read")) 197 goto out; 198 if (!mod_hdcp_execute_and_set(validate_bksv, 199 &input->bksv_validation, &status, 200 hdcp, "bksv_validation")) 201 goto out; 202 if (hdcp->auth.msg.hdcp1.ainfo) { 203 if (!mod_hdcp_execute_and_set(mod_hdcp_write_ainfo, 204 &input->ainfo_write, &status, 205 hdcp, "ainfo_write")) 206 goto out; 207 } 208 out: 209 return status; 210 } 211 212 static enum mod_hdcp_status computations_validate_rx_test_for_repeater( 213 struct mod_hdcp *hdcp, 214 struct mod_hdcp_event_context *event_ctx, 215 struct mod_hdcp_transition_input_hdcp1 *input) 216 { 217 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 218 219 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) { 220 event_ctx->unexpected_event = 1; 221 goto out; 222 } 223 224 if (!mod_hdcp_execute_and_set(mod_hdcp_read_r0p, 225 &input->r0p_read, &status, 226 hdcp, "r0p_read")) 227 goto out; 228 if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp1_validate_rx, 229 &input->rx_validation, &status, 230 hdcp, "rx_validation")) 231 goto out; 232 if (hdcp->connection.is_repeater) { 233 if (!hdcp->connection.link.adjust.hdcp1.postpone_encryption) 234 if (!mod_hdcp_execute_and_set( 235 mod_hdcp_hdcp1_enable_encryption, 236 &input->encryption, &status, 237 hdcp, "encryption")) 238 goto out; 239 } else { 240 if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp1_enable_encryption, 241 &input->encryption, &status, 242 hdcp, "encryption")) 243 goto out; 244 if (is_dp_mst_hdcp(hdcp)) 245 if (!mod_hdcp_execute_and_set( 246 mod_hdcp_hdcp1_enable_dp_stream_encryption, 247 &input->stream_encryption_dp, &status, 248 hdcp, "stream_encryption_dp")) 249 goto out; 250 } 251 out: 252 return status; 253 } 254 255 static enum mod_hdcp_status authenticated(struct mod_hdcp *hdcp, 256 struct mod_hdcp_event_context *event_ctx, 257 struct mod_hdcp_transition_input_hdcp1 *input) 258 { 259 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 260 261 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) { 262 event_ctx->unexpected_event = 1; 263 goto out; 264 } 265 266 mod_hdcp_execute_and_set(mod_hdcp_hdcp1_link_maintenance, 267 &input->link_maintenance, &status, 268 hdcp, "link_maintenance"); 269 270 if (status != MOD_HDCP_STATUS_SUCCESS) 271 mod_hdcp_save_current_encryption_states(hdcp); 272 out: 273 return status; 274 } 275 276 static enum mod_hdcp_status wait_for_ready(struct mod_hdcp *hdcp, 277 struct mod_hdcp_event_context *event_ctx, 278 struct mod_hdcp_transition_input_hdcp1 *input) 279 { 280 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 281 282 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK && 283 event_ctx->event != MOD_HDCP_EVENT_CPIRQ && 284 event_ctx->event != MOD_HDCP_EVENT_WATCHDOG_TIMEOUT) { 285 event_ctx->unexpected_event = 1; 286 goto out; 287 } 288 289 if (is_dp_hdcp(hdcp)) { 290 if (!mod_hdcp_execute_and_set(mod_hdcp_read_bstatus, 291 &input->bstatus_read, &status, 292 hdcp, "bstatus_read")) 293 goto out; 294 if (!mod_hdcp_execute_and_set(check_link_integrity_dp, 295 &input->link_integrity_check, &status, 296 hdcp, "link_integrity_check")) 297 goto out; 298 if (!mod_hdcp_execute_and_set(check_no_reauthentication_request_dp, 299 &input->reauth_request_check, &status, 300 hdcp, "reauth_request_check")) 301 goto out; 302 } else { 303 if (!mod_hdcp_execute_and_set(mod_hdcp_read_bcaps, 304 &input->bcaps_read, &status, 305 hdcp, "bcaps_read")) 306 goto out; 307 } 308 if (!mod_hdcp_execute_and_set(check_ksv_ready, 309 &input->ready_check, &status, 310 hdcp, "ready_check")) 311 goto out; 312 out: 313 return status; 314 } 315 316 static enum mod_hdcp_status read_ksv_list(struct mod_hdcp *hdcp, 317 struct mod_hdcp_event_context *event_ctx, 318 struct mod_hdcp_transition_input_hdcp1 *input) 319 { 320 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 321 uint8_t device_count; 322 323 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) { 324 event_ctx->unexpected_event = 1; 325 goto out; 326 } 327 328 if (is_dp_hdcp(hdcp)) { 329 if (!mod_hdcp_execute_and_set(mod_hdcp_read_binfo, 330 &input->binfo_read_dp, &status, 331 hdcp, "binfo_read_dp")) 332 goto out; 333 } else { 334 if (!mod_hdcp_execute_and_set(mod_hdcp_read_bstatus, 335 &input->bstatus_read, &status, 336 hdcp, "bstatus_read")) 337 goto out; 338 } 339 if (!mod_hdcp_execute_and_set(check_no_max_cascade, 340 &input->max_cascade_check, &status, 341 hdcp, "max_cascade_check")) 342 goto out; 343 if (!mod_hdcp_execute_and_set(check_no_max_devs, 344 &input->max_devs_check, &status, 345 hdcp, "max_devs_check")) 346 goto out; 347 if (!mod_hdcp_execute_and_set(check_device_count, 348 &input->device_count_check, &status, 349 hdcp, "device_count_check")) 350 goto out; 351 device_count = get_device_count(hdcp); 352 hdcp->auth.msg.hdcp1.ksvlist_size = device_count*5; 353 if (!mod_hdcp_execute_and_set(mod_hdcp_read_ksvlist, 354 &input->ksvlist_read, &status, 355 hdcp, "ksvlist_read")) 356 goto out; 357 if (!mod_hdcp_execute_and_set(mod_hdcp_read_vp, 358 &input->vp_read, &status, 359 hdcp, "vp_read")) 360 goto out; 361 if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp1_validate_ksvlist_vp, 362 &input->ksvlist_vp_validation, &status, 363 hdcp, "ksvlist_vp_validation")) 364 goto out; 365 if (input->encryption != PASS) 366 if (!mod_hdcp_execute_and_set(mod_hdcp_hdcp1_enable_encryption, 367 &input->encryption, &status, 368 hdcp, "encryption")) 369 goto out; 370 if (is_dp_mst_hdcp(hdcp)) 371 if (!mod_hdcp_execute_and_set( 372 mod_hdcp_hdcp1_enable_dp_stream_encryption, 373 &input->stream_encryption_dp, &status, 374 hdcp, "stream_encryption_dp")) 375 goto out; 376 out: 377 return status; 378 } 379 380 static enum mod_hdcp_status determine_rx_hdcp_capable_dp(struct mod_hdcp *hdcp, 381 struct mod_hdcp_event_context *event_ctx, 382 struct mod_hdcp_transition_input_hdcp1 *input) 383 { 384 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 385 386 if (event_ctx->event != MOD_HDCP_EVENT_CALLBACK) { 387 event_ctx->unexpected_event = 1; 388 goto out; 389 } 390 391 if (!mod_hdcp_execute_and_set(mod_hdcp_read_bcaps, 392 &input->bcaps_read, &status, 393 hdcp, "bcaps_read")) 394 goto out; 395 if (!mod_hdcp_execute_and_set(check_hdcp_capable_dp, 396 &input->hdcp_capable_dp, &status, 397 hdcp, "hdcp_capable_dp")) 398 goto out; 399 out: 400 return status; 401 } 402 403 static enum mod_hdcp_status wait_for_r0_prime_dp(struct mod_hdcp *hdcp, 404 struct mod_hdcp_event_context *event_ctx, 405 struct mod_hdcp_transition_input_hdcp1 *input) 406 { 407 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 408 409 if (event_ctx->event != MOD_HDCP_EVENT_CPIRQ && 410 event_ctx->event != MOD_HDCP_EVENT_WATCHDOG_TIMEOUT) { 411 event_ctx->unexpected_event = 1; 412 goto out; 413 } 414 415 if (!mod_hdcp_execute_and_set(mod_hdcp_read_bstatus, 416 &input->bstatus_read, &status, 417 hdcp, "bstatus_read")) 418 goto out; 419 if (!mod_hdcp_execute_and_set(check_r0p_available_dp, 420 &input->r0p_available_dp, &status, 421 hdcp, "r0p_available_dp")) 422 goto out; 423 out: 424 return status; 425 } 426 427 static enum mod_hdcp_status authenticated_dp(struct mod_hdcp *hdcp, 428 struct mod_hdcp_event_context *event_ctx, 429 struct mod_hdcp_transition_input_hdcp1 *input) 430 { 431 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 432 433 if (event_ctx->event != MOD_HDCP_EVENT_CPIRQ) { 434 event_ctx->unexpected_event = 1; 435 goto out; 436 } 437 438 if (status == MOD_HDCP_STATUS_SUCCESS) 439 mod_hdcp_execute_and_set(mod_hdcp_read_bstatus, 440 &input->bstatus_read, &status, 441 hdcp, "bstatus_read"); 442 if (status == MOD_HDCP_STATUS_SUCCESS) 443 mod_hdcp_execute_and_set(check_link_integrity_dp, 444 &input->link_integrity_check, &status, 445 hdcp, "link_integrity_check"); 446 if (status == MOD_HDCP_STATUS_SUCCESS) 447 mod_hdcp_execute_and_set(check_no_reauthentication_request_dp, 448 &input->reauth_request_check, &status, 449 hdcp, "reauth_request_check"); 450 451 if (status != MOD_HDCP_STATUS_SUCCESS) 452 mod_hdcp_save_current_encryption_states(hdcp); 453 out: 454 return status; 455 } 456 457 uint8_t mod_hdcp_execute_and_set( 458 mod_hdcp_action func, uint8_t *flag, 459 enum mod_hdcp_status *status, struct mod_hdcp *hdcp, char *str) 460 { 461 *status = func(hdcp); 462 if (*status == MOD_HDCP_STATUS_SUCCESS && *flag != PASS) { 463 HDCP_INPUT_PASS_TRACE(hdcp, str); 464 *flag = PASS; 465 } else if (*status != MOD_HDCP_STATUS_SUCCESS && *flag != FAIL) { 466 HDCP_INPUT_FAIL_TRACE(hdcp, str); 467 *flag = FAIL; 468 } 469 return (*status == MOD_HDCP_STATUS_SUCCESS); 470 } 471 472 enum mod_hdcp_status mod_hdcp_hdcp1_execution(struct mod_hdcp *hdcp, 473 struct mod_hdcp_event_context *event_ctx, 474 struct mod_hdcp_transition_input_hdcp1 *input) 475 { 476 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 477 478 switch (current_state(hdcp)) { 479 case H1_A0_WAIT_FOR_ACTIVE_RX: 480 status = wait_for_active_rx(hdcp, event_ctx, input); 481 break; 482 case H1_A1_EXCHANGE_KSVS: 483 status = exchange_ksvs(hdcp, event_ctx, input); 484 break; 485 case H1_A2_COMPUTATIONS_A3_VALIDATE_RX_A6_TEST_FOR_REPEATER: 486 status = computations_validate_rx_test_for_repeater(hdcp, 487 event_ctx, input); 488 break; 489 case H1_A45_AUTHENTICATED: 490 status = authenticated(hdcp, event_ctx, input); 491 break; 492 case H1_A8_WAIT_FOR_READY: 493 status = wait_for_ready(hdcp, event_ctx, input); 494 break; 495 case H1_A9_READ_KSV_LIST: 496 status = read_ksv_list(hdcp, event_ctx, input); 497 break; 498 default: 499 status = MOD_HDCP_STATUS_INVALID_STATE; 500 break; 501 } 502 503 return status; 504 } 505 506 extern enum mod_hdcp_status mod_hdcp_hdcp1_dp_execution(struct mod_hdcp *hdcp, 507 struct mod_hdcp_event_context *event_ctx, 508 struct mod_hdcp_transition_input_hdcp1 *input) 509 { 510 enum mod_hdcp_status status = MOD_HDCP_STATUS_SUCCESS; 511 512 switch (current_state(hdcp)) { 513 case D1_A0_DETERMINE_RX_HDCP_CAPABLE: 514 status = determine_rx_hdcp_capable_dp(hdcp, event_ctx, input); 515 break; 516 case D1_A1_EXCHANGE_KSVS: 517 status = exchange_ksvs(hdcp, event_ctx, input); 518 break; 519 case D1_A23_WAIT_FOR_R0_PRIME: 520 status = wait_for_r0_prime_dp(hdcp, event_ctx, input); 521 break; 522 case D1_A2_COMPUTATIONS_A3_VALIDATE_RX_A5_TEST_FOR_REPEATER: 523 status = computations_validate_rx_test_for_repeater( 524 hdcp, event_ctx, input); 525 break; 526 case D1_A4_AUTHENTICATED: 527 status = authenticated_dp(hdcp, event_ctx, input); 528 break; 529 case D1_A6_WAIT_FOR_READY: 530 status = wait_for_ready(hdcp, event_ctx, input); 531 break; 532 case D1_A7_READ_KSV_LIST: 533 status = read_ksv_list(hdcp, event_ctx, input); 534 break; 535 default: 536 status = MOD_HDCP_STATUS_INVALID_STATE; 537 break; 538 } 539 540 return status; 541 } 542