1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright(c) 2024 Intel Corporation. 4 */ 5 6 #include "xe_pxp.h" 7 8 #include <drm/drm_managed.h> 9 #include <uapi/drm/xe_drm.h> 10 11 #include "xe_bo.h" 12 #include "xe_bo_types.h" 13 #include "xe_device_types.h" 14 #include "xe_exec_queue.h" 15 #include "xe_force_wake.h" 16 #include "xe_guc_submit.h" 17 #include "xe_gsc_proxy.h" 18 #include "xe_gt.h" 19 #include "xe_gt_types.h" 20 #include "xe_huc.h" 21 #include "xe_mmio.h" 22 #include "xe_pm.h" 23 #include "xe_pxp_submit.h" 24 #include "xe_pxp_types.h" 25 #include "xe_uc_fw.h" 26 #include "regs/xe_irq_regs.h" 27 #include "regs/xe_pxp_regs.h" 28 29 /** 30 * DOC: PXP 31 * 32 * PXP (Protected Xe Path) allows execution and flip to display of protected 33 * (i.e. encrypted) objects. This feature is currently only supported in 34 * integrated parts. 35 */ 36 37 #define ARB_SESSION DRM_XE_PXP_HWDRM_DEFAULT_SESSION /* shorter define */ 38 39 /* 40 * A submission to GSC can take up to 250ms to complete, so use a 300ms 41 * timeout for activation where only one of those is involved. Termination 42 * additionally requires a submission to VCS and an interaction with KCR, so 43 * bump the timeout to 500ms for that. 44 */ 45 #define PXP_ACTIVATION_TIMEOUT_MS 300 46 #define PXP_TERMINATION_TIMEOUT_MS 500 47 48 bool xe_pxp_is_supported(const struct xe_device *xe) 49 { 50 return xe->info.has_pxp && IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY); 51 } 52 53 bool xe_pxp_is_enabled(const struct xe_pxp *pxp) 54 { 55 return pxp; 56 } 57 58 static bool pxp_prerequisites_done(const struct xe_pxp *pxp) 59 { 60 struct xe_gt *gt = pxp->gt; 61 unsigned int fw_ref; 62 bool ready; 63 64 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FORCEWAKE_ALL); 65 66 /* 67 * If force_wake fails we could falsely report the prerequisites as not 68 * done even if they are; the consequence of this would be that the 69 * callers won't go ahead with using PXP, but if force_wake doesn't work 70 * the GT is very likely in a bad state so not really a problem to abort 71 * PXP. Therefore, we can just log the force_wake error and not escalate 72 * it. 73 */ 74 XE_WARN_ON(!xe_force_wake_ref_has_domain(fw_ref, XE_FORCEWAKE_ALL)); 75 76 /* PXP requires both HuC authentication via GSC and GSC proxy initialized */ 77 ready = xe_huc_is_authenticated(>->uc.huc, XE_HUC_AUTH_VIA_GSC) && 78 xe_gsc_proxy_init_done(>->uc.gsc); 79 80 xe_force_wake_put(gt_to_fw(gt), fw_ref); 81 82 return ready; 83 } 84 85 /** 86 * xe_pxp_get_readiness_status - check whether PXP is ready for userspace use 87 * @pxp: the xe_pxp pointer (can be NULL if PXP is disabled) 88 * 89 * Returns: 0 if PXP is not ready yet, 1 if it is ready, a negative errno value 90 * if PXP is not supported/enabled or if something went wrong in the 91 * initialization of the prerequisites. Note that the return values of this 92 * function follow the uapi (see drm_xe_query_pxp_status), so they can be used 93 * directly in the query ioctl. 94 */ 95 int xe_pxp_get_readiness_status(struct xe_pxp *pxp) 96 { 97 int ret = 0; 98 99 if (!xe_pxp_is_enabled(pxp)) 100 return -ENODEV; 101 102 /* if the GSC or HuC FW are in an error state, PXP will never work */ 103 if (xe_uc_fw_status_to_error(pxp->gt->uc.huc.fw.status) || 104 xe_uc_fw_status_to_error(pxp->gt->uc.gsc.fw.status)) 105 return -EIO; 106 107 xe_pm_runtime_get(pxp->xe); 108 109 /* PXP requires both HuC loaded and GSC proxy initialized */ 110 if (pxp_prerequisites_done(pxp)) 111 ret = 1; 112 113 xe_pm_runtime_put(pxp->xe); 114 return ret; 115 } 116 117 static bool pxp_session_is_in_play(struct xe_pxp *pxp, u32 id) 118 { 119 struct xe_gt *gt = pxp->gt; 120 121 return xe_mmio_read32(>->mmio, KCR_SIP) & BIT(id); 122 } 123 124 static int pxp_wait_for_session_state(struct xe_pxp *pxp, u32 id, bool in_play) 125 { 126 struct xe_gt *gt = pxp->gt; 127 u32 mask = BIT(id); 128 129 return xe_mmio_wait32(>->mmio, KCR_SIP, mask, in_play ? mask : 0, 130 250, NULL, false); 131 } 132 133 static void pxp_invalidate_queues(struct xe_pxp *pxp); 134 135 static void pxp_invalidate_state(struct xe_pxp *pxp) 136 { 137 pxp_invalidate_queues(pxp); 138 139 if (pxp->status == XE_PXP_ACTIVE) 140 pxp->key_instance++; 141 } 142 143 static int pxp_terminate_hw(struct xe_pxp *pxp) 144 { 145 struct xe_gt *gt = pxp->gt; 146 unsigned int fw_ref; 147 int ret = 0; 148 149 drm_dbg(&pxp->xe->drm, "Terminating PXP\n"); 150 151 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT); 152 if (!xe_force_wake_ref_has_domain(fw_ref, XE_FW_GT)) { 153 ret = -EIO; 154 goto out; 155 } 156 157 /* terminate the hw session */ 158 ret = xe_pxp_submit_session_termination(pxp, ARB_SESSION); 159 if (ret) 160 goto out; 161 162 ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false); 163 if (ret) 164 goto out; 165 166 /* Trigger full HW cleanup */ 167 xe_mmio_write32(>->mmio, KCR_GLOBAL_TERMINATE, 1); 168 169 /* now we can tell the GSC to clean up its own state */ 170 ret = xe_pxp_submit_session_invalidation(&pxp->gsc_res, ARB_SESSION); 171 172 out: 173 xe_force_wake_put(gt_to_fw(gt), fw_ref); 174 return ret; 175 } 176 177 static void mark_termination_in_progress(struct xe_pxp *pxp) 178 { 179 lockdep_assert_held(&pxp->mutex); 180 181 reinit_completion(&pxp->termination); 182 pxp->status = XE_PXP_TERMINATION_IN_PROGRESS; 183 } 184 185 static void pxp_terminate(struct xe_pxp *pxp) 186 { 187 int ret = 0; 188 struct xe_device *xe = pxp->xe; 189 190 if (!wait_for_completion_timeout(&pxp->activation, 191 msecs_to_jiffies(PXP_ACTIVATION_TIMEOUT_MS))) 192 drm_err(&xe->drm, "failed to wait for PXP start before termination\n"); 193 194 mutex_lock(&pxp->mutex); 195 196 pxp_invalidate_state(pxp); 197 198 /* 199 * we'll mark the status as needing termination on resume, so no need to 200 * emit a termination now. 201 */ 202 if (pxp->status == XE_PXP_SUSPENDED) { 203 mutex_unlock(&pxp->mutex); 204 return; 205 } 206 207 /* 208 * If we have a termination already in progress, we need to wait for 209 * it to complete before queueing another one. Once the first 210 * termination is completed we'll set the state back to 211 * NEEDS_TERMINATION and leave it to the pxp start code to issue it. 212 */ 213 if (pxp->status == XE_PXP_TERMINATION_IN_PROGRESS) { 214 pxp->status = XE_PXP_NEEDS_ADDITIONAL_TERMINATION; 215 mutex_unlock(&pxp->mutex); 216 return; 217 } 218 219 mark_termination_in_progress(pxp); 220 221 mutex_unlock(&pxp->mutex); 222 223 ret = pxp_terminate_hw(pxp); 224 if (ret) { 225 drm_err(&xe->drm, "PXP termination failed: %pe\n", ERR_PTR(ret)); 226 mutex_lock(&pxp->mutex); 227 pxp->status = XE_PXP_ERROR; 228 complete_all(&pxp->termination); 229 mutex_unlock(&pxp->mutex); 230 } 231 } 232 233 static void pxp_terminate_complete(struct xe_pxp *pxp) 234 { 235 /* 236 * We expect PXP to be in one of 3 states when we get here: 237 * - XE_PXP_TERMINATION_IN_PROGRESS: a single termination event was 238 * requested and it is now completing, so we're ready to start. 239 * - XE_PXP_NEEDS_ADDITIONAL_TERMINATION: a second termination was 240 * requested while the first one was still being processed. 241 * - XE_PXP_SUSPENDED: PXP is now suspended, so we defer everything to 242 * when we come back on resume. 243 */ 244 mutex_lock(&pxp->mutex); 245 246 switch (pxp->status) { 247 case XE_PXP_TERMINATION_IN_PROGRESS: 248 pxp->status = XE_PXP_READY_TO_START; 249 break; 250 case XE_PXP_NEEDS_ADDITIONAL_TERMINATION: 251 pxp->status = XE_PXP_NEEDS_TERMINATION; 252 break; 253 case XE_PXP_SUSPENDED: 254 /* Nothing to do */ 255 break; 256 default: 257 drm_err(&pxp->xe->drm, 258 "PXP termination complete while status was %u\n", 259 pxp->status); 260 } 261 262 complete_all(&pxp->termination); 263 264 mutex_unlock(&pxp->mutex); 265 } 266 267 static void pxp_irq_work(struct work_struct *work) 268 { 269 struct xe_pxp *pxp = container_of(work, typeof(*pxp), irq.work); 270 struct xe_device *xe = pxp->xe; 271 u32 events = 0; 272 273 spin_lock_irq(&xe->irq.lock); 274 events = pxp->irq.events; 275 pxp->irq.events = 0; 276 spin_unlock_irq(&xe->irq.lock); 277 278 if (!events) 279 return; 280 281 /* 282 * If we're processing a termination irq while suspending then don't 283 * bother, we're going to re-init everything on resume anyway. 284 */ 285 if ((events & PXP_TERMINATION_REQUEST) && !xe_pm_runtime_get_if_active(xe)) 286 return; 287 288 if (events & PXP_TERMINATION_REQUEST) { 289 events &= ~PXP_TERMINATION_COMPLETE; 290 pxp_terminate(pxp); 291 } 292 293 if (events & PXP_TERMINATION_COMPLETE) 294 pxp_terminate_complete(pxp); 295 296 if (events & PXP_TERMINATION_REQUEST) 297 xe_pm_runtime_put(xe); 298 } 299 300 /** 301 * xe_pxp_irq_handler - Handles PXP interrupts. 302 * @xe: the xe_device structure 303 * @iir: interrupt vector 304 */ 305 void xe_pxp_irq_handler(struct xe_device *xe, u16 iir) 306 { 307 struct xe_pxp *pxp = xe->pxp; 308 309 if (!xe_pxp_is_enabled(pxp)) { 310 drm_err(&xe->drm, "PXP irq 0x%x received with PXP disabled!\n", iir); 311 return; 312 } 313 314 lockdep_assert_held(&xe->irq.lock); 315 316 if (unlikely(!iir)) 317 return; 318 319 if (iir & (KCR_PXP_STATE_TERMINATED_INTERRUPT | 320 KCR_APP_TERMINATED_PER_FW_REQ_INTERRUPT)) 321 pxp->irq.events |= PXP_TERMINATION_REQUEST; 322 323 if (iir & KCR_PXP_STATE_RESET_COMPLETE_INTERRUPT) 324 pxp->irq.events |= PXP_TERMINATION_COMPLETE; 325 326 if (pxp->irq.events) 327 queue_work(pxp->irq.wq, &pxp->irq.work); 328 } 329 330 static int kcr_pxp_set_status(const struct xe_pxp *pxp, bool enable) 331 { 332 u32 val = enable ? _MASKED_BIT_ENABLE(KCR_INIT_ALLOW_DISPLAY_ME_WRITES) : 333 _MASKED_BIT_DISABLE(KCR_INIT_ALLOW_DISPLAY_ME_WRITES); 334 unsigned int fw_ref; 335 336 fw_ref = xe_force_wake_get(gt_to_fw(pxp->gt), XE_FW_GT); 337 if (!xe_force_wake_ref_has_domain(fw_ref, XE_FW_GT)) 338 return -EIO; 339 340 xe_mmio_write32(&pxp->gt->mmio, KCR_INIT, val); 341 xe_force_wake_put(gt_to_fw(pxp->gt), fw_ref); 342 343 return 0; 344 } 345 346 static int kcr_pxp_enable(const struct xe_pxp *pxp) 347 { 348 return kcr_pxp_set_status(pxp, true); 349 } 350 351 static int kcr_pxp_disable(const struct xe_pxp *pxp) 352 { 353 return kcr_pxp_set_status(pxp, false); 354 } 355 356 static void pxp_fini(void *arg) 357 { 358 struct xe_pxp *pxp = arg; 359 360 destroy_workqueue(pxp->irq.wq); 361 xe_pxp_destroy_execution_resources(pxp); 362 363 /* no need to explicitly disable KCR since we're going to do an FLR */ 364 } 365 366 /** 367 * xe_pxp_init - initialize PXP support 368 * @xe: the xe_device structure 369 * 370 * Initialize the HW state and allocate the objects required for PXP support. 371 * Note that some of the requirement for PXP support (GSC proxy init, HuC auth) 372 * are performed asynchronously as part of the GSC init. PXP can only be used 373 * after both this function and the async worker have completed. 374 * 375 * Returns 0 if PXP is not supported or if PXP initialization is successful, 376 * other errno value if there is an error during the init. 377 */ 378 int xe_pxp_init(struct xe_device *xe) 379 { 380 struct xe_gt *gt = xe->tiles[0].media_gt; 381 struct xe_pxp *pxp; 382 int err; 383 384 if (!xe_pxp_is_supported(xe)) 385 return 0; 386 387 /* we only support PXP on single tile devices with a media GT */ 388 if (xe->info.tile_count > 1 || !gt) 389 return 0; 390 391 /* The GSCCS is required for submissions to the GSC FW */ 392 if (!(gt->info.engine_mask & BIT(XE_HW_ENGINE_GSCCS0))) 393 return 0; 394 395 /* PXP requires both GSC and HuC firmwares to be available */ 396 if (!xe_uc_fw_is_loadable(>->uc.gsc.fw) || 397 !xe_uc_fw_is_loadable(>->uc.huc.fw)) { 398 drm_info(&xe->drm, "skipping PXP init due to missing FW dependencies"); 399 return 0; 400 } 401 402 pxp = drmm_kzalloc(&xe->drm, sizeof(struct xe_pxp), GFP_KERNEL); 403 if (!pxp) { 404 err = -ENOMEM; 405 goto out; 406 } 407 408 INIT_LIST_HEAD(&pxp->queues.list); 409 spin_lock_init(&pxp->queues.lock); 410 INIT_WORK(&pxp->irq.work, pxp_irq_work); 411 pxp->xe = xe; 412 pxp->gt = gt; 413 414 pxp->key_instance = 1; 415 pxp->last_suspend_key_instance = 1; 416 417 /* 418 * we'll use the completions to check if there is an action pending, 419 * so we start them as completed and we reinit it when an action is 420 * triggered. 421 */ 422 init_completion(&pxp->activation); 423 init_completion(&pxp->termination); 424 complete_all(&pxp->termination); 425 complete_all(&pxp->activation); 426 427 mutex_init(&pxp->mutex); 428 429 pxp->irq.wq = alloc_ordered_workqueue("pxp-wq", 0); 430 if (!pxp->irq.wq) { 431 err = -ENOMEM; 432 goto out_free; 433 } 434 435 err = kcr_pxp_enable(pxp); 436 if (err) 437 goto out_wq; 438 439 err = xe_pxp_allocate_execution_resources(pxp); 440 if (err) 441 goto out_kcr_disable; 442 443 xe->pxp = pxp; 444 445 return devm_add_action_or_reset(xe->drm.dev, pxp_fini, pxp); 446 447 out_kcr_disable: 448 kcr_pxp_disable(pxp); 449 out_wq: 450 destroy_workqueue(pxp->irq.wq); 451 out_free: 452 drmm_kfree(&xe->drm, pxp); 453 out: 454 drm_err(&xe->drm, "PXP initialization failed: %pe\n", ERR_PTR(err)); 455 return err; 456 } 457 458 static int __pxp_start_arb_session(struct xe_pxp *pxp) 459 { 460 int ret; 461 unsigned int fw_ref; 462 463 fw_ref = xe_force_wake_get(gt_to_fw(pxp->gt), XE_FW_GT); 464 if (!xe_force_wake_ref_has_domain(fw_ref, XE_FW_GT)) 465 return -EIO; 466 467 if (pxp_session_is_in_play(pxp, ARB_SESSION)) { 468 ret = -EEXIST; 469 goto out_force_wake; 470 } 471 472 ret = xe_pxp_submit_session_init(&pxp->gsc_res, ARB_SESSION); 473 if (ret) { 474 drm_err(&pxp->xe->drm, "Failed to init PXP arb session: %pe\n", ERR_PTR(ret)); 475 goto out_force_wake; 476 } 477 478 ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true); 479 if (ret) { 480 drm_err(&pxp->xe->drm, "PXP ARB session failed to go in play%pe\n", ERR_PTR(ret)); 481 goto out_force_wake; 482 } 483 484 drm_dbg(&pxp->xe->drm, "PXP ARB session is active\n"); 485 486 out_force_wake: 487 xe_force_wake_put(gt_to_fw(pxp->gt), fw_ref); 488 return ret; 489 } 490 491 /** 492 * xe_pxp_exec_queue_set_type - Mark a queue as using PXP 493 * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled) 494 * @q: the queue to mark as using PXP 495 * @type: the type of PXP session this queue will use 496 * 497 * Returns 0 if the selected PXP type is supported, -ENODEV otherwise. 498 */ 499 int xe_pxp_exec_queue_set_type(struct xe_pxp *pxp, struct xe_exec_queue *q, u8 type) 500 { 501 if (!xe_pxp_is_enabled(pxp)) 502 return -ENODEV; 503 504 /* we only support HWDRM sessions right now */ 505 xe_assert(pxp->xe, type == DRM_XE_PXP_TYPE_HWDRM); 506 507 q->pxp.type = type; 508 509 return 0; 510 } 511 512 static void __exec_queue_add(struct xe_pxp *pxp, struct xe_exec_queue *q) 513 { 514 spin_lock_irq(&pxp->queues.lock); 515 list_add_tail(&q->pxp.link, &pxp->queues.list); 516 spin_unlock_irq(&pxp->queues.lock); 517 } 518 519 /** 520 * xe_pxp_exec_queue_add - add a queue to the PXP list 521 * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled) 522 * @q: the queue to add to the list 523 * 524 * If PXP is enabled and the prerequisites are done, start the PXP ARB 525 * session (if not already running) and add the queue to the PXP list. Note 526 * that the queue must have previously been marked as using PXP with 527 * xe_pxp_exec_queue_set_type. 528 * 529 * Returns 0 if the PXP ARB session is running and the queue is in the list, 530 * -ENODEV if PXP is disabled, -EBUSY if the PXP prerequisites are not done, 531 * other errno value if something goes wrong during the session start. 532 */ 533 int xe_pxp_exec_queue_add(struct xe_pxp *pxp, struct xe_exec_queue *q) 534 { 535 int ret = 0; 536 537 if (!xe_pxp_is_enabled(pxp)) 538 return -ENODEV; 539 540 /* we only support HWDRM sessions right now */ 541 xe_assert(pxp->xe, q->pxp.type == DRM_XE_PXP_TYPE_HWDRM); 542 543 /* 544 * Runtime suspend kills PXP, so we take a reference to prevent it from 545 * happening while we have active queues that use PXP 546 */ 547 xe_pm_runtime_get(pxp->xe); 548 549 if (!pxp_prerequisites_done(pxp)) { 550 ret = -EBUSY; 551 goto out; 552 } 553 554 wait_for_idle: 555 /* 556 * if there is an action in progress, wait for it. We need to wait 557 * outside the lock because the completion is done from within the lock. 558 * Note that the two action should never be pending at the same time. 559 */ 560 if (!wait_for_completion_timeout(&pxp->termination, 561 msecs_to_jiffies(PXP_TERMINATION_TIMEOUT_MS))) { 562 ret = -ETIMEDOUT; 563 goto out; 564 } 565 566 if (!wait_for_completion_timeout(&pxp->activation, 567 msecs_to_jiffies(PXP_ACTIVATION_TIMEOUT_MS))) { 568 ret = -ETIMEDOUT; 569 goto out; 570 } 571 572 mutex_lock(&pxp->mutex); 573 574 /* If PXP is not already active, turn it on */ 575 switch (pxp->status) { 576 case XE_PXP_ERROR: 577 ret = -EIO; 578 break; 579 case XE_PXP_ACTIVE: 580 __exec_queue_add(pxp, q); 581 mutex_unlock(&pxp->mutex); 582 goto out; 583 case XE_PXP_READY_TO_START: 584 pxp->status = XE_PXP_START_IN_PROGRESS; 585 reinit_completion(&pxp->activation); 586 break; 587 case XE_PXP_START_IN_PROGRESS: 588 /* If a start is in progress then the completion must not be done */ 589 XE_WARN_ON(completion_done(&pxp->activation)); 590 mutex_unlock(&pxp->mutex); 591 goto wait_for_idle; 592 case XE_PXP_NEEDS_TERMINATION: 593 mark_termination_in_progress(pxp); 594 break; 595 case XE_PXP_TERMINATION_IN_PROGRESS: 596 case XE_PXP_NEEDS_ADDITIONAL_TERMINATION: 597 /* If a termination is in progress then the completion must not be done */ 598 XE_WARN_ON(completion_done(&pxp->termination)); 599 mutex_unlock(&pxp->mutex); 600 goto wait_for_idle; 601 case XE_PXP_SUSPENDED: 602 default: 603 drm_err(&pxp->xe->drm, "unexpected state during PXP start: %u\n", pxp->status); 604 ret = -EIO; 605 break; 606 } 607 608 mutex_unlock(&pxp->mutex); 609 610 if (ret) 611 goto out; 612 613 if (!completion_done(&pxp->termination)) { 614 ret = pxp_terminate_hw(pxp); 615 if (ret) { 616 drm_err(&pxp->xe->drm, "PXP termination failed before start\n"); 617 mutex_lock(&pxp->mutex); 618 pxp->status = XE_PXP_ERROR; 619 mutex_unlock(&pxp->mutex); 620 621 goto out; 622 } 623 624 goto wait_for_idle; 625 } 626 627 /* All the cases except for start should have exited earlier */ 628 XE_WARN_ON(completion_done(&pxp->activation)); 629 ret = __pxp_start_arb_session(pxp); 630 631 mutex_lock(&pxp->mutex); 632 633 complete_all(&pxp->activation); 634 635 /* 636 * Any other process should wait until the state goes away from 637 * XE_PXP_START_IN_PROGRESS, so if the state is not that something went 638 * wrong. Mark the status as needing termination and try again. 639 */ 640 if (pxp->status != XE_PXP_START_IN_PROGRESS) { 641 drm_err(&pxp->xe->drm, "unexpected state after PXP start: %u\n", pxp->status); 642 pxp->status = XE_PXP_NEEDS_TERMINATION; 643 mutex_unlock(&pxp->mutex); 644 goto wait_for_idle; 645 } 646 647 /* If everything went ok, update the status and add the queue to the list */ 648 if (!ret) { 649 pxp->status = XE_PXP_ACTIVE; 650 __exec_queue_add(pxp, q); 651 } else { 652 pxp->status = XE_PXP_ERROR; 653 } 654 655 mutex_unlock(&pxp->mutex); 656 657 out: 658 /* 659 * in the successful case the PM ref is released from 660 * xe_pxp_exec_queue_remove 661 */ 662 if (ret) 663 xe_pm_runtime_put(pxp->xe); 664 665 return ret; 666 } 667 668 /** 669 * xe_pxp_exec_queue_remove - remove a queue from the PXP list 670 * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled) 671 * @q: the queue to remove from the list 672 * 673 * If PXP is enabled and the exec_queue is in the list, the queue will be 674 * removed from the list and its PM reference will be released. It is safe to 675 * call this function multiple times for the same queue. 676 */ 677 void xe_pxp_exec_queue_remove(struct xe_pxp *pxp, struct xe_exec_queue *q) 678 { 679 bool need_pm_put = false; 680 681 if (!xe_pxp_is_enabled(pxp)) 682 return; 683 684 spin_lock_irq(&pxp->queues.lock); 685 686 if (!list_empty(&q->pxp.link)) { 687 list_del_init(&q->pxp.link); 688 need_pm_put = true; 689 } 690 691 q->pxp.type = DRM_XE_PXP_TYPE_NONE; 692 693 spin_unlock_irq(&pxp->queues.lock); 694 695 if (need_pm_put) 696 xe_pm_runtime_put(pxp->xe); 697 } 698 699 static void pxp_invalidate_queues(struct xe_pxp *pxp) 700 { 701 struct xe_exec_queue *tmp, *q; 702 703 spin_lock_irq(&pxp->queues.lock); 704 705 /* 706 * Removing a queue from the PXP list requires a put of the RPM ref that 707 * the queue holds to keep the PXP session alive, which can't be done 708 * under spinlock. Since it is safe to kill a queue multiple times, we 709 * can leave the invalid queue in the list for now and postpone the 710 * removal and associated RPM put to when the queue is destroyed. 711 */ 712 list_for_each_entry(tmp, &pxp->queues.list, pxp.link) { 713 q = xe_exec_queue_get_unless_zero(tmp); 714 715 if (!q) 716 continue; 717 718 xe_exec_queue_kill(q); 719 xe_exec_queue_put(q); 720 } 721 722 spin_unlock_irq(&pxp->queues.lock); 723 } 724 725 /** 726 * xe_pxp_key_assign - mark a BO as using the current PXP key iteration 727 * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled) 728 * @bo: the BO to mark 729 * 730 * Returns: -ENODEV if PXP is disabled, 0 otherwise. 731 */ 732 int xe_pxp_key_assign(struct xe_pxp *pxp, struct xe_bo *bo) 733 { 734 if (!xe_pxp_is_enabled(pxp)) 735 return -ENODEV; 736 737 xe_assert(pxp->xe, !bo->pxp_key_instance); 738 739 /* 740 * Note that the PXP key handling is inherently racey, because the key 741 * can theoretically change at any time (although it's unlikely to do 742 * so without triggers), even right after we copy it. Taking a lock 743 * wouldn't help because the value might still change as soon as we 744 * release the lock. 745 * Userspace needs to handle the fact that their BOs can go invalid at 746 * any point. 747 */ 748 bo->pxp_key_instance = pxp->key_instance; 749 750 return 0; 751 } 752 753 /** 754 * xe_pxp_bo_key_check - check if the key used by a xe_bo is valid 755 * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled) 756 * @bo: the BO we want to check 757 * 758 * Checks whether a BO was encrypted with the current key or an obsolete one. 759 * 760 * Returns: 0 if the key is valid, -ENODEV if PXP is disabled, -EINVAL if the 761 * BO is not using PXP, -ENOEXEC if the key is not valid. 762 */ 763 int xe_pxp_bo_key_check(struct xe_pxp *pxp, struct xe_bo *bo) 764 { 765 if (!xe_pxp_is_enabled(pxp)) 766 return -ENODEV; 767 768 if (!xe_bo_is_protected(bo)) 769 return -EINVAL; 770 771 xe_assert(pxp->xe, bo->pxp_key_instance); 772 773 /* 774 * Note that the PXP key handling is inherently racey, because the key 775 * can theoretically change at any time (although it's unlikely to do 776 * so without triggers), even right after we check it. Taking a lock 777 * wouldn't help because the value might still change as soon as we 778 * release the lock. 779 * We mitigate the risk by checking the key at multiple points (on each 780 * submission involving the BO and right before flipping it on the 781 * display), but there is still a very small chance that we could 782 * operate on an invalid BO for a single submission or a single frame 783 * flip. This is a compromise made to protect the encrypted data (which 784 * is what the key termination is for). 785 */ 786 if (bo->pxp_key_instance != pxp->key_instance) 787 return -ENOEXEC; 788 789 return 0; 790 } 791 792 /** 793 * xe_pxp_obj_key_check - check if the key used by a drm_gem_obj is valid 794 * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled) 795 * @obj: the drm_gem_obj we want to check 796 * 797 * Checks whether a drm_gem_obj was encrypted with the current key or an 798 * obsolete one. 799 * 800 * Returns: 0 if the key is valid, -ENODEV if PXP is disabled, -EINVAL if the 801 * obj is not using PXP, -ENOEXEC if the key is not valid. 802 */ 803 int xe_pxp_obj_key_check(struct xe_pxp *pxp, struct drm_gem_object *obj) 804 { 805 return xe_pxp_bo_key_check(pxp, gem_to_xe_bo(obj)); 806 } 807 808 /** 809 * xe_pxp_pm_suspend - prepare PXP for HW suspend 810 * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled) 811 * 812 * Makes sure all PXP actions have completed and invalidates all PXP queues 813 * and objects before we go into a suspend state. 814 * 815 * Returns: 0 if successful, a negative errno value otherwise. 816 */ 817 int xe_pxp_pm_suspend(struct xe_pxp *pxp) 818 { 819 int ret = 0; 820 821 if (!xe_pxp_is_enabled(pxp)) 822 return 0; 823 824 wait_for_activation: 825 if (!wait_for_completion_timeout(&pxp->activation, 826 msecs_to_jiffies(PXP_ACTIVATION_TIMEOUT_MS))) 827 ret = -ETIMEDOUT; 828 829 mutex_lock(&pxp->mutex); 830 831 switch (pxp->status) { 832 case XE_PXP_ERROR: 833 case XE_PXP_READY_TO_START: 834 case XE_PXP_SUSPENDED: 835 case XE_PXP_TERMINATION_IN_PROGRESS: 836 case XE_PXP_NEEDS_ADDITIONAL_TERMINATION: 837 /* 838 * If PXP is not running there is nothing to cleanup. If there 839 * is a termination pending then no need to issue another one. 840 */ 841 break; 842 case XE_PXP_START_IN_PROGRESS: 843 mutex_unlock(&pxp->mutex); 844 goto wait_for_activation; 845 case XE_PXP_NEEDS_TERMINATION: 846 /* If PXP was never used we can skip the cleanup */ 847 if (pxp->key_instance == pxp->last_suspend_key_instance) 848 break; 849 fallthrough; 850 case XE_PXP_ACTIVE: 851 pxp_invalidate_state(pxp); 852 break; 853 default: 854 drm_err(&pxp->xe->drm, "unexpected state during PXP suspend: %u", 855 pxp->status); 856 ret = -EIO; 857 goto out; 858 } 859 860 /* 861 * We set this even if we were in error state, hoping the suspend clears 862 * the error. Worse case we fail again and go in error state again. 863 */ 864 pxp->status = XE_PXP_SUSPENDED; 865 866 mutex_unlock(&pxp->mutex); 867 868 /* 869 * if there is a termination in progress, wait for it. 870 * We need to wait outside the lock because the completion is done from 871 * within the lock 872 */ 873 if (!wait_for_completion_timeout(&pxp->termination, 874 msecs_to_jiffies(PXP_TERMINATION_TIMEOUT_MS))) 875 ret = -ETIMEDOUT; 876 877 pxp->last_suspend_key_instance = pxp->key_instance; 878 879 out: 880 return ret; 881 } 882 883 /** 884 * xe_pxp_pm_resume - re-init PXP after HW suspend 885 * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled) 886 */ 887 void xe_pxp_pm_resume(struct xe_pxp *pxp) 888 { 889 int err; 890 891 if (!xe_pxp_is_enabled(pxp)) 892 return; 893 894 err = kcr_pxp_enable(pxp); 895 896 mutex_lock(&pxp->mutex); 897 898 xe_assert(pxp->xe, pxp->status == XE_PXP_SUSPENDED); 899 900 if (err) 901 pxp->status = XE_PXP_ERROR; 902 else 903 pxp->status = XE_PXP_NEEDS_TERMINATION; 904 905 mutex_unlock(&pxp->mutex); 906 } 907