xref: /openbmc/linux/drivers/gpu/drm/i915/pxp/intel_pxp_session.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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"
10cbbd3764SHuang, Sean Z #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"
14cbbd3764SHuang, Sean Z #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)1895c9e122SHuang, Sean Z static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
1995c9e122SHuang, Sean Z {
2095c9e122SHuang, Sean Z 	struct intel_uncore *uncore = pxp->ctrl_gt->uncore;
21cbbd3764SHuang, Sean Z 	intel_wakeref_t wakeref;
22cbbd3764SHuang, Sean Z 	u32 sip = 0;
23f67986b0SAlan Previn 
24cbbd3764SHuang, Sean Z 	/* if we're suspended the session is considered off */
25cbbd3764SHuang, Sean Z 	with_intel_runtime_pm_if_in_use(uncore->rpm, wakeref)
26cbbd3764SHuang, Sean Z 		sip = intel_uncore_read(uncore, KCR_SIP(pxp->kcr_base));
270cfab4cbSHuang, Sean Z 
280cfab4cbSHuang, Sean Z 	return sip & BIT(id);
290cfab4cbSHuang, 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 {
33cbbd3764SHuang, Sean Z 	struct intel_uncore *uncore = pxp->ctrl_gt->uncore;
34cbbd3764SHuang, Sean Z 	intel_wakeref_t wakeref;
35cbbd3764SHuang, Sean Z 	u32 mask = BIT(id);
36f67986b0SAlan Previn 	int ret;
37cbbd3764SHuang, Sean Z 
38cbbd3764SHuang, Sean Z 	/* if we're suspended the session is considered off */
39cbbd3764SHuang, Sean Z 	wakeref = intel_runtime_pm_get_if_in_use(uncore->rpm);
40cbbd3764SHuang, Sean Z 	if (!wakeref)
410cfab4cbSHuang, Sean Z 		return in_play ? -ENODEV : 0;
420cfab4cbSHuang, Sean Z 
430cfab4cbSHuang, Sean Z 	ret = intel_wait_for_register(uncore,
440cfab4cbSHuang, Sean Z 				      KCR_SIP(pxp->kcr_base),
450cfab4cbSHuang, Sean Z 				      mask,
460cfab4cbSHuang, Sean Z 				      in_play ? mask : 0,
47cbbd3764SHuang, Sean Z 				      250);
48cbbd3764SHuang, Sean Z 
49cbbd3764SHuang, Sean Z 	intel_runtime_pm_put(uncore->rpm, wakeref);
50cbbd3764SHuang, Sean Z 
51cbbd3764SHuang, Sean Z 	return ret;
520cfab4cbSHuang, Sean Z }
530cfab4cbSHuang, Sean Z 
pxp_create_arb_session(struct intel_pxp * pxp)54cbbd3764SHuang, Sean Z static int pxp_create_arb_session(struct intel_pxp *pxp)
55cbbd3764SHuang, Sean Z {
56cbbd3764SHuang, Sean Z 	struct intel_gt *gt = pxp->ctrl_gt;
572ae09687SHuang, Sean Z 	int ret;
58cbbd3764SHuang, Sean Z 
59f67986b0SAlan Previn 	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(&gt->i915->drm, "arb session already in play at creation time\n");
63cbbd3764SHuang, Sean Z 		return -EEXIST;
64cbbd3764SHuang, Sean Z 	}
65cbbd3764SHuang, Sean Z 
66cbbd3764SHuang, Sean Z 	if (HAS_ENGINE(pxp->ctrl_gt, GSC0))
67cbbd3764SHuang, Sean Z 		ret = intel_pxp_gsccs_create_session(pxp, ARB_SESSION);
68cbbd3764SHuang, Sean Z 	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(&gt->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) {
77*69e6dd14SAlan Previn 		drm_dbg(&gt->i915->drm, "arb session failed to go in play\n");
78cbbd3764SHuang, Sean Z 		return ret;
79cbbd3764SHuang, Sean Z 	}
80abf46db3SAlan Previn 	drm_dbg(&gt->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(&gt->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(&gt->i915->drm, "Session state did not clear\n");
10895c9e122SHuang, Sean Z 		return ret;
10995c9e122SHuang, Sean Z 	}
11095c9e122SHuang, Sean Z 
11195c9e122SHuang, Sean Z 	intel_uncore_write(gt->uncore, KCR_GLOBAL_TERMINATE(pxp->kcr_base), 1);
11295c9e122SHuang, Sean Z 
113d374c047SAlan Previn 	if (HAS_ENGINE(gt, GSC0))
114d374c047SAlan Previn 		intel_pxp_gsccs_end_arb_fw_session(pxp, ARB_SESSION);
11595c9e122SHuang, Sean Z 	else
11695c9e122SHuang, Sean Z 		intel_pxp_tee_end_arb_fw_session(pxp, ARB_SESSION);
1172ae09687SHuang, Sean Z 
1189b469093SAlan Previn 	return ret;
1192ae09687SHuang, Sean Z }
1202ae09687SHuang, Sean Z 
intel_pxp_terminate(struct intel_pxp * pxp,bool post_invalidation_needs_restart)1212ae09687SHuang, Sean Z void intel_pxp_terminate(struct intel_pxp *pxp, bool post_invalidation_needs_restart)
1229b469093SAlan Previn {
1232ae09687SHuang, Sean Z 	int ret;
1242ae09687SHuang, Sean Z 
1252ae09687SHuang, Sean Z 	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 */
1402ae09687SHuang, Sean Z 	if (fetch_and_zero(&pxp->hw_state_invalidated))
1412ae09687SHuang, Sean Z 		pxp_create_arb_session(pxp);
1422ae09687SHuang, Sean Z 
143c5be8fc9SDaniele Ceraolo Spurio 	complete_all(&pxp->termination);
1442ae09687SHuang, Sean Z }
1452ae09687SHuang, Sean Z 
pxp_session_work(struct work_struct * work)146f67986b0SAlan Previn static void pxp_session_work(struct work_struct *work)
1470cfab4cbSHuang, Sean Z {
1482ae09687SHuang, Sean Z 	struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
1492ae09687SHuang, Sean Z 	struct intel_gt *gt = pxp->ctrl_gt;
15003d2c54dSMatt Roper 	intel_wakeref_t wakeref;
1512ae09687SHuang, Sean Z 	u32 events = 0;
15203d2c54dSMatt Roper 
1532ae09687SHuang, Sean Z 	spin_lock_irq(gt->irq_lock);
1542ae09687SHuang, Sean Z 	events = fetch_and_zero(&pxp->session_events);
1552ae09687SHuang, Sean Z 	spin_unlock_irq(gt->irq_lock);
1562ae09687SHuang, Sean Z 
15732271ecdSDaniele Ceraolo Spurio 	if (!events)
15832271ecdSDaniele Ceraolo Spurio 		return;
15932271ecdSDaniele Ceraolo Spurio 
1600cfab4cbSHuang, Sean Z 	if (events & PXP_INVAL_REQUIRED)
1610cfab4cbSHuang, Sean Z 		intel_pxp_invalidate(pxp);
1620cfab4cbSHuang, Sean Z 
1630cfab4cbSHuang, Sean Z 	/*
1640cfab4cbSHuang, Sean Z 	 * If we're processing an event while suspending then don't bother,
1650cfab4cbSHuang, Sean Z 	 * we're going to re-init everything on resume anyway.
1660cfab4cbSHuang, Sean Z 	 */
1670cfab4cbSHuang, Sean Z 	wakeref = intel_runtime_pm_get_if_in_use(gt->uncore->rpm);
1682ae09687SHuang, Sean Z 	if (!wakeref)
1692ae09687SHuang, Sean Z 		return;
1709b469093SAlan Previn 
1712ae09687SHuang, Sean Z 	if (events & PXP_TERMINATION_REQUEST) {
1722ae09687SHuang, Sean Z 		events &= ~PXP_TERMINATION_COMPLETE;
1732ae09687SHuang, Sean Z 		intel_pxp_terminate(pxp, true);
1742ae09687SHuang, Sean Z 	}
1750cfab4cbSHuang, Sean Z 
1760cfab4cbSHuang, Sean Z 	if (events & PXP_TERMINATION_COMPLETE)
1772ae09687SHuang, Sean Z 		pxp_terminate_complete(pxp);
178c5be8fc9SDaniele Ceraolo Spurio 
179c5be8fc9SDaniele Ceraolo Spurio 	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
180c5be8fc9SDaniele Ceraolo Spurio }
181c5be8fc9SDaniele Ceraolo Spurio 
intel_pxp_session_management_init(struct intel_pxp * pxp)182c5be8fc9SDaniele Ceraolo Spurio void intel_pxp_session_management_init(struct intel_pxp *pxp)
183c5be8fc9SDaniele Ceraolo Spurio {
184 	mutex_init(&pxp->arb_mutex);
185 	INIT_WORK(&pxp->session_work, pxp_session_work);
186 }
187