1 // SPDX-License-Identifier: MIT
3 * Copyright(c) 2020, Intel Corporation. All rights reserved.
9 #include "intel_pxp_cmd.h"
10 #include "intel_pxp_session.h"
11 #include "intel_pxp_tee.h"
12 #include "intel_pxp_types.h"
14 #define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */
16 #define GEN12_KCR_SIP _MMIO(0x32260) /* KCR hwdrm session in play 0-31 */
18 /* PXP global terminate register for session termination */
19 #define PXP_GLOBAL_TERMINATE _MMIO(0x320f8)
21 static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
23 struct intel_uncore *uncore = pxp_to_gt(pxp)->uncore;
24 intel_wakeref_t wakeref;
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);
34 static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play)
36 struct intel_uncore *uncore = pxp_to_gt(pxp)->uncore;
37 intel_wakeref_t wakeref;
41 /* if we're suspended the session is considered off */
42 wakeref = intel_runtime_pm_get_if_in_use(uncore->rpm);
44 return in_play ? -ENODEV : 0;
46 ret = intel_wait_for_register(uncore,
52 intel_runtime_pm_put(uncore->rpm, wakeref);
57 static int pxp_create_arb_session(struct intel_pxp *pxp)
59 struct intel_gt *gt = pxp_to_gt(pxp);
62 pxp->arb_is_valid = false;
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");
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_err(>->i915->drm, "arb session failed to go in play\n");
81 if (!++pxp->key_instance)
84 pxp->arb_is_valid = true;
89 static int pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)
92 struct intel_gt *gt = pxp_to_gt(pxp);
94 /* must mark termination in progress calling this function */
95 GEM_WARN_ON(pxp->arb_is_valid);
97 /* terminate the hw sessions */
98 ret = intel_pxp_terminate_session(pxp, ARB_SESSION);
100 drm_err(>->i915->drm, "Failed to submit session termination\n");
104 ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false);
106 drm_err(>->i915->drm, "Session state did not clear\n");
110 intel_uncore_write(gt->uncore, PXP_GLOBAL_TERMINATE, 1);
115 static void pxp_terminate(struct intel_pxp *pxp)
119 pxp->hw_state_invalidated = true;
122 * if we fail to submit the termination there is no point in waiting for
123 * it to complete. PXP will be marked as non-active until the next
124 * termination is issued.
126 ret = pxp_terminate_arb_session_and_global(pxp);
128 complete_all(&pxp->termination);
131 static void pxp_terminate_complete(struct intel_pxp *pxp)
133 /* Re-create the arb session after teardown handle complete */
134 if (fetch_and_zero(&pxp->hw_state_invalidated))
135 pxp_create_arb_session(pxp);
137 complete_all(&pxp->termination);
140 void intel_pxp_session_work(struct work_struct *work)
142 struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
143 struct intel_gt *gt = pxp_to_gt(pxp);
144 intel_wakeref_t wakeref;
147 spin_lock_irq(>->irq_lock);
148 events = fetch_and_zero(&pxp->session_events);
149 spin_unlock_irq(>->irq_lock);
154 if (events & PXP_INVAL_REQUIRED)
155 intel_pxp_invalidate(pxp);
158 * If we're processing an event while suspending then don't bother,
159 * we're going to re-init everything on resume anyway.
161 wakeref = intel_runtime_pm_get_if_in_use(gt->uncore->rpm);
165 if (events & PXP_TERMINATION_REQUEST) {
166 events &= ~PXP_TERMINATION_COMPLETE;
170 if (events & PXP_TERMINATION_COMPLETE)
171 pxp_terminate_complete(pxp);
173 intel_runtime_pm_put(gt->uncore->rpm, wakeref);