1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright(c) 2024, Intel Corporation. All rights reserved.
4  */
5 
6 #ifndef __XE_PXP_TYPES_H__
7 #define __XE_PXP_TYPES_H__
8 
9 #include <linux/completion.h>
10 #include <linux/iosys-map.h>
11 #include <linux/mutex.h>
12 #include <linux/spinlock.h>
13 #include <linux/types.h>
14 #include <linux/workqueue.h>
15 
16 struct xe_bo;
17 struct xe_exec_queue;
18 struct xe_device;
19 struct xe_gt;
20 struct xe_vm;
21 
22 enum xe_pxp_status {
23 	XE_PXP_ERROR = -1,
24 	XE_PXP_NEEDS_TERMINATION = 0, /* starting status */
25 	XE_PXP_NEEDS_ADDITIONAL_TERMINATION,
26 	XE_PXP_TERMINATION_IN_PROGRESS,
27 	XE_PXP_READY_TO_START,
28 	XE_PXP_START_IN_PROGRESS,
29 	XE_PXP_ACTIVE,
30 	XE_PXP_SUSPENDED,
31 };
32 
33 /**
34  * struct xe_pxp_gsc_client_resources - resources for GSC submission by a PXP
35  * client. The GSC FW supports multiple GSC client active at the same time.
36  */
37 struct xe_pxp_gsc_client_resources {
38 	/**
39 	 * @host_session_handle: handle used to identify the client in messages
40 	 * sent to the GSC firmware.
41 	 */
42 	u64 host_session_handle;
43 	/** @vm: VM used for PXP submissions to the GSCCS */
44 	struct xe_vm *vm;
45 	/** @q: GSCCS exec queue for PXP submissions */
46 	struct xe_exec_queue *q;
47 
48 	/**
49 	 * @bo: BO used for submissions to the GSCCS and GSC FW. It includes
50 	 * space for the GSCCS batch and the input/output buffers read/written
51 	 * by the FW
52 	 */
53 	struct xe_bo *bo;
54 	/** @inout_size: size of each of the msg_in/out sections individually */
55 	u32 inout_size;
56 	/** @batch: iosys_map to the batch memory within the BO */
57 	struct iosys_map batch;
58 	/** @msg_in: iosys_map to the input memory within the BO */
59 	struct iosys_map msg_in;
60 	/** @msg_out: iosys_map to the output memory within the BO */
61 	struct iosys_map msg_out;
62 };
63 
64 /**
65  * struct xe_pxp - pxp state
66  */
67 struct xe_pxp {
68 	/** @xe: Backpoiner to the xe_device struct */
69 	struct xe_device *xe;
70 
71 	/**
72 	 * @gt: pointer to the gt that owns the submission-side of PXP
73 	 * (VDBOX, KCR and GSC)
74 	 */
75 	struct xe_gt *gt;
76 
77 	/** @vcs_exec: kernel-owned objects for PXP submissions to the VCS */
78 	struct {
79 		/** @vcs_exec.q: kernel-owned VCS exec queue used for PXP terminations */
80 		struct xe_exec_queue *q;
81 		/** @vcs_exec.bo: BO used for submissions to the VCS */
82 		struct xe_bo *bo;
83 	} vcs_exec;
84 
85 	/** @gsc_res: kernel-owned objects for PXP submissions to the GSCCS */
86 	struct xe_pxp_gsc_client_resources gsc_res;
87 
88 	/** @irq: wrapper for the worker and queue used for PXP irq support */
89 	struct {
90 		/** @irq.work: worker that manages irq events. */
91 		struct work_struct work;
92 		/** @irq.wq: workqueue on which to queue the irq work. */
93 		struct workqueue_struct *wq;
94 		/** @irq.events: pending events, protected with xe->irq.lock. */
95 		u32 events;
96 #define PXP_TERMINATION_REQUEST  BIT(0)
97 #define PXP_TERMINATION_COMPLETE BIT(1)
98 	} irq;
99 
100 	/** @mutex: protects the pxp status and the queue list */
101 	struct mutex mutex;
102 	/** @status: the current pxp status */
103 	enum xe_pxp_status status;
104 	/** @activation: completion struct that tracks pxp start */
105 	struct completion activation;
106 	/** @termination: completion struct that tracks terminations */
107 	struct completion termination;
108 
109 	/** @queues: management of exec_queues that use PXP */
110 	struct {
111 		/** @queues.lock: spinlock protecting the queue management */
112 		spinlock_t lock;
113 		/** @queues.list: list of exec_queues that use PXP */
114 		struct list_head list;
115 	} queues;
116 
117 	/**
118 	 * @key_instance: keep track of the current iteration of the PXP key.
119 	 * Note that, due to the time needed for PXP termination and re-start
120 	 * to complete, the minimum time between 2 subsequent increases of this
121 	 * variable is 50ms, and even that only if there is a continuous attack;
122 	 * normal behavior is for this to increase much much slower than that.
123 	 * This means that we don't expect this to ever wrap and don't implement
124 	 * that case in the code.
125 	 */
126 	u32 key_instance;
127 	/**
128 	 * @last_suspend_key_instance: value of key_instance at the last
129 	 * suspend. Used to check if any PXP session has been created between
130 	 * suspend cycles.
131 	 */
132 	u32 last_suspend_key_instance;
133 };
134 
135 #endif /* __XE_PXP_TYPES_H__ */
136