1 // SPDX-License-Identifier: MIT
3 * Copyright(c) 2020, Intel Corporation. All rights reserved.
6 #include "drm/i915_drm.h"
10 #include "intel_pxp_cmd.h"
11 #include "intel_pxp_session.h"
12 #include "intel_pxp_tee.h"
13 #include "intel_pxp_types.h"
15 #define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */
17 #define GEN12_KCR_SIP _MMIO(0x32260) /* KCR hwdrm session in play 0-31 */
19 /* PXP global terminate register for session termination */
20 #define PXP_GLOBAL_TERMINATE _MMIO(0x320f8)
22 static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
24 struct intel_uncore *uncore = pxp_to_gt(pxp)->uncore;
25 intel_wakeref_t wakeref;
28 /* if we're suspended the session is considered off */
29 with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref)
30 sip = intel_uncore_read(uncore, GEN12_KCR_SIP);
35 static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play)
37 struct intel_uncore *uncore = pxp_to_gt(pxp)->uncore;
38 intel_wakeref_t wakeref;
42 /* if we're suspended the session is considered off */
43 wakeref = intel_runtime_pm_get_if_in_use(uncore->rpm);
45 return in_play ? -ENODEV : 0;
47 ret = intel_wait_for_register(uncore,
53 intel_runtime_pm_put(uncore->rpm, wakeref);
58 static int pxp_create_arb_session(struct intel_pxp *pxp)
60 struct intel_gt *gt = pxp_to_gt(pxp);
63 pxp->arb_is_valid = false;
65 if (intel_pxp_session_is_in_play(pxp, ARB_SESSION)) {
66 drm_err(>->i915->drm, "arb session already in play at creation time\n");
70 ret = intel_pxp_tee_cmd_create_arb_session(pxp, ARB_SESSION);
72 drm_err(>->i915->drm, "tee cmd for arb session creation failed\n");
76 ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true);
78 drm_err(>->i915->drm, "arb session failed to go in play\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_to_gt(pxp);
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, PXP_GLOBAL_TERMINATE, 1);
116 static void pxp_terminate(struct intel_pxp *pxp)
120 pxp->hw_state_invalidated = true;
123 * if we fail to submit the termination there is no point in waiting for
124 * it to complete. PXP will be marked as non-active until the next
125 * termination is issued.
127 ret = pxp_terminate_arb_session_and_global(pxp);
129 complete_all(&pxp->termination);
132 static void pxp_terminate_complete(struct intel_pxp *pxp)
134 /* Re-create the arb session after teardown handle complete */
135 if (fetch_and_zero(&pxp->hw_state_invalidated))
136 pxp_create_arb_session(pxp);
138 complete_all(&pxp->termination);
141 void intel_pxp_session_work(struct work_struct *work)
143 struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
144 struct intel_gt *gt = pxp_to_gt(pxp);
145 intel_wakeref_t wakeref;
148 spin_lock_irq(>->irq_lock);
149 events = fetch_and_zero(&pxp->session_events);
150 spin_unlock_irq(>->irq_lock);
155 if (events & PXP_INVAL_REQUIRED)
156 intel_pxp_invalidate(pxp);
159 * If we're processing an event while suspending then don't bother,
160 * we're going to re-init everything on resume anyway.
162 wakeref = intel_runtime_pm_get_if_in_use(gt->uncore->rpm);
166 if (events & PXP_TERMINATION_REQUEST) {
167 events &= ~PXP_TERMINATION_COMPLETE;
171 if (events & PXP_TERMINATION_COMPLETE)
172 pxp_terminate_complete(pxp);
174 intel_runtime_pm_put(gt->uncore->rpm, wakeref);