1 // SPDX-License-Identifier: MIT
3 * Copyright(c) 2023 Intel Corporation.
6 #include "gem/i915_gem_internal.h"
8 #include "gt/intel_context.h"
9 #include "gt/intel_gt.h"
10 #include "gt/uc/intel_gsc_fw.h"
11 #include "gt/uc/intel_gsc_uc_heci_cmd_submit.h"
14 #include "intel_pxp.h"
15 #include "intel_pxp_cmd_interface_42.h"
16 #include "intel_pxp_cmd_interface_43.h"
17 #include "intel_pxp_gsccs.h"
18 #include "intel_pxp_types.h"
21 is_fw_err_platform_config(struct intel_pxp *pxp, u32 type)
24 case PXP_STATUS_ERROR_API_VERSION:
25 case PXP_STATUS_PLATFCONFIG_KF1_NOVERIF:
26 case PXP_STATUS_PLATFCONFIG_KF1_BAD:
27 pxp->platform_cfg_is_bad = true;
36 fw_err_to_string(u32 type)
39 case PXP_STATUS_ERROR_API_VERSION:
40 return "ERR_API_VERSION";
41 case PXP_STATUS_NOT_READY:
42 return "ERR_NOT_READY";
43 case PXP_STATUS_PLATFCONFIG_KF1_NOVERIF:
44 case PXP_STATUS_PLATFCONFIG_KF1_BAD:
45 return "ERR_PLATFORM_CONFIG";
53 gsccs_send_message(struct intel_pxp *pxp,
54 void *msg_in, size_t msg_in_size,
55 void *msg_out, size_t msg_out_size_max,
57 u64 *gsc_msg_handle_retry)
59 struct intel_gt *gt = pxp->ctrl_gt;
60 struct drm_i915_private *i915 = gt->i915;
61 struct gsccs_session_resources *exec_res = &pxp->gsccs_res;
62 struct intel_gsc_mtl_header *header = exec_res->pkt_vaddr;
63 struct intel_gsc_heci_non_priv_pkt pkt;
71 max_msg_size = PXP43_MAX_HECI_INOUT_SIZE - sizeof(*header);
73 if (msg_in_size > max_msg_size || msg_out_size_max > max_msg_size)
76 if (!exec_res->pkt_vma || !exec_res->bb_vma)
79 GEM_BUG_ON(exec_res->pkt_vma->size < (2 * PXP43_MAX_HECI_INOUT_SIZE));
81 mutex_lock(&pxp->tee_mutex);
83 memset(header, 0, sizeof(*header));
84 intel_gsc_uc_heci_cmd_emit_mtl_header(header, HECI_MEADDRESS_PXP,
85 msg_in_size + sizeof(*header),
86 exec_res->host_session_handle);
88 /* check if this is a host-session-handle cleanup call (empty packet) */
89 if (!msg_in && !msg_out)
90 header->flags |= GSC_INFLAG_MSG_CLEANUP;
92 /* copy caller provided gsc message handle if this is polling for a prior msg completion */
93 header->gsc_message_handle = *gsc_msg_handle_retry;
95 /* NOTE: zero size packets are used for session-cleanups */
96 if (msg_in && msg_in_size)
97 memcpy(exec_res->pkt_vaddr + sizeof(*header), msg_in, msg_in_size);
99 pkt.addr_in = i915_vma_offset(exec_res->pkt_vma);
100 pkt.size_in = header->message_size;
101 pkt.addr_out = pkt.addr_in + PXP43_MAX_HECI_INOUT_SIZE;
102 pkt.size_out = msg_out_size_max + sizeof(*header);
103 pkt.heci_pkt_vma = exec_res->pkt_vma;
104 pkt.bb_vma = exec_res->bb_vma;
107 * Before submitting, let's clear-out the validity marker on the reply offset.
108 * We use offset PXP43_MAX_HECI_INOUT_SIZE for reply location so point header there.
110 header = exec_res->pkt_vaddr + PXP43_MAX_HECI_INOUT_SIZE;
111 header->validity_marker = 0;
113 ret = intel_gsc_uc_heci_cmd_submit_nonpriv(>->uc.gsc,
114 exec_res->ce, &pkt, exec_res->bb_vaddr,
115 GSC_HECI_REPLY_LATENCY_MS);
117 drm_err(&i915->drm, "failed to send gsc PXP msg (%d)\n", ret);
121 /* Response validity marker, status and busyness */
122 if (header->validity_marker != GSC_HECI_VALIDITY_MARKER) {
123 drm_err(&i915->drm, "gsc PXP reply with invalid validity marker\n");
127 if (header->status != 0) {
128 drm_dbg(&i915->drm, "gsc PXP reply status has error = 0x%08x\n",
133 if (header->flags & GSC_OUTFLAG_MSG_PENDING) {
134 drm_dbg(&i915->drm, "gsc PXP reply is busy\n");
136 * When the GSC firmware replies with pending bit, it means that the requested
137 * operation has begun but the completion is pending and the caller needs
138 * to re-request with the gsc_message_handle that was returned by the firmware.
139 * until the pending bit is turned off.
141 *gsc_msg_handle_retry = header->gsc_message_handle;
146 reply_size = header->message_size - sizeof(*header);
147 if (reply_size > msg_out_size_max) {
148 drm_warn(&i915->drm, "caller with insufficient PXP reply size %u (%zu)\n",
149 reply_size, msg_out_size_max);
150 reply_size = msg_out_size_max;
154 memcpy(msg_out, exec_res->pkt_vaddr + PXP43_MAX_HECI_INOUT_SIZE + sizeof(*header),
157 *msg_out_len = reply_size;
160 mutex_unlock(&pxp->tee_mutex);
165 gsccs_send_message_retry_complete(struct intel_pxp *pxp,
166 void *msg_in, size_t msg_in_size,
167 void *msg_out, size_t msg_out_size_max,
170 u64 gsc_session_retry = 0;
174 * Keep sending request if GSC firmware was busy. Based on fw specs +
175 * sw overhead (and testing) we expect a worst case pending-bit delay of
176 * GSC_PENDING_RETRY_MAXCOUNT x GSC_PENDING_RETRY_PAUSE_MS millisecs.
179 ret = gsccs_send_message(pxp, msg_in, msg_in_size, msg_out, msg_out_size_max,
180 msg_out_len, &gsc_session_retry);
181 /* Only try again if gsc says so */
185 msleep(GSC_PENDING_RETRY_PAUSE_MS);
186 } while (++tries < GSC_PENDING_RETRY_MAXCOUNT);
191 bool intel_pxp_gsccs_is_ready_for_sessions(struct intel_pxp *pxp)
194 * GSC-fw loading, HuC-fw loading, HuC-fw authentication and
195 * GSC-proxy init flow (requiring an mei component driver)
196 * must all occur first before we can start requesting for PXP
197 * sessions. Checking for completion on HuC authentication and
198 * gsc-proxy init flow (the last set of dependencies that
199 * are out of order) will suffice.
201 if (intel_huc_is_authenticated(&pxp->ctrl_gt->uc.huc, INTEL_HUC_AUTH_BY_GSC) &&
202 intel_gsc_uc_fw_proxy_init_done(&pxp->ctrl_gt->uc.gsc, true))
208 int intel_pxp_gsccs_create_session(struct intel_pxp *pxp,
211 struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
212 struct pxp43_create_arb_in msg_in = {};
213 struct pxp43_create_arb_out msg_out = {};
216 msg_in.header.api_version = PXP_APIVER(4, 3);
217 msg_in.header.command_id = PXP43_CMDID_INIT_SESSION;
218 msg_in.header.stream_id = (FIELD_PREP(PXP43_INIT_SESSION_APPID, arb_session_id) |
219 FIELD_PREP(PXP43_INIT_SESSION_VALID, 1) |
220 FIELD_PREP(PXP43_INIT_SESSION_APPTYPE, 0));
221 msg_in.header.buffer_len = sizeof(msg_in) - sizeof(msg_in.header);
222 msg_in.protection_mode = PXP43_INIT_SESSION_PROTECTION_ARB;
224 ret = gsccs_send_message_retry_complete(pxp,
225 &msg_in, sizeof(msg_in),
226 &msg_out, sizeof(msg_out), NULL);
228 drm_err(&i915->drm, "Failed to init session %d, ret=[%d]\n", arb_session_id, ret);
229 } else if (msg_out.header.status != 0) {
230 if (is_fw_err_platform_config(pxp, msg_out.header.status)) {
231 drm_info_once(&i915->drm,
232 "PXP init-session-%d failed due to BIOS/SOC:0x%08x:%s\n",
233 arb_session_id, msg_out.header.status,
234 fw_err_to_string(msg_out.header.status));
236 drm_dbg(&i915->drm, "PXP init-session-%d failed 0x%08x:%st:\n",
237 arb_session_id, msg_out.header.status,
238 fw_err_to_string(msg_out.header.status));
239 drm_dbg(&i915->drm, " cmd-detail: ID=[0x%08x],API-Ver-[0x%08x]\n",
240 msg_in.header.command_id, msg_in.header.api_version);
247 void intel_pxp_gsccs_end_arb_fw_session(struct intel_pxp *pxp, u32 session_id)
249 struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
250 struct pxp42_inv_stream_key_in msg_in = {};
251 struct pxp42_inv_stream_key_out msg_out = {};
255 * Stream key invalidation reuses the same version 4.2 input/output
256 * command format but firmware requires 4.3 API interaction
258 msg_in.header.api_version = PXP_APIVER(4, 3);
259 msg_in.header.command_id = PXP42_CMDID_INVALIDATE_STREAM_KEY;
260 msg_in.header.buffer_len = sizeof(msg_in) - sizeof(msg_in.header);
262 msg_in.header.stream_id = FIELD_PREP(PXP_CMDHDR_EXTDATA_SESSION_VALID, 1);
263 msg_in.header.stream_id |= FIELD_PREP(PXP_CMDHDR_EXTDATA_APP_TYPE, 0);
264 msg_in.header.stream_id |= FIELD_PREP(PXP_CMDHDR_EXTDATA_SESSION_ID, session_id);
266 ret = gsccs_send_message_retry_complete(pxp,
267 &msg_in, sizeof(msg_in),
268 &msg_out, sizeof(msg_out), NULL);
270 drm_err(&i915->drm, "Failed to inv-stream-key-%u, ret=[%d]\n",
272 } else if (msg_out.header.status != 0) {
273 if (is_fw_err_platform_config(pxp, msg_out.header.status)) {
274 drm_info_once(&i915->drm,
275 "PXP inv-stream-key-%u failed due to BIOS/SOC :0x%08x:%s\n",
276 session_id, msg_out.header.status,
277 fw_err_to_string(msg_out.header.status));
279 drm_dbg(&i915->drm, "PXP inv-stream-key-%u failed 0x%08x:%s:\n",
280 session_id, msg_out.header.status,
281 fw_err_to_string(msg_out.header.status));
282 drm_dbg(&i915->drm, " cmd-detail: ID=[0x%08x],API-Ver-[0x%08x]\n",
283 msg_in.header.command_id, msg_in.header.api_version);
289 gsccs_cleanup_fw_host_session_handle(struct intel_pxp *pxp)
291 struct drm_i915_private *i915 = pxp->ctrl_gt->i915;
294 ret = gsccs_send_message_retry_complete(pxp, NULL, 0, NULL, 0, NULL);
296 drm_dbg(&i915->drm, "Failed to send gsccs msg host-session-cleanup: ret=[%d]\n",
301 gsccs_destroy_execution_resource(struct intel_pxp *pxp)
303 struct gsccs_session_resources *exec_res = &pxp->gsccs_res;
305 if (exec_res->host_session_handle)
306 gsccs_cleanup_fw_host_session_handle(pxp);
308 intel_context_put(exec_res->ce);
309 if (exec_res->bb_vma)
310 i915_vma_unpin_and_release(&exec_res->bb_vma, I915_VMA_RELEASE_MAP);
311 if (exec_res->pkt_vma)
312 i915_vma_unpin_and_release(&exec_res->pkt_vma, I915_VMA_RELEASE_MAP);
314 memset(exec_res, 0, sizeof(*exec_res));
318 gsccs_create_buffer(struct intel_gt *gt,
319 const char *bufname, size_t size,
320 struct i915_vma **vma, void **map)
322 struct drm_i915_private *i915 = gt->i915;
323 struct drm_i915_gem_object *obj;
326 obj = i915_gem_object_create_internal(i915, size);
328 drm_err(&i915->drm, "Failed to allocate gsccs backend %s.\n", bufname);
333 *vma = i915_vma_instance(obj, gt->vm, NULL);
335 drm_err(&i915->drm, "Failed to vma-instance gsccs backend %s.\n", bufname);
340 /* return a virtual pointer */
341 *map = i915_gem_object_pin_map_unlocked(obj, intel_gt_coherent_map_type(gt, obj, true));
343 drm_err(&i915->drm, "Failed to map gsccs backend %s.\n", bufname);
348 /* all PXP sessions commands are treated as non-privileged */
349 err = i915_vma_pin(*vma, 0, 0, PIN_USER);
351 drm_err(&i915->drm, "Failed to vma-pin gsccs backend %s.\n", bufname);
358 i915_gem_object_unpin_map(obj);
360 i915_gem_object_put(obj);
369 gsccs_allocate_execution_resource(struct intel_pxp *pxp)
371 struct intel_gt *gt = pxp->ctrl_gt;
372 struct gsccs_session_resources *exec_res = &pxp->gsccs_res;
373 struct intel_engine_cs *engine = gt->engine[GSC0];
374 struct intel_context *ce;
378 * First, ensure the GSC engine is present.
379 * NOTE: Backend would only be called with the correct gt.
385 * Now, allocate, pin and map two objects, one for the heci message packet
386 * and another for the batch buffer we submit into GSC engine (that includes the packet).
387 * NOTE: GSC-CS backend is currently only supported on MTL, so we allocate shmem.
389 err = gsccs_create_buffer(pxp->ctrl_gt, "Heci Packet",
390 2 * PXP43_MAX_HECI_INOUT_SIZE,
391 &exec_res->pkt_vma, &exec_res->pkt_vaddr);
395 err = gsccs_create_buffer(pxp->ctrl_gt, "Batch Buffer", PAGE_SIZE,
396 &exec_res->bb_vma, &exec_res->bb_vaddr);
400 /* Finally, create an intel_context to be used during the submission */
401 ce = intel_context_create(engine);
403 drm_err(>->i915->drm, "Failed creating gsccs backend ctx\n");
409 ce->vm = i915_vm_get(pxp->ctrl_gt->vm);
412 /* initialize host-session-handle (for all i915-to-gsc-firmware PXP cmds) */
413 get_random_bytes(&exec_res->host_session_handle, sizeof(exec_res->host_session_handle));
418 i915_vma_unpin_and_release(&exec_res->bb_vma, I915_VMA_RELEASE_MAP);
420 i915_vma_unpin_and_release(&exec_res->pkt_vma, I915_VMA_RELEASE_MAP);
421 memset(exec_res, 0, sizeof(*exec_res));
426 void intel_pxp_gsccs_fini(struct intel_pxp *pxp)
428 intel_wakeref_t wakeref;
430 gsccs_destroy_execution_resource(pxp);
431 with_intel_runtime_pm(&pxp->ctrl_gt->i915->runtime_pm, wakeref)
432 intel_pxp_fini_hw(pxp);
435 int intel_pxp_gsccs_init(struct intel_pxp *pxp)
438 intel_wakeref_t wakeref;
440 ret = gsccs_allocate_execution_resource(pxp);
442 with_intel_runtime_pm(&pxp->ctrl_gt->i915->runtime_pm, wakeref)
443 intel_pxp_init_hw(pxp);