1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright(c) 2020 Intel Corporation.
4  */
5 
6 #include <linux/component.h>
7 
8 #include <drm/i915_pxp_tee_interface.h>
9 #include <drm/i915_component.h>
10 
11 #include "gem/i915_gem_lmem.h"
12 
13 #include "i915_drv.h"
14 
15 #include "intel_pxp.h"
16 #include "intel_pxp_cmd_interface_42.h"
17 #include "intel_pxp_huc.h"
18 #include "intel_pxp_session.h"
19 #include "intel_pxp_tee.h"
20 #include "intel_pxp_types.h"
21 
22 static bool
23 is_fw_err_platform_config(u32 type)
24 {
25 	switch (type) {
26 	case PXP_STATUS_ERROR_API_VERSION:
27 	case PXP_STATUS_PLATFCONFIG_KF1_NOVERIF:
28 	case PXP_STATUS_PLATFCONFIG_KF1_BAD:
29 		return true;
30 	default:
31 		break;
32 	}
33 	return false;
34 }
35 
36 static const char *
37 fw_err_to_string(u32 type)
38 {
39 	switch (type) {
40 	case PXP_STATUS_ERROR_API_VERSION:
41 		return "ERR_API_VERSION";
42 	case PXP_STATUS_NOT_READY:
43 		return "ERR_NOT_READY";
44 	case PXP_STATUS_PLATFCONFIG_KF1_NOVERIF:
45 	case PXP_STATUS_PLATFCONFIG_KF1_BAD:
46 		return "ERR_PLATFORM_CONFIG";
47 	default:
48 		break;
49 	}
50 	return NULL;
51 }
52 
53 static int intel_pxp_tee_io_message(struct intel_pxp *pxp,
54 				    void *msg_in, u32 msg_in_size,
55 				    void *msg_out, u32 msg_out_max_size,
56 				    u32 *msg_out_rcv_size)
57 {
58 	struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
59 	struct i915_pxp_component *pxp_component = pxp->pxp_component;
60 	int ret = 0;
61 
62 	mutex_lock(&pxp->tee_mutex);
63 
64 	/*
65 	 * The binding of the component is asynchronous from i915 probe, so we
66 	 * can't be sure it has happened.
67 	 */
68 	if (!pxp_component) {
69 		ret = -ENODEV;
70 		goto unlock;
71 	}
72 
73 	ret = pxp_component->ops->send(pxp_component->tee_dev, msg_in, msg_in_size);
74 	if (ret) {
75 		drm_err(&i915->drm, "Failed to send PXP TEE message\n");
76 		goto unlock;
77 	}
78 
79 	ret = pxp_component->ops->recv(pxp_component->tee_dev, msg_out, msg_out_max_size);
80 	if (ret < 0) {
81 		drm_err(&i915->drm, "Failed to receive PXP TEE message\n");
82 		goto unlock;
83 	}
84 
85 	if (ret > msg_out_max_size) {
86 		drm_err(&i915->drm,
87 			"Failed to receive PXP TEE message due to unexpected output size\n");
88 		ret = -ENOSPC;
89 		goto unlock;
90 	}
91 
92 	if (msg_out_rcv_size)
93 		*msg_out_rcv_size = ret;
94 
95 	ret = 0;
96 unlock:
97 	mutex_unlock(&pxp->tee_mutex);
98 	return ret;
99 }
100 
101 int intel_pxp_tee_stream_message(struct intel_pxp *pxp,
102 				 u8 client_id, u32 fence_id,
103 				 void *msg_in, size_t msg_in_len,
104 				 void *msg_out, size_t msg_out_len)
105 {
106 	/* TODO: for bigger objects we need to use a sg of 4k pages */
107 	const size_t max_msg_size = PAGE_SIZE;
108 	struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
109 	struct i915_pxp_component *pxp_component = pxp->pxp_component;
110 	unsigned int offset = 0;
111 	struct scatterlist *sg;
112 	int ret;
113 
114 	if (msg_in_len > max_msg_size || msg_out_len > max_msg_size)
115 		return -ENOSPC;
116 
117 	mutex_lock(&pxp->tee_mutex);
118 
119 	if (unlikely(!pxp_component || !pxp_component->ops->gsc_command)) {
120 		ret = -ENODEV;
121 		goto unlock;
122 	}
123 
124 	GEM_BUG_ON(!pxp->stream_cmd.obj);
125 
126 	sg = i915_gem_object_get_sg_dma(pxp->stream_cmd.obj, 0, &offset);
127 
128 	memcpy(pxp->stream_cmd.vaddr, msg_in, msg_in_len);
129 
130 	ret = pxp_component->ops->gsc_command(pxp_component->tee_dev, client_id,
131 					      fence_id, sg, msg_in_len, sg);
132 	if (ret < 0)
133 		drm_err(&i915->drm, "Failed to send PXP TEE gsc command\n");
134 	else
135 		memcpy(msg_out, pxp->stream_cmd.vaddr, msg_out_len);
136 
137 unlock:
138 	mutex_unlock(&pxp->tee_mutex);
139 	return ret;
140 }
141 
142 /**
143  * i915_pxp_tee_component_bind - bind function to pass the function pointers to pxp_tee
144  * @i915_kdev: pointer to i915 kernel device
145  * @tee_kdev: pointer to tee kernel device
146  * @data: pointer to pxp_tee_master containing the function pointers
147  *
148  * This bind function is called during the system boot or resume from system sleep.
149  *
150  * Return: return 0 if successful.
151  */
152 static int i915_pxp_tee_component_bind(struct device *i915_kdev,
153 				       struct device *tee_kdev, void *data)
154 {
155 	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
156 	struct intel_pxp *pxp = i915->pxp;
157 	struct intel_uc *uc = &pxp->ctrl_gt->uc;
158 	intel_wakeref_t wakeref;
159 	int ret = 0;
160 
161 	if (!HAS_HECI_PXP(i915)) {
162 		pxp->dev_link = device_link_add(i915_kdev, tee_kdev, DL_FLAG_STATELESS);
163 		if (drm_WARN_ON(&i915->drm, !pxp->dev_link))
164 			return -ENODEV;
165 	}
166 
167 	mutex_lock(&pxp->tee_mutex);
168 	pxp->pxp_component = data;
169 	pxp->pxp_component->tee_dev = tee_kdev;
170 	mutex_unlock(&pxp->tee_mutex);
171 
172 	if (intel_uc_uses_huc(uc) && intel_huc_is_loaded_by_gsc(&uc->huc)) {
173 		with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
174 			/* load huc via pxp */
175 			ret = intel_huc_fw_load_and_auth_via_gsc(&uc->huc);
176 			if (ret < 0)
177 				drm_err(&i915->drm, "failed to load huc via gsc %d\n", ret);
178 		}
179 	}
180 
181 	/* if we are suspended, the HW will be re-initialized on resume */
182 	wakeref = intel_runtime_pm_get_if_in_use(&i915->runtime_pm);
183 	if (!wakeref)
184 		return 0;
185 
186 	/* the component is required to fully start the PXP HW */
187 	if (intel_pxp_is_enabled(pxp))
188 		intel_pxp_init_hw(pxp);
189 
190 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
191 
192 	return ret;
193 }
194 
195 static void i915_pxp_tee_component_unbind(struct device *i915_kdev,
196 					  struct device *tee_kdev, void *data)
197 {
198 	struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
199 	struct intel_pxp *pxp = i915->pxp;
200 	intel_wakeref_t wakeref;
201 
202 	if (intel_pxp_is_enabled(pxp))
203 		with_intel_runtime_pm_if_in_use(&i915->runtime_pm, wakeref)
204 			intel_pxp_fini_hw(pxp);
205 
206 	mutex_lock(&pxp->tee_mutex);
207 	pxp->pxp_component = NULL;
208 	mutex_unlock(&pxp->tee_mutex);
209 
210 	if (pxp->dev_link) {
211 		device_link_del(pxp->dev_link);
212 		pxp->dev_link = NULL;
213 	}
214 }
215 
216 static const struct component_ops i915_pxp_tee_component_ops = {
217 	.bind   = i915_pxp_tee_component_bind,
218 	.unbind = i915_pxp_tee_component_unbind,
219 };
220 
221 static int alloc_streaming_command(struct intel_pxp *pxp)
222 {
223 	struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
224 	struct drm_i915_gem_object *obj = NULL;
225 	void *cmd;
226 	int err;
227 
228 	pxp->stream_cmd.obj = NULL;
229 	pxp->stream_cmd.vaddr = NULL;
230 
231 	if (!IS_DGFX(i915))
232 		return 0;
233 
234 	/* allocate lmem object of one page for PXP command memory and store it */
235 	obj = i915_gem_object_create_lmem(i915, PAGE_SIZE, I915_BO_ALLOC_CONTIGUOUS);
236 	if (IS_ERR(obj)) {
237 		drm_err(&i915->drm, "Failed to allocate pxp streaming command!\n");
238 		return PTR_ERR(obj);
239 	}
240 
241 	err = i915_gem_object_pin_pages_unlocked(obj);
242 	if (err) {
243 		drm_err(&i915->drm, "Failed to pin gsc message page!\n");
244 		goto out_put;
245 	}
246 
247 	/* map the lmem into the virtual memory pointer */
248 	cmd = i915_gem_object_pin_map_unlocked(obj, i915_coherent_map_type(i915, obj, true));
249 	if (IS_ERR(cmd)) {
250 		drm_err(&i915->drm, "Failed to map gsc message page!\n");
251 		err = PTR_ERR(cmd);
252 		goto out_unpin;
253 	}
254 
255 	memset(cmd, 0, obj->base.size);
256 
257 	pxp->stream_cmd.obj = obj;
258 	pxp->stream_cmd.vaddr = cmd;
259 
260 	return 0;
261 
262 out_unpin:
263 	i915_gem_object_unpin_pages(obj);
264 out_put:
265 	i915_gem_object_put(obj);
266 	return err;
267 }
268 
269 static void free_streaming_command(struct intel_pxp *pxp)
270 {
271 	struct drm_i915_gem_object *obj = fetch_and_zero(&pxp->stream_cmd.obj);
272 
273 	if (!obj)
274 		return;
275 
276 	i915_gem_object_unpin_map(obj);
277 	i915_gem_object_unpin_pages(obj);
278 	i915_gem_object_put(obj);
279 }
280 
281 int intel_pxp_tee_component_init(struct intel_pxp *pxp)
282 {
283 	int ret;
284 	struct intel_gt *gt = pxp->ctrl_gt;
285 	struct drm_i915_private *i915 = gt->i915;
286 
287 	mutex_init(&pxp->tee_mutex);
288 
289 	ret = alloc_streaming_command(pxp);
290 	if (ret)
291 		return ret;
292 
293 	ret = component_add_typed(i915->drm.dev, &i915_pxp_tee_component_ops,
294 				  I915_COMPONENT_PXP);
295 	if (ret < 0) {
296 		drm_err(&i915->drm, "Failed to add PXP component (%d)\n", ret);
297 		goto out_free;
298 	}
299 
300 	pxp->pxp_component_added = true;
301 
302 	return 0;
303 
304 out_free:
305 	free_streaming_command(pxp);
306 	return ret;
307 }
308 
309 void intel_pxp_tee_component_fini(struct intel_pxp *pxp)
310 {
311 	struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
312 
313 	if (!pxp->pxp_component_added)
314 		return;
315 
316 	component_del(i915->drm.dev, &i915_pxp_tee_component_ops);
317 	pxp->pxp_component_added = false;
318 
319 	free_streaming_command(pxp);
320 }
321 
322 int intel_pxp_tee_cmd_create_arb_session(struct intel_pxp *pxp,
323 					 int arb_session_id)
324 {
325 	struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
326 	struct pxp42_create_arb_in msg_in = {0};
327 	struct pxp42_create_arb_out msg_out = {0};
328 	int ret;
329 
330 	msg_in.header.api_version = PXP_APIVER(4, 2);
331 	msg_in.header.command_id = PXP42_CMDID_INIT_SESSION;
332 	msg_in.header.buffer_len = sizeof(msg_in) - sizeof(msg_in.header);
333 	msg_in.protection_mode = PXP42_ARB_SESSION_MODE_HEAVY;
334 	msg_in.session_id = arb_session_id;
335 
336 	ret = intel_pxp_tee_io_message(pxp,
337 				       &msg_in, sizeof(msg_in),
338 				       &msg_out, sizeof(msg_out),
339 				       NULL);
340 
341 	if (ret) {
342 		drm_err(&i915->drm, "Failed to send tee msg init arb session, ret=[%d]\n", ret);
343 	} else if (msg_out.header.status != 0) {
344 		if (is_fw_err_platform_config(msg_out.header.status)) {
345 			drm_info_once(&i915->drm,
346 				      "PXP init-arb-session-%d failed due to BIOS/SOC:0x%08x:%s\n",
347 				      arb_session_id, msg_out.header.status,
348 				      fw_err_to_string(msg_out.header.status));
349 		} else {
350 			drm_dbg(&i915->drm, "PXP init-arb-session--%d failed 0x%08x:%st:\n",
351 				arb_session_id, msg_out.header.status,
352 				fw_err_to_string(msg_out.header.status));
353 			drm_dbg(&i915->drm, "     cmd-detail: ID=[0x%08x],API-Ver-[0x%08x]\n",
354 				msg_in.header.command_id, msg_in.header.api_version);
355 		}
356 	}
357 
358 	return ret;
359 }
360 
361 void intel_pxp_tee_end_arb_fw_session(struct intel_pxp *pxp, u32 session_id)
362 {
363 	struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
364 	struct pxp42_inv_stream_key_in msg_in = {0};
365 	struct pxp42_inv_stream_key_out msg_out = {0};
366 	int ret, trials = 0;
367 
368 try_again:
369 	memset(&msg_in, 0, sizeof(msg_in));
370 	memset(&msg_out, 0, sizeof(msg_out));
371 	msg_in.header.api_version = PXP_APIVER(4, 2);
372 	msg_in.header.command_id = PXP42_CMDID_INVALIDATE_STREAM_KEY;
373 	msg_in.header.buffer_len = sizeof(msg_in) - sizeof(msg_in.header);
374 
375 	msg_in.header.stream_id = FIELD_PREP(PXP_CMDHDR_EXTDATA_SESSION_VALID, 1);
376 	msg_in.header.stream_id |= FIELD_PREP(PXP_CMDHDR_EXTDATA_APP_TYPE, 0);
377 	msg_in.header.stream_id |= FIELD_PREP(PXP_CMDHDR_EXTDATA_SESSION_ID, session_id);
378 
379 	ret = intel_pxp_tee_io_message(pxp,
380 				       &msg_in, sizeof(msg_in),
381 				       &msg_out, sizeof(msg_out),
382 				       NULL);
383 
384 	/* Cleanup coherency between GT and Firmware is critical, so try again if it fails */
385 	if ((ret || msg_out.header.status != 0x0) && ++trials < 3)
386 		goto try_again;
387 
388 	if (ret) {
389 		drm_err(&i915->drm, "Failed to send tee msg for inv-stream-key-%u, ret=[%d]\n",
390 			session_id, ret);
391 	} else if (msg_out.header.status != 0) {
392 		if (is_fw_err_platform_config(msg_out.header.status)) {
393 			drm_info_once(&i915->drm,
394 				      "PXP inv-stream-key-%u failed due to BIOS/SOC :0x%08x:%s\n",
395 				      session_id, msg_out.header.status,
396 				      fw_err_to_string(msg_out.header.status));
397 		} else {
398 			drm_dbg(&i915->drm, "PXP inv-stream-key-%u failed 0x%08x:%s:\n",
399 				session_id, msg_out.header.status,
400 				fw_err_to_string(msg_out.header.status));
401 			drm_dbg(&i915->drm, "     cmd-detail: ID=[0x%08x],API-Ver-[0x%08x]\n",
402 				msg_in.header.command_id, msg_in.header.api_version);
403 		}
404 	}
405 }
406