1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright(c) 2020, Intel Corporation. All rights reserved. 4 */ 5 6 #include "i915_drv.h" 7 8 #include "intel_pxp.h" 9 #include "intel_pxp_cmd.h" 10 #include "intel_pxp_session.h" 11 #include "intel_pxp_tee.h" 12 #include "intel_pxp_types.h" 13 14 #define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */ 15 16 #define GEN12_KCR_SIP _MMIO(0x32260) /* KCR hwdrm session in play 0-31 */ 17 18 /* PXP global terminate register for session termination */ 19 #define PXP_GLOBAL_TERMINATE _MMIO(0x320f8) 20 21 static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id) 22 { 23 struct intel_uncore *uncore = pxp->ctrl_gt->uncore; 24 intel_wakeref_t wakeref; 25 u32 sip = 0; 26 27 /* if we're suspended the session is considered off */ 28 with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref) 29 sip = intel_uncore_read(uncore, GEN12_KCR_SIP); 30 31 return sip & BIT(id); 32 } 33 34 static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play) 35 { 36 struct intel_uncore *uncore = pxp->ctrl_gt->uncore; 37 intel_wakeref_t wakeref; 38 u32 mask = BIT(id); 39 int ret; 40 41 /* if we're suspended the session is considered off */ 42 wakeref = intel_runtime_pm_get_if_in_use(uncore->rpm); 43 if (!wakeref) 44 return in_play ? -ENODEV : 0; 45 46 ret = intel_wait_for_register(uncore, 47 GEN12_KCR_SIP, 48 mask, 49 in_play ? mask : 0, 50 100); 51 52 intel_runtime_pm_put(uncore->rpm, wakeref); 53 54 return ret; 55 } 56 57 static int pxp_create_arb_session(struct intel_pxp *pxp) 58 { 59 struct intel_gt *gt = pxp->ctrl_gt; 60 int ret; 61 62 pxp->arb_is_valid = false; 63 64 if (intel_pxp_session_is_in_play(pxp, ARB_SESSION)) { 65 drm_err(>->i915->drm, "arb session already in play at creation time\n"); 66 return -EEXIST; 67 } 68 69 ret = intel_pxp_tee_cmd_create_arb_session(pxp, ARB_SESSION); 70 if (ret) { 71 drm_err(>->i915->drm, "tee cmd for arb session creation failed\n"); 72 return ret; 73 } 74 75 ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true); 76 if (ret) { 77 drm_dbg(>->i915->drm, "arb session failed to go in play\n"); 78 return ret; 79 } 80 drm_dbg(>->i915->drm, "PXP ARB session is alive\n"); 81 82 if (!++pxp->key_instance) 83 ++pxp->key_instance; 84 85 pxp->arb_is_valid = true; 86 87 return 0; 88 } 89 90 static int pxp_terminate_arb_session_and_global(struct intel_pxp *pxp) 91 { 92 int ret; 93 struct intel_gt *gt = pxp->ctrl_gt; 94 95 /* must mark termination in progress calling this function */ 96 GEM_WARN_ON(pxp->arb_is_valid); 97 98 /* terminate the hw sessions */ 99 ret = intel_pxp_terminate_session(pxp, ARB_SESSION); 100 if (ret) { 101 drm_err(>->i915->drm, "Failed to submit session termination\n"); 102 return ret; 103 } 104 105 ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false); 106 if (ret) { 107 drm_err(>->i915->drm, "Session state did not clear\n"); 108 return ret; 109 } 110 111 intel_uncore_write(gt->uncore, PXP_GLOBAL_TERMINATE, 1); 112 113 intel_pxp_tee_end_arb_fw_session(pxp, ARB_SESSION); 114 115 return ret; 116 } 117 118 void intel_pxp_terminate(struct intel_pxp *pxp, bool post_invalidation_needs_restart) 119 { 120 int ret; 121 122 pxp->hw_state_invalidated = post_invalidation_needs_restart; 123 124 /* 125 * if we fail to submit the termination there is no point in waiting for 126 * it to complete. PXP will be marked as non-active until the next 127 * termination is issued. 128 */ 129 ret = pxp_terminate_arb_session_and_global(pxp); 130 if (ret) 131 complete_all(&pxp->termination); 132 } 133 134 static void pxp_terminate_complete(struct intel_pxp *pxp) 135 { 136 /* Re-create the arb session after teardown handle complete */ 137 if (fetch_and_zero(&pxp->hw_state_invalidated)) 138 pxp_create_arb_session(pxp); 139 140 complete_all(&pxp->termination); 141 } 142 143 static void pxp_session_work(struct work_struct *work) 144 { 145 struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work); 146 struct intel_gt *gt = pxp->ctrl_gt; 147 intel_wakeref_t wakeref; 148 u32 events = 0; 149 150 spin_lock_irq(gt->irq_lock); 151 events = fetch_and_zero(&pxp->session_events); 152 spin_unlock_irq(gt->irq_lock); 153 154 if (!events) 155 return; 156 157 if (events & PXP_INVAL_REQUIRED) 158 intel_pxp_invalidate(pxp); 159 160 /* 161 * If we're processing an event while suspending then don't bother, 162 * we're going to re-init everything on resume anyway. 163 */ 164 wakeref = intel_runtime_pm_get_if_in_use(gt->uncore->rpm); 165 if (!wakeref) 166 return; 167 168 if (events & PXP_TERMINATION_REQUEST) { 169 events &= ~PXP_TERMINATION_COMPLETE; 170 intel_pxp_terminate(pxp, true); 171 } 172 173 if (events & PXP_TERMINATION_COMPLETE) 174 pxp_terminate_complete(pxp); 175 176 intel_runtime_pm_put(gt->uncore->rpm, wakeref); 177 } 178 179 void intel_pxp_session_management_init(struct intel_pxp *pxp) 180 { 181 mutex_init(&pxp->arb_mutex); 182 INIT_WORK(&pxp->session_work, pxp_session_work); 183 } 184