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