xref: /linux/drivers/gpu/drm/xe/xe_pxp.c (revision 2d7eaeb05ae265bd64270376e6a2c0d80244771f)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright(c) 2024 Intel Corporation.
4  */
5 
6 #include "xe_pxp.h"
7 
8 #include <linux/error-injection.h>
9 
10 #include <drm/drm_managed.h>
11 #include <uapi/drm/xe_drm.h>
12 
13 #include <linux/device.h>
14 
15 #include "xe_bo.h"
16 #include "xe_bo_types.h"
17 #include "xe_device_types.h"
18 #include "xe_exec_queue.h"
19 #include "xe_force_wake.h"
20 #include "xe_guc_exec_queue_types.h"
21 #include "xe_guc_submit.h"
22 #include "xe_gsc_proxy.h"
23 #include "xe_gt_types.h"
24 #include "xe_huc.h"
25 #include "xe_hw_engine.h"
26 #include "xe_mmio.h"
27 #include "xe_pm.h"
28 #include "xe_pxp_submit.h"
29 #include "xe_pxp_types.h"
30 #include "xe_uc_fw.h"
31 #include "regs/xe_irq_regs.h"
32 #include "regs/xe_pxp_regs.h"
33 
34 /**
35  * DOC: PXP
36  *
37  * PXP (Protected Xe Path) allows execution and flip to display of protected
38  * (i.e. encrypted) objects. This feature is currently only supported in
39  * integrated parts.
40  */
41 
42 #define ARB_SESSION DRM_XE_PXP_HWDRM_DEFAULT_SESSION /* shorter define */
43 
44 /*
45  * A submission to GSC can take up to 250ms to complete, so use a 300ms
46  * timeout for activation where only one of those is involved. Termination
47  * additionally requires a submission to VCS and an interaction with KCR, so
48  * bump the timeout to 500ms for that.
49  */
50 #define PXP_ACTIVATION_TIMEOUT_MS 300
51 #define PXP_TERMINATION_TIMEOUT_MS 500
52 
53 bool xe_pxp_is_supported(const struct xe_device *xe)
54 {
55 	return xe->info.has_pxp && IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY);
56 }
57 
58 bool xe_pxp_is_enabled(const struct xe_pxp *pxp)
59 {
60 	return pxp;
61 }
62 
63 static bool pxp_prerequisites_done(const struct xe_pxp *pxp)
64 {
65 	struct xe_gt *gt = pxp->gt;
66 	bool huc_ok;
67 	bool ready;
68 
69 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
70 
71 	/*
72 	 * If force_wake fails we could falsely report the prerequisites as not
73 	 * done even if they are; the consequence of this would be that the
74 	 * callers won't go ahead with using PXP, but if force_wake doesn't work
75 	 * the GT is very likely in a bad state so not really a problem to abort
76 	 * PXP. Therefore, we can just log the force_wake error and not escalate
77 	 * it.
78 	 */
79 	XE_WARN_ON(!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL));
80 
81 	/*
82 	 * PXP requires GSC proxy to be initialized. On platforms where the HuC
83 	 * is loaded by the kernel driver (i.e., pre media 35) PXP also requires
84 	 * the HuC to be authenticated by GSC.
85 	 */
86 	huc_ok = MEDIA_VER(gt_to_xe(gt)) >= 35 ||
87 		 xe_huc_is_authenticated(&gt->uc.huc, XE_HUC_AUTH_VIA_GSC);
88 	ready = huc_ok && xe_gsc_proxy_init_done(&gt->uc.gsc);
89 
90 	return ready;
91 }
92 
93 /**
94  * xe_pxp_get_readiness_status - check whether PXP is ready for userspace use
95  * @pxp: the xe_pxp pointer (can be NULL if PXP is disabled)
96  *
97  * Returns: 0 if PXP is not ready yet, 1 if it is ready, a negative errno value
98  * if PXP is not supported/enabled or if something went wrong in the
99  * initialization of the prerequisites. Note that the return values of this
100  * function follow the uapi (see drm_xe_query_pxp_status), so they can be used
101  * directly in the query ioctl.
102  */
103 int xe_pxp_get_readiness_status(struct xe_pxp *pxp)
104 {
105 	int ret = 0;
106 
107 	if (!xe_pxp_is_enabled(pxp))
108 		return -ENODEV;
109 
110 	/* If the GSC FW is in an error state, PXP will never work */
111 	if (xe_uc_fw_status_to_error(pxp->gt->uc.gsc.fw.status))
112 		return -EIO;
113 
114 	/* Same for HuC FW, but only if the kernel owns HuC-loading (i.e. pre-NVL) */
115 	if (MEDIA_VER(gt_to_xe(pxp->gt)) < 35 &&
116 	    xe_uc_fw_status_to_error(pxp->gt->uc.huc.fw.status))
117 		return -EIO;
118 
119 	guard(xe_pm_runtime)(pxp->xe);
120 
121 	/* PXP requires both HuC loaded and GSC proxy initialized */
122 	if (pxp_prerequisites_done(pxp))
123 		ret = 1;
124 
125 	return ret;
126 }
127 
128 static bool pxp_session_is_in_play(struct xe_pxp *pxp, u32 id)
129 {
130 	struct xe_gt *gt = pxp->gt;
131 
132 	return xe_mmio_read32(&gt->mmio, KCR_SIP) & BIT(id);
133 }
134 
135 static int pxp_wait_for_session_state(struct xe_pxp *pxp, u32 id, bool in_play)
136 {
137 	struct xe_gt *gt = pxp->gt;
138 	u32 mask = BIT(id);
139 
140 	return xe_mmio_wait32(&gt->mmio, KCR_SIP, mask, in_play ? mask : 0,
141 			      250, NULL, false);
142 }
143 
144 static void pxp_invalidate_queues(struct xe_pxp *pxp);
145 
146 static int pxp_terminate_hw(struct xe_pxp *pxp)
147 {
148 	struct xe_gt *gt = pxp->gt;
149 	int ret = 0;
150 
151 	drm_dbg(&pxp->xe->drm, "Terminating PXP\n");
152 
153 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
154 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FW_GT))
155 		return -EIO;
156 
157 	/* terminate the hw session */
158 	ret = xe_pxp_submit_session_termination(pxp, ARB_SESSION);
159 	if (ret)
160 		return ret;
161 
162 	ret = pxp_wait_for_session_state(pxp, ARB_SESSION, false);
163 	if (ret)
164 		return ret;
165 
166 	/* Trigger full HW cleanup */
167 	xe_mmio_write32(&gt->mmio, KCR_GLOBAL_TERMINATE, 1);
168 
169 	/* now we can tell the GSC to clean up its own state */
170 	return xe_pxp_submit_session_invalidation(&pxp->gsc_res, ARB_SESSION);
171 }
172 
173 static void mark_termination_in_progress(struct xe_pxp *pxp)
174 {
175 	lockdep_assert_held(&pxp->mutex);
176 
177 	reinit_completion(&pxp->termination);
178 	pxp->status = XE_PXP_TERMINATION_IN_PROGRESS;
179 }
180 
181 static bool pxp_prep_for_termination(struct xe_pxp *pxp)
182 {
183 	lockdep_assert_held(&pxp->mutex);
184 
185 	if (pxp->status == XE_PXP_ACTIVE)
186 		pxp->key_instance++;
187 
188 	/*
189 	 * we'll mark the status as needing termination on resume, so no need to
190 	 * emit a termination now.
191 	 */
192 	if (pxp->status == XE_PXP_SUSPENDED)
193 		return false;
194 
195 	/*
196 	 * If we have a termination already in progress, we need to wait for
197 	 * it to complete before queueing another one. Once the first
198 	 * termination is completed we'll set the state back to
199 	 * NEEDS_TERMINATION and leave it to the pxp start code to issue it.
200 	 */
201 	if (pxp->status == XE_PXP_TERMINATION_IN_PROGRESS) {
202 		pxp->status = XE_PXP_NEEDS_ADDITIONAL_TERMINATION;
203 		return false;
204 	}
205 
206 	mark_termination_in_progress(pxp);
207 
208 	return true;
209 }
210 
211 static void pxp_terminate(struct xe_pxp *pxp, bool hw_only)
212 {
213 	struct xe_device *xe = pxp->xe;
214 	int ret = 0;
215 
216 	if (!wait_for_completion_timeout(&pxp->activation,
217 					 msecs_to_jiffies(PXP_ACTIVATION_TIMEOUT_MS)))
218 		drm_err(&xe->drm, "failed to wait for PXP start before termination\n");
219 
220 	if (!hw_only) {
221 		bool prep_ok;
222 
223 		mutex_lock(&pxp->mutex);
224 
225 		prep_ok = pxp_prep_for_termination(pxp);
226 
227 		mutex_unlock(&pxp->mutex);
228 
229 		if (!prep_ok)
230 			return;
231 
232 		pxp_invalidate_queues(pxp);
233 	} else {
234 		/*
235 		 * The caller of the HW-only termination should have already
236 		 * called pxp_prep_for_termination and marked the termination as
237 		 * in progress.
238 		 */
239 		xe_assert(xe, !completion_done(&pxp->termination));
240 	}
241 
242 	ret = pxp_terminate_hw(pxp);
243 	if (ret) {
244 		drm_err(&xe->drm, "PXP termination failed: %pe\n", ERR_PTR(ret));
245 		mutex_lock(&pxp->mutex);
246 		pxp->status = XE_PXP_ERROR;
247 		complete_all(&pxp->termination);
248 		mutex_unlock(&pxp->mutex);
249 	}
250 }
251 
252 static void pxp_terminate_complete(struct xe_pxp *pxp)
253 {
254 	/*
255 	 * We expect PXP to be in one of 3 states when we get here:
256 	 * - XE_PXP_TERMINATION_IN_PROGRESS: a single termination event was
257 	 * requested and it is now completing, so we're ready to start.
258 	 * - XE_PXP_NEEDS_ADDITIONAL_TERMINATION: a second termination was
259 	 * requested while the first one was still being processed.
260 	 * - XE_PXP_SUSPENDED: PXP is now suspended, so we defer everything to
261 	 * when we come back on resume.
262 	 */
263 	mutex_lock(&pxp->mutex);
264 
265 	switch (pxp->status) {
266 	case XE_PXP_TERMINATION_IN_PROGRESS:
267 		pxp->status = XE_PXP_READY_TO_START;
268 		break;
269 	case XE_PXP_NEEDS_ADDITIONAL_TERMINATION:
270 		pxp->status = XE_PXP_NEEDS_TERMINATION;
271 		break;
272 	case XE_PXP_SUSPENDED:
273 		/* Nothing to do */
274 		break;
275 	default:
276 		drm_err(&pxp->xe->drm,
277 			"PXP termination complete while status was %u\n",
278 			pxp->status);
279 	}
280 
281 	complete_all(&pxp->termination);
282 
283 	mutex_unlock(&pxp->mutex);
284 }
285 
286 static void pxp_events_work(struct work_struct *work)
287 {
288 	struct xe_pxp *pxp = container_of(work, typeof(*pxp), events.work);
289 	struct xe_device *xe = pxp->xe;
290 	bool hw_only = false;
291 	u32 events = 0;
292 
293 	events = atomic_xchg(&pxp->events.pending, 0);
294 
295 	if (!events)
296 		return;
297 
298 	/*
299 	 * If the termination request comes from an irq while we're suspending,
300 	 * then we can defer it to the resume path instead of waking the device
301 	 * up.
302 	 * In the case of the termination on resume the pm reference is taken
303 	 * in xe_pxp_pm_resume() and released here.
304 	 * Note that we do not expect both events to be set at the same time,
305 	 * but if it does happen due to a spurious interrupt we want to behave
306 	 * as if the only request we got was the one from the resume path; this
307 	 * is because the termination prep has already been done in
308 	 * xe_pxp_pm_resume() and it is impossible for any PXP operations to
309 	 * occur between the prep and the termination completion, so there is no
310 	 * need for a new SW prep.
311 	 */
312 	if (events & PXP_TERMINATION_REQUEST_ON_RESUME) {
313 		events &= ~PXP_TERMINATION_REQUEST_IRQ;
314 		hw_only = true;
315 	}
316 
317 	if ((events & PXP_TERMINATION_REQUEST_IRQ) && !xe_pm_runtime_get_if_active(xe))
318 		return;
319 
320 	if (events & PXP_TERMINATION_REQUEST) {
321 		events &= ~PXP_TERMINATION_COMPLETE_IRQ;
322 		pxp_terminate(pxp, hw_only);
323 	}
324 
325 	if (events & PXP_TERMINATION_COMPLETE_IRQ)
326 		pxp_terminate_complete(pxp);
327 
328 	if (events & PXP_TERMINATION_REQUEST)
329 		xe_pm_runtime_put(xe);
330 }
331 
332 /**
333  * xe_pxp_irq_handler - Handles PXP interrupts.
334  * @xe: the xe_device structure
335  * @iir: interrupt vector
336  */
337 void xe_pxp_irq_handler(struct xe_device *xe, u16 iir)
338 {
339 	struct xe_pxp *pxp = xe->pxp;
340 
341 	if (!xe_pxp_is_enabled(pxp)) {
342 		drm_err(&xe->drm, "PXP irq 0x%x received with PXP disabled!\n", iir);
343 		return;
344 	}
345 
346 	if (unlikely(!iir))
347 		return;
348 
349 	if (iir & (KCR_PXP_STATE_TERMINATED_INTERRUPT |
350 		   KCR_APP_TERMINATED_PER_FW_REQ_INTERRUPT))
351 		atomic_or(PXP_TERMINATION_REQUEST_IRQ, &pxp->events.pending);
352 
353 	if (iir & KCR_PXP_STATE_RESET_COMPLETE_INTERRUPT)
354 		atomic_or(PXP_TERMINATION_COMPLETE_IRQ, &pxp->events.pending);
355 
356 	if (atomic_read(&pxp->events.pending))
357 		queue_work(pxp->events.wq, &pxp->events.work);
358 }
359 
360 static int kcr_pxp_set_status(const struct xe_pxp *pxp, bool enable)
361 {
362 	u32 val = enable ? REG_MASKED_FIELD_ENABLE(KCR_INIT_ALLOW_DISPLAY_ME_WRITES) :
363 		  REG_MASKED_FIELD_DISABLE(KCR_INIT_ALLOW_DISPLAY_ME_WRITES);
364 
365 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(pxp->gt), XE_FW_GT);
366 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FW_GT))
367 		return -EIO;
368 
369 	xe_mmio_write32(&pxp->gt->mmio, KCR_INIT, val);
370 
371 	return 0;
372 }
373 
374 static int kcr_pxp_enable(const struct xe_pxp *pxp)
375 {
376 	return kcr_pxp_set_status(pxp, true);
377 }
378 
379 static int kcr_pxp_disable(const struct xe_pxp *pxp)
380 {
381 	return kcr_pxp_set_status(pxp, false);
382 }
383 
384 static void pxp_fini(void *arg)
385 {
386 	struct xe_pxp *pxp = arg;
387 
388 	destroy_workqueue(pxp->events.wq);
389 	xe_pxp_destroy_execution_resources(pxp);
390 
391 	/* no need to explicitly disable KCR since we're going to do an FLR */
392 }
393 
394 /**
395  * xe_pxp_init - initialize PXP support
396  * @xe: the xe_device structure
397  *
398  * Initialize the HW state and allocate the objects required for PXP support.
399  * Note that some of the requirement for PXP support (GSC proxy init, HuC auth)
400  * are performed asynchronously as part of the GSC init. PXP can only be used
401  * after both this function and the async worker have completed.
402  *
403  * Returns 0 if PXP is not supported or if PXP initialization is successful,
404  * other errno value if there is an error during the init.
405  */
406 int xe_pxp_init(struct xe_device *xe)
407 {
408 	struct xe_gt *gt = xe->tiles[0].media_gt;
409 	bool gsc_ok, huc_ok;
410 	struct xe_pxp *pxp;
411 	int err;
412 
413 	if (!xe_pxp_is_supported(xe))
414 		return 0;
415 
416 	/* we only support PXP on single tile devices with a media GT */
417 	if (xe->info.tile_count > 1 || !gt)
418 		return 0;
419 
420 	/* The GSCCS is required for submissions to the GSC FW */
421 	if (!(gt->info.engine_mask & BIT(XE_HW_ENGINE_GSCCS0)))
422 		return 0;
423 
424 	/* PXP requires GSC FW to be available. Pre-NVL it also requires HuC FW */
425 	gsc_ok = xe_uc_fw_is_loadable(&gt->uc.gsc.fw);
426 	huc_ok = MEDIA_VER(xe) >= 35 || xe_uc_fw_is_loadable(&gt->uc.huc.fw);
427 
428 	if (!gsc_ok || !huc_ok) {
429 		drm_info(&xe->drm, "Skipping PXP due to unsatisfied FW deps - GSC=%s, HuC=%s\n",
430 			 str_yes_no(gsc_ok),
431 			 MEDIA_VER(xe) >= 35 ? "not needed" : str_yes_no(huc_ok));
432 		return 0;
433 	}
434 
435 	/*
436 	 * On PTL, older GSC FWs have a bug that can cause them to crash during
437 	 * PXP invalidation events, which leads to a complete loss of power
438 	 * management on the media GT. Therefore, we can't use PXP on FWs that
439 	 * have this bug, which was fixed in PTL GSC build 1396.
440 	 */
441 	if (xe->info.platform == XE_PANTHERLAKE &&
442 	    gt->uc.gsc.fw.versions.found[XE_UC_FW_VER_RELEASE].build < 1396) {
443 		drm_info(&xe->drm, "PXP requires PTL GSC build 1396 or newer\n");
444 		return 0;
445 	}
446 
447 	pxp = drmm_kzalloc(&xe->drm, sizeof(struct xe_pxp), GFP_KERNEL);
448 	if (!pxp) {
449 		err = -ENOMEM;
450 		goto out;
451 	}
452 
453 	INIT_LIST_HEAD(&pxp->queues.list);
454 	spin_lock_init(&pxp->queues.lock);
455 	INIT_WORK(&pxp->events.work, pxp_events_work);
456 	pxp->xe = xe;
457 	pxp->gt = gt;
458 
459 	pxp->key_instance = 1;
460 	pxp->last_suspend_key_instance = 1;
461 
462 	/*
463 	 * we'll use the completions to check if there is an action pending,
464 	 * so we start them as completed and we reinit it when an action is
465 	 * triggered.
466 	 */
467 	init_completion(&pxp->activation);
468 	init_completion(&pxp->termination);
469 	complete_all(&pxp->termination);
470 	complete_all(&pxp->activation);
471 
472 	mutex_init(&pxp->mutex);
473 
474 	pxp->events.wq = alloc_ordered_workqueue("pxp-wq", 0);
475 	if (!pxp->events.wq) {
476 		err = -ENOMEM;
477 		goto out_free;
478 	}
479 
480 	err = kcr_pxp_enable(pxp);
481 	if (err)
482 		goto out_wq;
483 
484 	err = xe_pxp_allocate_execution_resources(pxp);
485 	if (err)
486 		goto out_kcr_disable;
487 
488 	xe->pxp = pxp;
489 
490 	return devm_add_action_or_reset(xe->drm.dev, pxp_fini, pxp);
491 
492 out_kcr_disable:
493 	kcr_pxp_disable(pxp);
494 out_wq:
495 	destroy_workqueue(pxp->events.wq);
496 out_free:
497 	drmm_kfree(&xe->drm, pxp);
498 out:
499 	drm_err(&xe->drm, "PXP initialization failed: %pe\n", ERR_PTR(err));
500 	return err;
501 }
502 
503 static int __pxp_start_arb_session(struct xe_pxp *pxp)
504 {
505 	int ret;
506 
507 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(pxp->gt), XE_FW_GT);
508 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FW_GT))
509 		return -EIO;
510 
511 	if (pxp_session_is_in_play(pxp, ARB_SESSION))
512 		return -EEXIST;
513 
514 	ret = xe_pxp_submit_session_init(&pxp->gsc_res, ARB_SESSION);
515 	if (ret) {
516 		drm_err(&pxp->xe->drm, "Failed to init PXP arb session: %pe\n", ERR_PTR(ret));
517 		return ret;
518 	}
519 
520 	ret = pxp_wait_for_session_state(pxp, ARB_SESSION, true);
521 	if (ret) {
522 		drm_err(&pxp->xe->drm, "PXP ARB session failed to go in play%pe\n", ERR_PTR(ret));
523 		return ret;
524 	}
525 
526 	drm_dbg(&pxp->xe->drm, "PXP ARB session is active\n");
527 	return 0;
528 }
529 
530 /**
531  * xe_pxp_exec_queue_set_type - Mark a queue as using PXP
532  * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled)
533  * @q: the queue to mark as using PXP
534  * @type: the type of PXP session this queue will use
535  *
536  * Returns 0 if the selected PXP type is supported, -ENODEV otherwise.
537  */
538 int xe_pxp_exec_queue_set_type(struct xe_pxp *pxp, struct xe_exec_queue *q, u8 type)
539 {
540 	if (!xe_pxp_is_enabled(pxp))
541 		return -ENODEV;
542 
543 	/* we only support HWDRM sessions right now */
544 	xe_assert(pxp->xe, type == DRM_XE_PXP_TYPE_HWDRM);
545 
546 	q->pxp.type = type;
547 
548 	return 0;
549 }
550 
551 static int __exec_queue_add(struct xe_pxp *pxp, struct xe_exec_queue *q)
552 {
553 	int ret = 0;
554 
555 	/*
556 	 * A queue can be added to the list only if the PXP is in active status,
557 	 * otherwise the termination might not handle it correctly.
558 	 */
559 	mutex_lock(&pxp->mutex);
560 
561 	if (pxp->status == XE_PXP_ACTIVE) {
562 		spin_lock_irq(&pxp->queues.lock);
563 		list_add_tail(&q->pxp.link, &pxp->queues.list);
564 		spin_unlock_irq(&pxp->queues.lock);
565 	} else if (pxp->status == XE_PXP_ERROR || pxp->status == XE_PXP_SUSPENDED) {
566 		ret = -EIO;
567 	} else {
568 		ret = -EBUSY; /* try again later */
569 	}
570 
571 	mutex_unlock(&pxp->mutex);
572 
573 	return ret;
574 }
575 
576 static int pxp_start(struct xe_pxp *pxp, u8 type)
577 {
578 	int ret = 0;
579 	bool restart;
580 
581 	if (!xe_pxp_is_enabled(pxp))
582 		return -ENODEV;
583 
584 	/* we only support HWDRM sessions right now */
585 	xe_assert(pxp->xe, type == DRM_XE_PXP_TYPE_HWDRM);
586 
587 	/* get_readiness_status() returns 0 for in-progress and 1 for done */
588 	ret = xe_pxp_get_readiness_status(pxp);
589 	if (ret <= 0)
590 		return ret ?: -EBUSY;
591 
592 	ret = 0;
593 
594 wait_for_idle:
595 	/*
596 	 * if there is an action in progress, wait for it. We need to wait
597 	 * outside the lock because the completion is done from within the lock.
598 	 * Note that the two actions should never be pending at the same time.
599 	 */
600 	if (!wait_for_completion_timeout(&pxp->termination,
601 					 msecs_to_jiffies(PXP_TERMINATION_TIMEOUT_MS)))
602 		return -ETIMEDOUT;
603 
604 	if (!wait_for_completion_timeout(&pxp->activation,
605 					 msecs_to_jiffies(PXP_ACTIVATION_TIMEOUT_MS)))
606 		return -ETIMEDOUT;
607 
608 	restart = false;
609 
610 	mutex_lock(&pxp->mutex);
611 
612 	/* If PXP is not already active, turn it on */
613 	switch (pxp->status) {
614 	case XE_PXP_ERROR:
615 		ret = -EIO;
616 		goto out_unlock;
617 	case XE_PXP_ACTIVE:
618 		goto out_unlock;
619 	case XE_PXP_READY_TO_START:
620 		pxp->status = XE_PXP_START_IN_PROGRESS;
621 		reinit_completion(&pxp->activation);
622 		break;
623 	case XE_PXP_START_IN_PROGRESS:
624 		/* If a start is in progress then the completion must not be done */
625 		XE_WARN_ON(completion_done(&pxp->activation));
626 		restart = true;
627 		goto out_unlock;
628 	case XE_PXP_NEEDS_TERMINATION:
629 		mark_termination_in_progress(pxp);
630 		break;
631 	case XE_PXP_TERMINATION_IN_PROGRESS:
632 	case XE_PXP_NEEDS_ADDITIONAL_TERMINATION:
633 		/* If a termination is in progress then the completion must not be done */
634 		XE_WARN_ON(completion_done(&pxp->termination));
635 		restart = true;
636 		goto out_unlock;
637 	case XE_PXP_SUSPENDED:
638 	default:
639 		drm_err(&pxp->xe->drm, "unexpected state during PXP start: %u\n", pxp->status);
640 		ret = -EIO;
641 		goto out_unlock;
642 	}
643 
644 	mutex_unlock(&pxp->mutex);
645 
646 	if (!completion_done(&pxp->termination)) {
647 		ret = pxp_terminate_hw(pxp);
648 		if (ret) {
649 			drm_err(&pxp->xe->drm, "PXP termination failed before start\n");
650 			mutex_lock(&pxp->mutex);
651 			pxp->status = XE_PXP_ERROR;
652 			complete_all(&pxp->termination);
653 
654 			goto out_unlock;
655 		}
656 
657 		goto wait_for_idle;
658 	}
659 
660 	/* All the cases except for start should have exited earlier */
661 	XE_WARN_ON(completion_done(&pxp->activation));
662 	ret = __pxp_start_arb_session(pxp);
663 
664 	mutex_lock(&pxp->mutex);
665 
666 	complete_all(&pxp->activation);
667 
668 	/*
669 	 * Any other process should wait until the state goes away from
670 	 * XE_PXP_START_IN_PROGRESS, so if the state is not that something went
671 	 * wrong. Mark the status as needing termination and try again.
672 	 */
673 	if (pxp->status != XE_PXP_START_IN_PROGRESS) {
674 		drm_err(&pxp->xe->drm, "unexpected state after PXP start: %u\n", pxp->status);
675 		pxp->status = XE_PXP_NEEDS_TERMINATION;
676 		restart = true;
677 		goto out_unlock;
678 	}
679 
680 	/* If everything went ok, update the status and add the queue to the list */
681 	if (!ret)
682 		pxp->status = XE_PXP_ACTIVE;
683 	else
684 		pxp->status = XE_PXP_ERROR;
685 
686 out_unlock:
687 	mutex_unlock(&pxp->mutex);
688 
689 	if (restart)
690 		goto wait_for_idle;
691 
692 	return ret;
693 }
694 
695 /**
696  * xe_pxp_exec_queue_add - add a queue to the PXP list
697  * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled)
698  * @q: the queue to add to the list
699  *
700  * If PXP is enabled and the prerequisites are done, start the PXP default
701  * session (if not already running) and add the queue to the PXP list.
702  *
703  * Returns 0 if the PXP session is running and the queue is in the list,
704  * -ENODEV if PXP is disabled, -EBUSY if the PXP prerequisites are not done,
705  * other errno value if something goes wrong during the session start.
706  */
707 int xe_pxp_exec_queue_add(struct xe_pxp *pxp, struct xe_exec_queue *q)
708 {
709 	int ret;
710 
711 	if (!xe_pxp_is_enabled(pxp))
712 		return -ENODEV;
713 
714 	/*
715 	 * Runtime suspend kills PXP, so we take a reference to prevent it from
716 	 * happening while we have active queues that use PXP
717 	 */
718 	xe_pm_runtime_get(pxp->xe);
719 
720 start:
721 	ret = pxp_start(pxp, q->pxp.type);
722 
723 	if (!ret) {
724 		ret = __exec_queue_add(pxp, q);
725 		if (ret == -EBUSY)
726 			goto start;
727 	}
728 
729 	/*
730 	 * in the successful case the PM ref is released from
731 	 * xe_pxp_exec_queue_remove
732 	 */
733 	if (ret)
734 		xe_pm_runtime_put(pxp->xe);
735 
736 	return ret;
737 }
738 ALLOW_ERROR_INJECTION(xe_pxp_exec_queue_add, ERRNO);
739 
740 static void __pxp_exec_queue_remove(struct xe_pxp *pxp, struct xe_exec_queue *q, bool lock)
741 {
742 	bool need_pm_put = false;
743 
744 	if (!xe_pxp_is_enabled(pxp))
745 		return;
746 
747 	if (lock)
748 		spin_lock_irq(&pxp->queues.lock);
749 
750 	if (!list_empty(&q->pxp.link)) {
751 		list_del_init(&q->pxp.link);
752 		need_pm_put = true;
753 	}
754 
755 	q->pxp.type = DRM_XE_PXP_TYPE_NONE;
756 
757 	if (lock)
758 		spin_unlock_irq(&pxp->queues.lock);
759 
760 	if (need_pm_put)
761 		xe_pm_runtime_put(pxp->xe);
762 }
763 
764 /**
765  * xe_pxp_exec_queue_remove - remove a queue from the PXP list
766  * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled)
767  * @q: the queue to remove from the list
768  *
769  * If PXP is enabled and the exec_queue is in the list, the queue will be
770  * removed from the list and its PM reference will be released. It is safe to
771  * call this function multiple times for the same queue.
772  */
773 void xe_pxp_exec_queue_remove(struct xe_pxp *pxp, struct xe_exec_queue *q)
774 {
775 	__pxp_exec_queue_remove(pxp, q, true);
776 }
777 
778 static void pxp_invalidate_queues(struct xe_pxp *pxp)
779 {
780 	struct xe_exec_queue *tmp, *q;
781 	LIST_HEAD(to_clean);
782 
783 	spin_lock_irq(&pxp->queues.lock);
784 
785 	list_for_each_entry_safe(q, tmp, &pxp->queues.list, pxp.link) {
786 		q = xe_exec_queue_get_unless_zero(q);
787 		if (!q)
788 			continue;
789 
790 		list_move_tail(&q->pxp.link, &to_clean);
791 	}
792 	spin_unlock_irq(&pxp->queues.lock);
793 
794 	list_for_each_entry_safe(q, tmp, &to_clean, pxp.link) {
795 		drm_dbg(&pxp->xe->drm,
796 			"Killing queue due to PXP termination: eclass=%s, guc_id=%d\n",
797 			xe_hw_engine_class_to_str(q->class), q->guc->id);
798 
799 		xe_exec_queue_kill(q);
800 
801 		/*
802 		 * We hold a ref to the queue so there is no risk of racing with
803 		 * the calls to exec_queue_remove coming from exec_queue_destroy.
804 		 */
805 		__pxp_exec_queue_remove(pxp, q, false);
806 
807 		xe_exec_queue_put(q);
808 	}
809 }
810 
811 /**
812  * xe_pxp_key_assign - mark a BO as using the current PXP key iteration
813  * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled)
814  * @bo: the BO to mark
815  *
816  * Returns: -ENODEV if PXP is disabled, 0 otherwise.
817  */
818 int xe_pxp_key_assign(struct xe_pxp *pxp, struct xe_bo *bo)
819 {
820 	if (!xe_pxp_is_enabled(pxp))
821 		return -ENODEV;
822 
823 	xe_assert(pxp->xe, !bo->pxp_key_instance);
824 
825 	/*
826 	 * Note that the PXP key handling is inherently racey, because the key
827 	 * can theoretically change at any time (although it's unlikely to do
828 	 * so without triggers), even right after we copy it. Taking a lock
829 	 * wouldn't help because the value might still change as soon as we
830 	 * release the lock.
831 	 * Userspace needs to handle the fact that their BOs can go invalid at
832 	 * any point.
833 	 */
834 	bo->pxp_key_instance = pxp->key_instance;
835 
836 	return 0;
837 }
838 
839 /**
840  * xe_pxp_bo_key_check - check if the key used by a xe_bo is valid
841  * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled)
842  * @bo: the BO we want to check
843  *
844  * Checks whether a BO was encrypted with the current key or an obsolete one.
845  *
846  * Returns: 0 if the key is valid, -ENODEV if PXP is disabled, -EINVAL if the
847  * BO is not using PXP,  -ENOEXEC if the key is not valid.
848  */
849 int xe_pxp_bo_key_check(struct xe_pxp *pxp, struct xe_bo *bo)
850 {
851 	if (!xe_pxp_is_enabled(pxp))
852 		return -ENODEV;
853 
854 	if (!xe_bo_is_protected(bo))
855 		return -EINVAL;
856 
857 	xe_assert(pxp->xe, bo->pxp_key_instance);
858 
859 	/*
860 	 * Note that the PXP key handling is inherently racey, because the key
861 	 * can theoretically change at any time (although it's unlikely to do
862 	 * so without triggers), even right after we check it. Taking a lock
863 	 * wouldn't help because the value might still change as soon as we
864 	 * release the lock.
865 	 * We mitigate the risk by checking the key at multiple points (on each
866 	 * submission involving the BO and right before flipping it on the
867 	 * display), but there is still a very small chance that we could
868 	 * operate on an invalid BO for a single submission or a single frame
869 	 * flip. This is a compromise made to protect the encrypted data (which
870 	 * is what the key termination is for).
871 	 */
872 	if (bo->pxp_key_instance != pxp->key_instance)
873 		return -ENOEXEC;
874 
875 	return 0;
876 }
877 
878 /**
879  * xe_pxp_obj_key_check - check if the key used by a drm_gem_obj is valid
880  * @obj: the drm_gem_obj we want to check
881  *
882  * Checks whether a drm_gem_obj was encrypted with the current key or an
883  * obsolete one.
884  *
885  * Returns: 0 if the key is valid, -ENODEV if PXP is disabled, -EINVAL if the
886  * obj is not using PXP,  -ENOEXEC if the key is not valid.
887  */
888 int xe_pxp_obj_key_check(struct drm_gem_object *obj)
889 {
890 	struct xe_bo *bo = gem_to_xe_bo(obj);
891 	struct xe_device *xe = xe_bo_device(bo);
892 	struct xe_pxp *pxp = xe->pxp;
893 
894 	return xe_pxp_bo_key_check(pxp, bo);
895 }
896 
897 /**
898  * xe_pxp_pm_suspend - prepare PXP for HW suspend
899  * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled)
900  *
901  * Makes sure all PXP actions have completed and invalidates all PXP queues
902  * and objects before we go into a suspend state.
903  *
904  * Returns: 0 if successful, a negative errno value otherwise.
905  */
906 int xe_pxp_pm_suspend(struct xe_pxp *pxp)
907 {
908 	bool needs_queue_inval = false;
909 	int ret = 0;
910 
911 	if (!xe_pxp_is_enabled(pxp))
912 		return 0;
913 
914 wait_for_activation:
915 	if (!wait_for_completion_timeout(&pxp->activation,
916 					 msecs_to_jiffies(PXP_ACTIVATION_TIMEOUT_MS)))
917 		ret = -ETIMEDOUT;
918 
919 	mutex_lock(&pxp->mutex);
920 
921 	switch (pxp->status) {
922 	case XE_PXP_ERROR:
923 	case XE_PXP_READY_TO_START:
924 	case XE_PXP_SUSPENDED:
925 	case XE_PXP_TERMINATION_IN_PROGRESS:
926 	case XE_PXP_NEEDS_ADDITIONAL_TERMINATION:
927 		/*
928 		 * If PXP is not running there is nothing to cleanup. If there
929 		 * is a termination pending then no need to issue another one.
930 		 */
931 		break;
932 	case XE_PXP_START_IN_PROGRESS:
933 		mutex_unlock(&pxp->mutex);
934 		goto wait_for_activation;
935 	case XE_PXP_NEEDS_TERMINATION:
936 		/* If PXP was never used we can skip the cleanup */
937 		if (pxp->key_instance == pxp->last_suspend_key_instance)
938 			break;
939 		fallthrough;
940 	case XE_PXP_ACTIVE:
941 		pxp->key_instance++;
942 		pxp->needs_termination_on_resume = true;
943 		needs_queue_inval = true;
944 		break;
945 	}
946 
947 	/*
948 	 * We set this even if we were in error state, hoping the suspend clears
949 	 * the error. Worse case we fail again and go in error state again.
950 	 */
951 	pxp->status = XE_PXP_SUSPENDED;
952 
953 	mutex_unlock(&pxp->mutex);
954 
955 	if (needs_queue_inval)
956 		pxp_invalidate_queues(pxp);
957 
958 	/*
959 	 * if there is a termination in progress, wait for it.
960 	 * We need to wait outside the lock because the completion is done from
961 	 * within the lock
962 	 */
963 	if (!wait_for_completion_timeout(&pxp->termination,
964 					 msecs_to_jiffies(PXP_TERMINATION_TIMEOUT_MS)))
965 		ret = -ETIMEDOUT;
966 
967 	pxp->last_suspend_key_instance = pxp->key_instance;
968 
969 	return ret;
970 }
971 
972 /**
973  * xe_pxp_pm_resume - re-init PXP after HW suspend
974  * @pxp: the xe->pxp pointer (it will be NULL if PXP is disabled)
975  */
976 void xe_pxp_pm_resume(struct xe_pxp *pxp)
977 {
978 	bool has_pm = false;
979 	int err;
980 
981 	if (!xe_pxp_is_enabled(pxp))
982 		return;
983 
984 	err = kcr_pxp_enable(pxp);
985 
986 	/*
987 	 * We want to avoid the device runtime suspending before we're done with
988 	 * the termination queued below, so we need a runtime PM reference; we
989 	 * can't call the rpm functions from within the PXP lock, so we take the
990 	 * ref here. Note that we don't want the rpm resume code to actually run
991 	 * here as that would call back into this function, but as long as we
992 	 * don't enable DPM_FLAG_SMART_SUSPEND (which we currently do not) we're
993 	 * guaranteed to not be runtime suspended at this point, so we can
994 	 * safely use the get_noresume variant.
995 	 */
996 	if (pxp->needs_termination_on_resume) {
997 		has_pm = true;
998 
999 		xe_assert(pxp->xe, !dev_pm_smart_suspend(pxp->xe->drm.dev));
1000 		xe_pm_runtime_get_noresume(pxp->xe);
1001 	}
1002 
1003 	mutex_lock(&pxp->mutex);
1004 
1005 	xe_assert(pxp->xe, pxp->status == XE_PXP_SUSPENDED);
1006 
1007 	if (err) {
1008 		pxp->status = XE_PXP_ERROR;
1009 	} else {
1010 		pxp->status = XE_PXP_NEEDS_TERMINATION;
1011 
1012 		if (pxp->needs_termination_on_resume) {
1013 			pxp->needs_termination_on_resume = false;
1014 
1015 			/*
1016 			 * We can't call pxp_terminate_hw directly from here
1017 			 * because we're not allowed to do allocations within
1018 			 * the rpm resume call, so we defer the termination to
1019 			 * the worker that we use for the termination irqs.
1020 			 * However, we do not want any PXP ops to go through
1021 			 * between the suspend completing and the worker
1022 			 * starting, so we need to do the termination prep
1023 			 * immediately, which will mark the termination as in
1024 			 * progress and stall PXP ops.
1025 			 */
1026 			if (pxp_prep_for_termination(pxp)) {
1027 				has_pm = false; /* move PM ref ownership to worker */
1028 
1029 				atomic_or(PXP_TERMINATION_REQUEST_ON_RESUME, &pxp->events.pending);
1030 				queue_work(pxp->events.wq, &pxp->events.work);
1031 			}
1032 		}
1033 	}
1034 
1035 	mutex_unlock(&pxp->mutex);
1036 
1037 	if (has_pm)
1038 		xe_pm_runtime_put(pxp->xe);
1039 }
1040