1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright(c) 2020 Intel Corporation. 4 */ 5 #include <linux/workqueue.h> 6 7 #include "gem/i915_gem_context.h" 8 9 #include "gt/intel_context.h" 10 #include "gt/intel_gt.h" 11 12 #include "i915_drv.h" 13 14 #include "intel_pxp.h" 15 #include "intel_pxp_gsccs.h" 16 #include "intel_pxp_irq.h" 17 #include "intel_pxp_regs.h" 18 #include "intel_pxp_session.h" 19 #include "intel_pxp_tee.h" 20 #include "intel_pxp_types.h" 21 22 /** 23 * DOC: PXP 24 * 25 * PXP (Protected Xe Path) is a feature available in Gen12 and newer platforms. 26 * It allows execution and flip to display of protected (i.e. encrypted) 27 * objects. The SW support is enabled via the CONFIG_DRM_I915_PXP kconfig. 28 * 29 * Objects can opt-in to PXP encryption at creation time via the 30 * I915_GEM_CREATE_EXT_PROTECTED_CONTENT create_ext flag. For objects to be 31 * correctly protected they must be used in conjunction with a context created 32 * with the I915_CONTEXT_PARAM_PROTECTED_CONTENT flag. See the documentation 33 * of those two uapi flags for details and restrictions. 34 * 35 * Protected objects are tied to a pxp session; currently we only support one 36 * session, which i915 manages and whose index is available in the uapi 37 * (I915_PROTECTED_CONTENT_DEFAULT_SESSION) for use in instructions targeting 38 * protected objects. 39 * The session is invalidated by the HW when certain events occur (e.g. 40 * suspend/resume). When this happens, all the objects that were used with the 41 * session are marked as invalid and all contexts marked as using protected 42 * content are banned. Any further attempt at using them in an execbuf call is 43 * rejected, while flips are converted to black frames. 44 * 45 * Some of the PXP setup operations are performed by the Management Engine, 46 * which is handled by the mei driver; communication between i915 and mei is 47 * performed via the mei_pxp component module. 48 */ 49 50 bool intel_pxp_is_supported(const struct intel_pxp *pxp) 51 { 52 return IS_ENABLED(CONFIG_DRM_I915_PXP) && pxp; 53 } 54 55 bool intel_pxp_is_enabled(const struct intel_pxp *pxp) 56 { 57 return IS_ENABLED(CONFIG_DRM_I915_PXP) && pxp && pxp->ce; 58 } 59 60 bool intel_pxp_is_active(const struct intel_pxp *pxp) 61 { 62 return IS_ENABLED(CONFIG_DRM_I915_PXP) && pxp && pxp->arb_is_valid; 63 } 64 65 static void kcr_pxp_set_status(const struct intel_pxp *pxp, bool enable) 66 { 67 u32 val = enable ? _MASKED_BIT_ENABLE(KCR_INIT_ALLOW_DISPLAY_ME_WRITES) : 68 _MASKED_BIT_DISABLE(KCR_INIT_ALLOW_DISPLAY_ME_WRITES); 69 70 intel_uncore_write(pxp->ctrl_gt->uncore, KCR_INIT(pxp->kcr_base), val); 71 } 72 73 static void kcr_pxp_enable(const struct intel_pxp *pxp) 74 { 75 kcr_pxp_set_status(pxp, true); 76 } 77 78 static void kcr_pxp_disable(const struct intel_pxp *pxp) 79 { 80 kcr_pxp_set_status(pxp, false); 81 } 82 83 static int create_vcs_context(struct intel_pxp *pxp) 84 { 85 static struct lock_class_key pxp_lock; 86 struct intel_gt *gt = pxp->ctrl_gt; 87 struct intel_engine_cs *engine; 88 struct intel_context *ce; 89 int i; 90 91 /* 92 * Find the first VCS engine present. We're guaranteed there is one 93 * if we're in this function due to the check in has_pxp 94 */ 95 for (i = 0, engine = NULL; !engine; i++) 96 engine = gt->engine_class[VIDEO_DECODE_CLASS][i]; 97 98 GEM_BUG_ON(!engine || engine->class != VIDEO_DECODE_CLASS); 99 100 ce = intel_engine_create_pinned_context(engine, engine->gt->vm, SZ_4K, 101 I915_GEM_HWS_PXP_ADDR, 102 &pxp_lock, "pxp_context"); 103 if (IS_ERR(ce)) { 104 drm_err(>->i915->drm, "failed to create VCS ctx for PXP\n"); 105 return PTR_ERR(ce); 106 } 107 108 pxp->ce = ce; 109 110 return 0; 111 } 112 113 static void destroy_vcs_context(struct intel_pxp *pxp) 114 { 115 if (pxp->ce) 116 intel_engine_destroy_pinned_context(fetch_and_zero(&pxp->ce)); 117 } 118 119 static void pxp_init_full(struct intel_pxp *pxp) 120 { 121 struct intel_gt *gt = pxp->ctrl_gt; 122 int ret; 123 124 /* 125 * we'll use the completion to check if there is a termination pending, 126 * so we start it as completed and we reinit it when a termination 127 * is triggered. 128 */ 129 init_completion(&pxp->termination); 130 complete_all(&pxp->termination); 131 132 if (pxp->ctrl_gt->type == GT_MEDIA) 133 pxp->kcr_base = MTL_KCR_BASE; 134 else 135 pxp->kcr_base = GEN12_KCR_BASE; 136 137 intel_pxp_session_management_init(pxp); 138 139 ret = create_vcs_context(pxp); 140 if (ret) 141 return; 142 143 if (HAS_ENGINE(pxp->ctrl_gt, GSC0)) 144 ret = intel_pxp_gsccs_init(pxp); 145 else 146 ret = intel_pxp_tee_component_init(pxp); 147 if (ret) 148 goto out_context; 149 150 drm_info(>->i915->drm, "Protected Xe Path (PXP) protected content support initialized\n"); 151 152 return; 153 154 out_context: 155 destroy_vcs_context(pxp); 156 } 157 158 static struct intel_gt *find_gt_for_required_teelink(struct drm_i915_private *i915) 159 { 160 /* 161 * NOTE: Only certain platforms require PXP-tee-backend dependencies 162 * for HuC authentication. For now, its limited to DG2. 163 */ 164 if (IS_ENABLED(CONFIG_INTEL_MEI_PXP) && IS_ENABLED(CONFIG_INTEL_MEI_GSC) && 165 intel_huc_is_loaded_by_gsc(&to_gt(i915)->uc.huc) && intel_uc_uses_huc(&to_gt(i915)->uc)) 166 return to_gt(i915); 167 168 return NULL; 169 } 170 171 static struct intel_gt *find_gt_for_required_protected_content(struct drm_i915_private *i915) 172 { 173 if (!IS_ENABLED(CONFIG_DRM_I915_PXP) || !INTEL_INFO(i915)->has_pxp) 174 return NULL; 175 176 /* 177 * For MTL onwards, PXP-controller-GT needs to have a valid GSC engine 178 * on the media GT. NOTE: if we have a media-tile with a GSC-engine, 179 * the VDBOX is already present so skip that check. We also have to 180 * ensure the GSC and HUC firmware are coming online 181 */ 182 if (i915->media_gt && HAS_ENGINE(i915->media_gt, GSC0) && 183 intel_uc_fw_is_loadable(&i915->media_gt->uc.gsc.fw) && 184 intel_uc_fw_is_loadable(&i915->media_gt->uc.huc.fw)) 185 return i915->media_gt; 186 187 /* 188 * Else we rely on mei-pxp module but only on legacy platforms 189 * prior to having separate media GTs and has a valid VDBOX. 190 */ 191 if (IS_ENABLED(CONFIG_INTEL_MEI_PXP) && !i915->media_gt && VDBOX_MASK(to_gt(i915))) 192 return to_gt(i915); 193 194 return NULL; 195 } 196 197 int intel_pxp_init(struct drm_i915_private *i915) 198 { 199 struct intel_gt *gt; 200 bool is_full_feature = false; 201 202 /* 203 * NOTE: Get the ctrl_gt before checking intel_pxp_is_supported since 204 * we still need it if PXP's backend tee transport is needed. 205 */ 206 gt = find_gt_for_required_protected_content(i915); 207 if (gt) 208 is_full_feature = true; 209 else 210 gt = find_gt_for_required_teelink(i915); 211 212 if (!gt) 213 return -ENODEV; 214 215 /* 216 * At this point, we will either enable full featured PXP capabilities 217 * including session and object management, or we will init the backend tee 218 * channel for internal users such as HuC loading by GSC 219 */ 220 i915->pxp = kzalloc(sizeof(*i915->pxp), GFP_KERNEL); 221 if (!i915->pxp) 222 return -ENOMEM; 223 224 /* init common info used by all feature-mode usages*/ 225 i915->pxp->ctrl_gt = gt; 226 mutex_init(&i915->pxp->tee_mutex); 227 228 /* 229 * If full PXP feature is not available but HuC is loaded by GSC on pre-MTL 230 * such as DG2, we can skip the init of the full PXP session/object management 231 * and just init the tee channel. 232 */ 233 if (is_full_feature) 234 pxp_init_full(i915->pxp); 235 else 236 intel_pxp_tee_component_init(i915->pxp); 237 238 return 0; 239 } 240 241 void intel_pxp_fini(struct drm_i915_private *i915) 242 { 243 if (!i915->pxp) 244 return; 245 246 i915->pxp->arb_is_valid = false; 247 248 if (HAS_ENGINE(i915->pxp->ctrl_gt, GSC0)) 249 intel_pxp_gsccs_fini(i915->pxp); 250 else 251 intel_pxp_tee_component_fini(i915->pxp); 252 253 destroy_vcs_context(i915->pxp); 254 255 kfree(i915->pxp); 256 i915->pxp = NULL; 257 } 258 259 void intel_pxp_mark_termination_in_progress(struct intel_pxp *pxp) 260 { 261 pxp->arb_is_valid = false; 262 reinit_completion(&pxp->termination); 263 } 264 265 static void pxp_queue_termination(struct intel_pxp *pxp) 266 { 267 struct intel_gt *gt = pxp->ctrl_gt; 268 269 /* 270 * We want to get the same effect as if we received a termination 271 * interrupt, so just pretend that we did. 272 */ 273 spin_lock_irq(gt->irq_lock); 274 intel_pxp_mark_termination_in_progress(pxp); 275 pxp->session_events |= PXP_TERMINATION_REQUEST; 276 queue_work(system_unbound_wq, &pxp->session_work); 277 spin_unlock_irq(gt->irq_lock); 278 } 279 280 static bool pxp_component_bound(struct intel_pxp *pxp) 281 { 282 bool bound = false; 283 284 mutex_lock(&pxp->tee_mutex); 285 if (pxp->pxp_component) 286 bound = true; 287 mutex_unlock(&pxp->tee_mutex); 288 289 return bound; 290 } 291 292 int intel_pxp_get_backend_timeout_ms(struct intel_pxp *pxp) 293 { 294 if (HAS_ENGINE(pxp->ctrl_gt, GSC0)) 295 return GSCFW_MAX_ROUND_TRIP_LATENCY_MS; 296 else 297 return 250; 298 } 299 300 static int __pxp_global_teardown_final(struct intel_pxp *pxp) 301 { 302 int timeout; 303 304 if (!pxp->arb_is_valid) 305 return 0; 306 /* 307 * To ensure synchronous and coherent session teardown completion 308 * in response to suspend or shutdown triggers, don't use a worker. 309 */ 310 intel_pxp_mark_termination_in_progress(pxp); 311 intel_pxp_terminate(pxp, false); 312 313 timeout = intel_pxp_get_backend_timeout_ms(pxp); 314 315 if (!wait_for_completion_timeout(&pxp->termination, msecs_to_jiffies(timeout))) 316 return -ETIMEDOUT; 317 318 return 0; 319 } 320 321 static int __pxp_global_teardown_restart(struct intel_pxp *pxp) 322 { 323 int timeout; 324 325 if (pxp->arb_is_valid) 326 return 0; 327 /* 328 * The arb-session is currently inactive and we are doing a reset and restart 329 * due to a runtime event. Use the worker that was designed for this. 330 */ 331 pxp_queue_termination(pxp); 332 333 timeout = intel_pxp_get_backend_timeout_ms(pxp); 334 335 if (!wait_for_completion_timeout(&pxp->termination, msecs_to_jiffies(timeout))) 336 return -ETIMEDOUT; 337 338 return 0; 339 } 340 341 void intel_pxp_end(struct intel_pxp *pxp) 342 { 343 struct drm_i915_private *i915 = pxp->ctrl_gt->i915; 344 intel_wakeref_t wakeref; 345 346 if (!intel_pxp_is_enabled(pxp)) 347 return; 348 349 wakeref = intel_runtime_pm_get(&i915->runtime_pm); 350 351 mutex_lock(&pxp->arb_mutex); 352 353 if (__pxp_global_teardown_final(pxp)) 354 drm_dbg(&i915->drm, "PXP end timed out\n"); 355 356 mutex_unlock(&pxp->arb_mutex); 357 358 intel_pxp_fini_hw(pxp); 359 intel_runtime_pm_put(&i915->runtime_pm, wakeref); 360 } 361 362 static bool pxp_required_fw_failed(struct intel_pxp *pxp) 363 { 364 if (__intel_uc_fw_status(&pxp->ctrl_gt->uc.huc.fw) == INTEL_UC_FIRMWARE_LOAD_FAIL) 365 return true; 366 if (HAS_ENGINE(pxp->ctrl_gt, GSC0) && 367 __intel_uc_fw_status(&pxp->ctrl_gt->uc.gsc.fw) == INTEL_UC_FIRMWARE_LOAD_FAIL) 368 return true; 369 370 return false; 371 } 372 373 static bool pxp_fw_dependencies_completed(struct intel_pxp *pxp) 374 { 375 if (HAS_ENGINE(pxp->ctrl_gt, GSC0)) 376 return intel_pxp_gsccs_is_ready_for_sessions(pxp); 377 378 return pxp_component_bound(pxp); 379 } 380 381 /* 382 * this helper is used by both intel_pxp_start and by 383 * the GET_PARAM IOCTL that user space calls. Thus, the 384 * return values here should match the UAPI spec. 385 */ 386 int intel_pxp_get_readiness_status(struct intel_pxp *pxp, int timeout_ms) 387 { 388 if (!intel_pxp_is_enabled(pxp)) 389 return -ENODEV; 390 391 if (pxp_required_fw_failed(pxp)) 392 return -ENODEV; 393 394 if (pxp->platform_cfg_is_bad) 395 return -ENODEV; 396 397 if (timeout_ms) { 398 if (wait_for(pxp_fw_dependencies_completed(pxp), timeout_ms)) 399 return 2; 400 } else if (!pxp_fw_dependencies_completed(pxp)) { 401 return 2; 402 } 403 return 1; 404 } 405 406 /* 407 * the arb session is restarted from the irq work when we receive the 408 * termination completion interrupt 409 */ 410 #define PXP_READINESS_TIMEOUT 250 411 412 int intel_pxp_start(struct intel_pxp *pxp) 413 { 414 int ret = 0; 415 416 ret = intel_pxp_get_readiness_status(pxp, PXP_READINESS_TIMEOUT); 417 if (ret < 0) 418 return ret; 419 else if (ret > 1) 420 return -EIO; /* per UAPI spec, user may retry later */ 421 422 mutex_lock(&pxp->arb_mutex); 423 424 ret = __pxp_global_teardown_restart(pxp); 425 if (ret) 426 goto unlock; 427 428 /* make sure the compiler doesn't optimize the double access */ 429 barrier(); 430 431 if (!pxp->arb_is_valid) 432 ret = -EIO; 433 434 unlock: 435 mutex_unlock(&pxp->arb_mutex); 436 return ret; 437 } 438 439 void intel_pxp_init_hw(struct intel_pxp *pxp) 440 { 441 kcr_pxp_enable(pxp); 442 intel_pxp_irq_enable(pxp); 443 } 444 445 void intel_pxp_fini_hw(struct intel_pxp *pxp) 446 { 447 kcr_pxp_disable(pxp); 448 intel_pxp_irq_disable(pxp); 449 } 450 451 int intel_pxp_key_check(struct intel_pxp *pxp, 452 struct drm_i915_gem_object *obj, 453 bool assign) 454 { 455 if (!intel_pxp_is_active(pxp)) 456 return -ENODEV; 457 458 if (!i915_gem_object_is_protected(obj)) 459 return -EINVAL; 460 461 GEM_BUG_ON(!pxp->key_instance); 462 463 /* 464 * If this is the first time we're using this object, it's not 465 * encrypted yet; it will be encrypted with the current key, so mark it 466 * as such. If the object is already encrypted, check instead if the 467 * used key is still valid. 468 */ 469 if (!obj->pxp_key_instance && assign) 470 obj->pxp_key_instance = pxp->key_instance; 471 472 if (obj->pxp_key_instance != pxp->key_instance) 473 return -ENOEXEC; 474 475 return 0; 476 } 477 478 void intel_pxp_invalidate(struct intel_pxp *pxp) 479 { 480 struct drm_i915_private *i915 = pxp->ctrl_gt->i915; 481 struct i915_gem_context *ctx, *cn; 482 483 /* ban all contexts marked as protected */ 484 spin_lock_irq(&i915->gem.contexts.lock); 485 list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) { 486 struct i915_gem_engines_iter it; 487 struct intel_context *ce; 488 489 if (!kref_get_unless_zero(&ctx->ref)) 490 continue; 491 492 if (likely(!i915_gem_context_uses_protected_content(ctx))) { 493 i915_gem_context_put(ctx); 494 continue; 495 } 496 497 spin_unlock_irq(&i915->gem.contexts.lock); 498 499 /* 500 * By the time we get here we are either going to suspend with 501 * quiesced execution or the HW keys are already long gone and 502 * in this case it is worthless to attempt to close the context 503 * and wait for its execution. It will hang the GPU if it has 504 * not already. So, as a fast mitigation, we can ban the 505 * context as quick as we can. That might race with the 506 * execbuffer, but currently this is the best that can be done. 507 */ 508 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) 509 intel_context_ban(ce, NULL); 510 i915_gem_context_unlock_engines(ctx); 511 512 /* 513 * The context has been banned, no need to keep the wakeref. 514 * This is safe from races because the only other place this 515 * is touched is context_release and we're holding a ctx ref 516 */ 517 if (ctx->pxp_wakeref) { 518 intel_runtime_pm_put(&i915->runtime_pm, 519 ctx->pxp_wakeref); 520 ctx->pxp_wakeref = 0; 521 } 522 523 spin_lock_irq(&i915->gem.contexts.lock); 524 list_safe_reset_next(ctx, cn, link); 525 i915_gem_context_put(ctx); 526 } 527 spin_unlock_irq(&i915->gem.contexts.lock); 528 } 529