1 // SPDX-License-Identifier: MIT
3 * Copyright(c) 2020, Intel Corporation. All rights reserved.
9 #include "intel_pxp_cmd.h"
10 #include "intel_pxp_gsccs.h"
11 #include "intel_pxp_session.h"
12 #include "intel_pxp_tee.h"
13 #include "intel_pxp_types.h"
14 #include "intel_pxp_regs.h"
16 #define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */
18 static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
20 struct intel_uncore *uncore = pxp->ctrl_gt->uncore;
21 intel_wakeref_t wakeref;
24 /* if we're suspended the session is considered off */
25 with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref)
26 sip = intel_uncore_read(uncore, KCR_SIP(pxp->kcr_base));
31 static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play)
33 struct intel_uncore *uncore = pxp->ctrl_gt->uncore;
34 intel_wakeref_t wakeref;
38 /* if we're suspended the session is considered off */
39 wakeref = intel_runtime_pm_get_if_in_use(uncore->rpm);
41 return in_play ? -ENODEV : 0;
43 ret = intel_wait_for_register(uncore,
44 KCR_SIP(pxp->kcr_base),
49 intel_runtime_pm_put(uncore->rpm, wakeref);
54 static int pxp_create_arb_session(struct intel_pxp *pxp)
56 struct intel_gt *gt = pxp->ctrl_gt;
59 pxp->arb_is_valid = false;
61 if (intel_pxp_session_is_in_play(pxp, ARB_SESSION)) {
62 drm_err(>->i915->drm, "arb session already in play at creation time\n");
66 if (HAS_ENGINE(pxp->ctrl_gt, GSC0))
67 ret = intel_pxp_gsccs_create_session(pxp, ARB_SESSION);
69 ret = intel_pxp_tee_cmd_create_arb_session(pxp, ARB_SESSION);
71 drm_err(>->i915->drm, "tee cmd for arb session creation failed\n");
75 ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true);
77 drm_dbg(>->i915->drm, "arb session failed to go in play\n");
80 drm_dbg(>->i915->drm, "PXP ARB session is alive\n");
82 if (!++pxp->key_instance)
85 pxp->arb_is_valid = true;
90 static int pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)
93 struct intel_gt *gt = pxp->ctrl_gt;
95 /* must mark termination in progress calling this function */
96 GEM_WARN_ON(pxp->arb_is_valid);
98 /* terminate the hw sessions */
99 ret = intel_pxp_terminate_session(pxp, ARB_SESSION);
101 drm_err(>->i915->drm, "Failed to submit session termination\n");
105 ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false);
107 drm_err(>->i915->drm, "Session state did not clear\n");
111 intel_uncore_write(gt->uncore, KCR_GLOBAL_TERMINATE(pxp->kcr_base), 1);
113 if (HAS_ENGINE(gt, GSC0))
114 intel_pxp_gsccs_end_arb_fw_session(pxp, ARB_SESSION);
116 intel_pxp_tee_end_arb_fw_session(pxp, ARB_SESSION);
121 void intel_pxp_terminate(struct intel_pxp *pxp, bool post_invalidation_needs_restart)
125 pxp->hw_state_invalidated = post_invalidation_needs_restart;
128 * if we fail to submit the termination there is no point in waiting for
129 * it to complete. PXP will be marked as non-active until the next
130 * termination is issued.
132 ret = pxp_terminate_arb_session_and_global(pxp);
134 complete_all(&pxp->termination);
137 static void pxp_terminate_complete(struct intel_pxp *pxp)
139 /* Re-create the arb session after teardown handle complete */
140 if (fetch_and_zero(&pxp->hw_state_invalidated)) {
141 drm_dbg(&pxp->ctrl_gt->i915->drm, "PXP: creating arb_session after invalidation");
142 pxp_create_arb_session(pxp);
145 complete_all(&pxp->termination);
148 static void pxp_session_work(struct work_struct *work)
150 struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
151 struct intel_gt *gt = pxp->ctrl_gt;
152 intel_wakeref_t wakeref;
155 spin_lock_irq(gt->irq_lock);
156 events = fetch_and_zero(&pxp->session_events);
157 spin_unlock_irq(gt->irq_lock);
162 drm_dbg(>->i915->drm, "PXP: processing event-flags 0x%08x", events);
164 if (events & PXP_INVAL_REQUIRED)
165 intel_pxp_invalidate(pxp);
168 * If we're processing an event while suspending then don't bother,
169 * we're going to re-init everything on resume anyway.
171 wakeref = intel_runtime_pm_get_if_in_use(gt->uncore->rpm);
175 if (events & PXP_TERMINATION_REQUEST) {
176 events &= ~PXP_TERMINATION_COMPLETE;
177 intel_pxp_terminate(pxp, true);
180 if (events & PXP_TERMINATION_COMPLETE)
181 pxp_terminate_complete(pxp);
183 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
186 void intel_pxp_session_management_init(struct intel_pxp *pxp)
188 mutex_init(&pxp->arb_mutex);
189 INIT_WORK(&pxp->session_work, pxp_session_work);