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