1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright(c) 2020, Intel Corporation. All rights reserved.
4  */
5 
6 #include "drm/i915_drm.h"
7 #include "i915_drv.h"
8 
9 #include "intel_pxp.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"
14 
15 #define ARB_SESSION I915_PROTECTED_CONTENT_DEFAULT_SESSION /* shorter define */
16 
17 #define GEN12_KCR_SIP _MMIO(0x32260) /* KCR hwdrm session in play 0-31 */
18 
19 /* PXP global terminate register for session termination */
20 #define PXP_GLOBAL_TERMINATE _MMIO(0x320f8)
21 
22 static bool intel_pxp_session_is_in_play(struct intel_pxp *pxp, u32 id)
23 {
24 	struct intel_gt *gt = pxp_to_gt(pxp);
25 	intel_wakeref_t wakeref;
26 	u32 sip = 0;
27 
28 	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
29 		sip = intel_uncore_read(gt->uncore, GEN12_KCR_SIP);
30 
31 	return sip & BIT(id);
32 }
33 
34 static int pxp_wait_for_session_state(struct intel_pxp *pxp, u32 id, bool in_play)
35 {
36 	struct intel_gt *gt = pxp_to_gt(pxp);
37 	intel_wakeref_t wakeref;
38 	u32 mask = BIT(id);
39 	int ret;
40 
41 	with_intel_runtime_pm(gt->uncore->rpm, wakeref)
42 		ret = intel_wait_for_register(gt->uncore,
43 					      GEN12_KCR_SIP,
44 					      mask,
45 					      in_play ? mask : 0,
46 					      100);
47 
48 	return ret;
49 }
50 
51 static int pxp_create_arb_session(struct intel_pxp *pxp)
52 {
53 	struct intel_gt *gt = pxp_to_gt(pxp);
54 	int ret;
55 
56 	pxp->arb_is_valid = false;
57 
58 	if (intel_pxp_session_is_in_play(pxp, ARB_SESSION)) {
59 		drm_err(&gt->i915->drm, "arb session already in play at creation time\n");
60 		return -EEXIST;
61 	}
62 
63 	ret = intel_pxp_tee_cmd_create_arb_session(pxp, ARB_SESSION);
64 	if (ret) {
65 		drm_err(&gt->i915->drm, "tee cmd for arb session creation failed\n");
66 		return ret;
67 	}
68 
69 	ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true);
70 	if (ret) {
71 		drm_err(&gt->i915->drm, "arb session failed to go in play\n");
72 		return ret;
73 	}
74 
75 	pxp->arb_is_valid = true;
76 
77 	return 0;
78 }
79 
80 static int pxp_terminate_arb_session_and_global(struct intel_pxp *pxp)
81 {
82 	int ret;
83 	struct intel_gt *gt = pxp_to_gt(pxp);
84 
85 	/* must mark termination in progress calling this function */
86 	GEM_WARN_ON(pxp->arb_is_valid);
87 
88 	/* terminate the hw sessions */
89 	ret = intel_pxp_terminate_session(pxp, ARB_SESSION);
90 	if (ret) {
91 		drm_err(&gt->i915->drm, "Failed to submit session termination\n");
92 		return ret;
93 	}
94 
95 	ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false);
96 	if (ret) {
97 		drm_err(&gt->i915->drm, "Session state did not clear\n");
98 		return ret;
99 	}
100 
101 	intel_uncore_write(gt->uncore, PXP_GLOBAL_TERMINATE, 1);
102 
103 	return ret;
104 }
105 
106 static void pxp_terminate(struct intel_pxp *pxp)
107 {
108 	int ret;
109 
110 	pxp->hw_state_invalidated = true;
111 
112 	/*
113 	 * if we fail to submit the termination there is no point in waiting for
114 	 * it to complete. PXP will be marked as non-active until the next
115 	 * termination is issued.
116 	 */
117 	ret = pxp_terminate_arb_session_and_global(pxp);
118 	if (ret)
119 		complete_all(&pxp->termination);
120 }
121 
122 static void pxp_terminate_complete(struct intel_pxp *pxp)
123 {
124 	/* Re-create the arb session after teardown handle complete */
125 	if (fetch_and_zero(&pxp->hw_state_invalidated))
126 		pxp_create_arb_session(pxp);
127 
128 	complete_all(&pxp->termination);
129 }
130 
131 void intel_pxp_session_work(struct work_struct *work)
132 {
133 	struct intel_pxp *pxp = container_of(work, typeof(*pxp), session_work);
134 	struct intel_gt *gt = pxp_to_gt(pxp);
135 	u32 events = 0;
136 
137 	spin_lock_irq(&gt->irq_lock);
138 	events = fetch_and_zero(&pxp->session_events);
139 	spin_unlock_irq(&gt->irq_lock);
140 
141 	if (!events)
142 		return;
143 
144 	if (events & PXP_TERMINATION_REQUEST) {
145 		events &= ~PXP_TERMINATION_COMPLETE;
146 		pxp_terminate(pxp);
147 	}
148 
149 	if (events & PXP_TERMINATION_COMPLETE)
150 		pxp_terminate_complete(pxp);
151 }
152