1cbbd3764SHuang, Sean Z // SPDX-License-Identifier: MIT
2cbbd3764SHuang, Sean Z /*
3cbbd3764SHuang, Sean Z * Copyright(c) 2020, Intel Corporation. All rights reserved.
4cbbd3764SHuang, Sean Z */
5cbbd3764SHuang, Sean Z
6cbbd3764SHuang, Sean Z #include "i915_drv.h"
7cbbd3764SHuang, Sean Z
8cbbd3764SHuang, Sean Z #include "intel_pxp.h"
995c9e122SHuang, Sean Z #include "intel_pxp_cmd.h"
1099afb7ccSAlan Previn #include "intel_pxp_gsccs.h"
11cbbd3764SHuang, Sean Z #include "intel_pxp_session.h"
12cbbd3764SHuang, Sean Z #include "intel_pxp_tee.h"
13cbbd3764SHuang, Sean Z #include "intel_pxp_types.h"
145adacf19SAlan Previn #include "intel_pxp_regs.h"
15cbbd3764SHuang, Sean Z
16cbbd3764SHuang, Sean Z #define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */
17cbbd3764SHuang, Sean Z
intel_pxp_session_is_in_play(struct intel_pxp * pxp,u32 id)18cbbd3764SHuang, Sean Z static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
19cbbd3764SHuang, Sean Z {
20f67986b0SAlan Previn struct intel_uncore *uncore = pxp->ctrl_gt->uncore;
21cbbd3764SHuang, Sean Z intel_wakeref_t wakeref;
22cbbd3764SHuang, Sean Z u32 sip = 0;
23cbbd3764SHuang, Sean Z
240cfab4cbSHuang, Sean Z /* if we're suspended the session is considered off */
250cfab4cbSHuang, Sean Z with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref)
265adacf19SAlan Previn sip = intel_uncore_read(uncore, KCR_SIP(pxp->kcr_base));
27cbbd3764SHuang, Sean Z
28cbbd3764SHuang, Sean Z return sip & BIT(id);
29cbbd3764SHuang, Sean Z }
30cbbd3764SHuang, Sean Z
pxp_wait_for_session_state(struct intel_pxp * pxp,u32 id,bool in_play)31cbbd3764SHuang, Sean Z static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play)
32cbbd3764SHuang, Sean Z {
33f67986b0SAlan Previn struct intel_uncore *uncore = pxp->ctrl_gt->uncore;
34cbbd3764SHuang, Sean Z intel_wakeref_t wakeref;
35cbbd3764SHuang, Sean Z u32 mask = BIT(id);
36cbbd3764SHuang, Sean Z int ret;
37cbbd3764SHuang, Sean Z
380cfab4cbSHuang, Sean Z /* if we're suspended the session is considered off */
390cfab4cbSHuang, Sean Z wakeref = intel_runtime_pm_get_if_in_use(uncore->rpm);
400cfab4cbSHuang, Sean Z if (!wakeref)
410cfab4cbSHuang, Sean Z return in_play ? -ENODEV : 0;
420cfab4cbSHuang, Sean Z
430cfab4cbSHuang, Sean Z ret = intel_wait_for_register(uncore,
445adacf19SAlan Previn KCR_SIP(pxp->kcr_base),
45cbbd3764SHuang, Sean Z mask,
46cbbd3764SHuang, Sean Z in_play ? mask : 0,
4741e65d87SAlan Previn 250);
48cbbd3764SHuang, Sean Z
490cfab4cbSHuang, Sean Z intel_runtime_pm_put(uncore->rpm, wakeref);
500cfab4cbSHuang, Sean Z
51cbbd3764SHuang, Sean Z return ret;
52cbbd3764SHuang, Sean Z }
53cbbd3764SHuang, Sean Z
pxp_create_arb_session(struct intel_pxp * pxp)542ae09687SHuang, Sean Z static int pxp_create_arb_session(struct intel_pxp *pxp)
55cbbd3764SHuang, Sean Z {
56f67986b0SAlan Previn struct intel_gt *gt = pxp->ctrl_gt;
57cbbd3764SHuang, Sean Z int ret;
58cbbd3764SHuang, Sean Z
59cbbd3764SHuang, Sean Z pxp->arb_is_valid = false;
60cbbd3764SHuang, Sean Z
61cbbd3764SHuang, Sean Z if (intel_pxp_session_is_in_play(pxp, ARB_SESSION)) {
62cbbd3764SHuang, Sean Z drm_err(>->i915->drm, "arb session already in play at creation time\n");
63cbbd3764SHuang, Sean Z return -EEXIST;
64cbbd3764SHuang, Sean Z }
65cbbd3764SHuang, Sean Z
6699afb7ccSAlan Previn if (HAS_ENGINE(pxp->ctrl_gt, GSC0))
6799afb7ccSAlan Previn ret = intel_pxp_gsccs_create_session(pxp, ARB_SESSION);
6899afb7ccSAlan Previn else
69cbbd3764SHuang, Sean Z ret = intel_pxp_tee_cmd_create_arb_session(pxp, ARB_SESSION);
70cbbd3764SHuang, Sean Z if (ret) {
71cbbd3764SHuang, Sean Z drm_err(>->i915->drm, "tee cmd for arb session creation failed\n");
72cbbd3764SHuang, Sean Z return ret;
73cbbd3764SHuang, Sean Z }
74cbbd3764SHuang, Sean Z
75cbbd3764SHuang, Sean Z ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true);
76cbbd3764SHuang, Sean Z if (ret) {
7769e6dd14SAlan Previn drm_dbg(>->i915->drm, "arb session failed to go in play\n");
78cbbd3764SHuang, Sean Z return ret;
79cbbd3764SHuang, Sean Z }
80abf46db3SAlan Previn drm_dbg(>->i915->drm, "PXP ARB session is alive\n");
81cbbd3764SHuang, Sean Z
82d3ac8d42SDaniele Ceraolo Spurio if (!++pxp->key_instance)
83d3ac8d42SDaniele Ceraolo Spurio ++pxp->key_instance;
84d3ac8d42SDaniele Ceraolo Spurio
85cbbd3764SHuang, Sean Z pxp->arb_is_valid = true;
86cbbd3764SHuang, Sean Z
87cbbd3764SHuang, Sean Z return 0;
88cbbd3764SHuang, Sean Z }
8995c9e122SHuang, Sean Z
pxp_terminate_arb_session_and_global(struct intel_pxp * pxp)902ae09687SHuang, Sean Z static int pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)
9195c9e122SHuang, Sean Z {
9295c9e122SHuang, Sean Z int ret;
93f67986b0SAlan Previn struct intel_gt *gt = pxp->ctrl_gt;
9495c9e122SHuang, Sean Z
952ae09687SHuang, Sean Z /* must mark termination in progress calling this function */
962ae09687SHuang, Sean Z GEM_WARN_ON(pxp->arb_is_valid);
9795c9e122SHuang, Sean Z
9895c9e122SHuang, Sean Z /* terminate the hw sessions */
9995c9e122SHuang, Sean Z ret = intel_pxp_terminate_session(pxp, ARB_SESSION);
10095c9e122SHuang, Sean Z if (ret) {
10195c9e122SHuang, Sean Z drm_err(>->i915->drm, "Failed to submit session termination\n");
10295c9e122SHuang, Sean Z return ret;
10395c9e122SHuang, Sean Z }
10495c9e122SHuang, Sean Z
10595c9e122SHuang, Sean Z ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false);
10695c9e122SHuang, Sean Z if (ret) {
10795c9e122SHuang, Sean Z drm_err(>->i915->drm, "Session state did not clear\n");
10895c9e122SHuang, Sean Z return ret;
10995c9e122SHuang, Sean Z }
11095c9e122SHuang, Sean Z
1115adacf19SAlan Previn intel_uncore_write(gt->uncore, KCR_GLOBAL_TERMINATE(pxp->kcr_base), 1);
11295c9e122SHuang, Sean Z
11399afb7ccSAlan Previn if (HAS_ENGINE(gt, GSC0))
11499afb7ccSAlan Previn intel_pxp_gsccs_end_arb_fw_session(pxp, ARB_SESSION);
11599afb7ccSAlan Previn else
116d374c047SAlan Previn intel_pxp_tee_end_arb_fw_session(pxp, ARB_SESSION);
117d374c047SAlan Previn
11895c9e122SHuang, Sean Z return ret;
11995c9e122SHuang, Sean Z }
1202ae09687SHuang, Sean Z
intel_pxp_terminate(struct intel_pxp * pxp,bool post_invalidation_needs_restart)1219b469093SAlan Previn void intel_pxp_terminate(struct intel_pxp *pxp, bool post_invalidation_needs_restart)
1222ae09687SHuang, Sean Z {
1232ae09687SHuang, Sean Z int ret;
1242ae09687SHuang, Sean Z
1259b469093SAlan Previn pxp->hw_state_invalidated = post_invalidation_needs_restart;
1262ae09687SHuang, Sean Z
1272ae09687SHuang, Sean Z /*
1282ae09687SHuang, Sean Z * if we fail to submit the termination there is no point in waiting for
1292ae09687SHuang, Sean Z * it to complete. PXP will be marked as non-active until the next
1302ae09687SHuang, Sean Z * termination is issued.
1312ae09687SHuang, Sean Z */
1322ae09687SHuang, Sean Z ret = pxp_terminate_arb_session_and_global(pxp);
1332ae09687SHuang, Sean Z if (ret)
1342ae09687SHuang, Sean Z complete_all(&pxp->termination);
1352ae09687SHuang, Sean Z }
1362ae09687SHuang, Sean Z
pxp_terminate_complete(struct intel_pxp * pxp)1372ae09687SHuang, Sean Z static void pxp_terminate_complete(struct intel_pxp *pxp)
1382ae09687SHuang, Sean Z {
1392ae09687SHuang, Sean Z /* Re-create the arb session after teardown handle complete */
140*0eec708eSAlan Previn if (fetch_and_zero(&pxp->hw_state_invalidated)) {
141*0eec708eSAlan Previn drm_dbg(&pxp->ctrl_gt->i915->drm, "PXP: creating arb_session after invalidation");
1422ae09687SHuang, Sean Z pxp_create_arb_session(pxp);
143*0eec708eSAlan Previn }
1442ae09687SHuang, Sean Z
1452ae09687SHuang, Sean Z complete_all(&pxp->termination);
1462ae09687SHuang, Sean Z }
1472ae09687SHuang, Sean Z
pxp_session_work(struct work_struct * work)148c5be8fc9SDaniele Ceraolo Spurio static void pxp_session_work(struct work_struct *work)
1492ae09687SHuang, Sean Z {
1502ae09687SHuang, Sean Z struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
151f67986b0SAlan Previn struct intel_gt *gt = pxp->ctrl_gt;
1520cfab4cbSHuang, Sean Z intel_wakeref_t wakeref;
1532ae09687SHuang, Sean Z u32 events = 0;
1542ae09687SHuang, Sean Z
15503d2c54dSMatt Roper spin_lock_irq(gt->irq_lock);
1562ae09687SHuang, Sean Z events = fetch_and_zero(&pxp->session_events);
15703d2c54dSMatt Roper spin_unlock_irq(gt->irq_lock);
1582ae09687SHuang, Sean Z
1592ae09687SHuang, Sean Z if (!events)
1602ae09687SHuang, Sean Z return;
1612ae09687SHuang, Sean Z
162*0eec708eSAlan Previn drm_dbg(>->i915->drm, "PXP: processing event-flags 0x%08x", events);
163*0eec708eSAlan Previn
16432271ecdSDaniele Ceraolo Spurio if (events & PXP_INVAL_REQUIRED)
16532271ecdSDaniele Ceraolo Spurio intel_pxp_invalidate(pxp);
16632271ecdSDaniele Ceraolo Spurio
1670cfab4cbSHuang, Sean Z /*
1680cfab4cbSHuang, Sean Z * If we're processing an event while suspending then don't bother,
1690cfab4cbSHuang, Sean Z * we're going to re-init everything on resume anyway.
1700cfab4cbSHuang, Sean Z */
1710cfab4cbSHuang, Sean Z wakeref = intel_runtime_pm_get_if_in_use(gt->uncore->rpm);
1720cfab4cbSHuang, Sean Z if (!wakeref)
1730cfab4cbSHuang, Sean Z return;
1740cfab4cbSHuang, Sean Z
1752ae09687SHuang, Sean Z if (events & PXP_TERMINATION_REQUEST) {
1762ae09687SHuang, Sean Z events &= ~PXP_TERMINATION_COMPLETE;
1779b469093SAlan Previn intel_pxp_terminate(pxp, true);
1782ae09687SHuang, Sean Z }
1792ae09687SHuang, Sean Z
1802ae09687SHuang, Sean Z if (events & PXP_TERMINATION_COMPLETE)
1812ae09687SHuang, Sean Z pxp_terminate_complete(pxp);
1820cfab4cbSHuang, Sean Z
1830cfab4cbSHuang, Sean Z intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1842ae09687SHuang, Sean Z }
185c5be8fc9SDaniele Ceraolo Spurio
intel_pxp_session_management_init(struct intel_pxp * pxp)186c5be8fc9SDaniele Ceraolo Spurio void intel_pxp_session_management_init(struct intel_pxp *pxp)
187c5be8fc9SDaniele Ceraolo Spurio {
188c5be8fc9SDaniele Ceraolo Spurio mutex_init(&pxp->arb_mutex);
189c5be8fc9SDaniele Ceraolo Spurio INIT_WORK(&pxp->session_work, pxp_session_work);
190c5be8fc9SDaniele Ceraolo Spurio }
191