1*677dec6eSriastradh /*	$NetBSD: kfd_dbgmgr.h,v 1.3 2021/12/18 23:44:59 riastradh Exp $	*/
2d350ecf5Sriastradh 
3d350ecf5Sriastradh /*
4d350ecf5Sriastradh  * Copyright 2014 Advanced Micro Devices, Inc.
5d350ecf5Sriastradh  *
6d350ecf5Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
7d350ecf5Sriastradh  * copy of this software and associated documentation files (the "Software"),
8d350ecf5Sriastradh  * to deal in the Software without restriction, including without limitation
9d350ecf5Sriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10d350ecf5Sriastradh  * and/or sell copies of the Software, and to permit persons to whom the
11d350ecf5Sriastradh  * Software is furnished to do so, subject to the following conditions:
12d350ecf5Sriastradh  *
13d350ecf5Sriastradh  * The above copyright notice and this permission notice shall be included in
14d350ecf5Sriastradh  * all copies or substantial portions of the Software.
15d350ecf5Sriastradh  *
16d350ecf5Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d350ecf5Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d350ecf5Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19d350ecf5Sriastradh  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20d350ecf5Sriastradh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21d350ecf5Sriastradh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22d350ecf5Sriastradh  * OTHER DEALINGS IN THE SOFTWARE.
23d350ecf5Sriastradh  *
24d350ecf5Sriastradh  */
25d350ecf5Sriastradh 
26d350ecf5Sriastradh #ifndef KFD_DBGMGR_H_
27d350ecf5Sriastradh #define KFD_DBGMGR_H_
28d350ecf5Sriastradh 
29d350ecf5Sriastradh #include "kfd_priv.h"
30d350ecf5Sriastradh 
31d350ecf5Sriastradh /* must align with hsakmttypes definition */
32d350ecf5Sriastradh #pragma pack(push, 4)
33d350ecf5Sriastradh 
34d350ecf5Sriastradh enum HSA_DBG_WAVEOP {
35d350ecf5Sriastradh 	HSA_DBG_WAVEOP_HALT = 1,   /* Halts a wavefront */
36d350ecf5Sriastradh 	HSA_DBG_WAVEOP_RESUME = 2, /* Resumes a wavefront */
37d350ecf5Sriastradh 	HSA_DBG_WAVEOP_KILL = 3,   /* Kills a wavefront */
38*677dec6eSriastradh 	HSA_DBG_WAVEOP_DEBUG = 4,  /* Causes wavefront to enter dbg mode */
39*677dec6eSriastradh 	HSA_DBG_WAVEOP_TRAP = 5,   /* Causes wavefront to take a trap */
40d350ecf5Sriastradh 	HSA_DBG_NUM_WAVEOP = 5,
41d350ecf5Sriastradh 	HSA_DBG_MAX_WAVEOP = 0xFFFFFFFF
42d350ecf5Sriastradh };
43d350ecf5Sriastradh 
44d350ecf5Sriastradh enum HSA_DBG_WAVEMODE {
45d350ecf5Sriastradh 	/* send command to a single wave */
46d350ecf5Sriastradh 	HSA_DBG_WAVEMODE_SINGLE = 0,
47d350ecf5Sriastradh 	/*
48d350ecf5Sriastradh 	 * Broadcast to all wavefronts of all processes is not
49d350ecf5Sriastradh 	 * supported for HSA user mode
50d350ecf5Sriastradh 	 */
51d350ecf5Sriastradh 
52d350ecf5Sriastradh 	/* send to waves within current process */
53d350ecf5Sriastradh 	HSA_DBG_WAVEMODE_BROADCAST_PROCESS = 2,
54d350ecf5Sriastradh 	/* send to waves within current process on CU  */
55d350ecf5Sriastradh 	HSA_DBG_WAVEMODE_BROADCAST_PROCESS_CU = 3,
56d350ecf5Sriastradh 	HSA_DBG_NUM_WAVEMODE = 3,
57d350ecf5Sriastradh 	HSA_DBG_MAX_WAVEMODE = 0xFFFFFFFF
58d350ecf5Sriastradh };
59d350ecf5Sriastradh 
60d350ecf5Sriastradh enum HSA_DBG_WAVEMSG_TYPE {
61d350ecf5Sriastradh 	HSA_DBG_WAVEMSG_AUTO = 0,
62d350ecf5Sriastradh 	HSA_DBG_WAVEMSG_USER = 1,
63d350ecf5Sriastradh 	HSA_DBG_WAVEMSG_ERROR = 2,
64d350ecf5Sriastradh 	HSA_DBG_NUM_WAVEMSG,
65d350ecf5Sriastradh 	HSA_DBG_MAX_WAVEMSG = 0xFFFFFFFF
66d350ecf5Sriastradh };
67d350ecf5Sriastradh 
68d350ecf5Sriastradh enum HSA_DBG_WATCH_MODE {
69d350ecf5Sriastradh 	HSA_DBG_WATCH_READ = 0,		/* Read operations only */
70d350ecf5Sriastradh 	HSA_DBG_WATCH_NONREAD = 1,	/* Write or Atomic operations only */
71d350ecf5Sriastradh 	HSA_DBG_WATCH_ATOMIC = 2,	/* Atomic Operations only */
72d350ecf5Sriastradh 	HSA_DBG_WATCH_ALL = 3,		/* Read, Write or Atomic operations */
73d350ecf5Sriastradh 	HSA_DBG_WATCH_NUM,
74d350ecf5Sriastradh 	HSA_DBG_WATCH_SIZE = 0xFFFFFFFF
75d350ecf5Sriastradh };
76d350ecf5Sriastradh 
77d350ecf5Sriastradh /* This structure is hardware specific and may change in the future */
78d350ecf5Sriastradh struct HsaDbgWaveMsgAMDGen2 {
79d350ecf5Sriastradh 	union {
80d350ecf5Sriastradh 		struct ui32 {
81d350ecf5Sriastradh 			uint32_t UserData:8;	/* user data */
82d350ecf5Sriastradh 			uint32_t ShaderArray:1;	/* Shader array */
83d350ecf5Sriastradh 			uint32_t Priv:1;	/* Privileged */
84*677dec6eSriastradh 			uint32_t Reserved0:4;	/* Reserved, should be 0 */
85d350ecf5Sriastradh 			uint32_t WaveId:4;	/* wave id */
86d350ecf5Sriastradh 			uint32_t SIMD:2;	/* SIMD id */
87d350ecf5Sriastradh 			uint32_t HSACU:4;	/* Compute unit */
88d350ecf5Sriastradh 			uint32_t ShaderEngine:2;/* Shader engine */
89d350ecf5Sriastradh 			uint32_t MessageType:2;	/* see HSA_DBG_WAVEMSG_TYPE */
90*677dec6eSriastradh 			uint32_t Reserved1:4;	/* Reserved, should be 0 */
91d350ecf5Sriastradh 		} ui32;
92d350ecf5Sriastradh 		uint32_t Value;
93d350ecf5Sriastradh 	};
94d350ecf5Sriastradh 	uint32_t Reserved2;
95d350ecf5Sriastradh };
96d350ecf5Sriastradh 
97d350ecf5Sriastradh union HsaDbgWaveMessageAMD {
98d350ecf5Sriastradh 	struct HsaDbgWaveMsgAMDGen2 WaveMsgInfoGen2;
99d350ecf5Sriastradh 	/* for future HsaDbgWaveMsgAMDGen3; */
100d350ecf5Sriastradh };
101d350ecf5Sriastradh 
102d350ecf5Sriastradh struct HsaDbgWaveMessage {
103d350ecf5Sriastradh 	void *MemoryVA;		/* ptr to associated host-accessible data */
104d350ecf5Sriastradh 	union HsaDbgWaveMessageAMD DbgWaveMsg;
105d350ecf5Sriastradh };
106d350ecf5Sriastradh 
107d350ecf5Sriastradh /*
108d350ecf5Sriastradh  * TODO: This definitions to be MOVED to kfd_event, once it is implemented.
109d350ecf5Sriastradh  *
110d350ecf5Sriastradh  * HSA sync primitive, Event and HW Exception notification API definitions.
111d350ecf5Sriastradh  * The API functions allow the runtime to define a so-called sync-primitive,
112d350ecf5Sriastradh  * a SW object combining a user-mode provided "syncvar" and a scheduler event
113d350ecf5Sriastradh  * that can be signaled through a defined GPU interrupt. A syncvar is
114d350ecf5Sriastradh  * a process virtual memory location of a certain size that can be accessed
115d350ecf5Sriastradh  * by CPU and GPU shader code within the process to set and query the content
116d350ecf5Sriastradh  * within that memory. The definition of the content is determined by the HSA
117d350ecf5Sriastradh  * runtime and potentially GPU shader code interfacing with the HSA runtime.
118d350ecf5Sriastradh  * The syncvar values may be commonly written through an PM4 WRITE_DATA packet
119d350ecf5Sriastradh  * in the user mode instruction stream. The OS scheduler event is typically
120d350ecf5Sriastradh  * associated and signaled by an interrupt issued by the GPU, but other HSA
121d350ecf5Sriastradh  * system interrupt conditions from other HW (e.g. IOMMUv2) may be surfaced
122*677dec6eSriastradh  * by the KFD by this mechanism, too.
123*677dec6eSriastradh  */
124d350ecf5Sriastradh 
125d350ecf5Sriastradh /* these are the new definitions for events */
126d350ecf5Sriastradh enum HSA_EVENTTYPE {
127d350ecf5Sriastradh 	HSA_EVENTTYPE_SIGNAL = 0,	/* user-mode generated GPU signal */
128d350ecf5Sriastradh 	HSA_EVENTTYPE_NODECHANGE = 1,	/* HSA node change (attach/detach) */
129d350ecf5Sriastradh 	HSA_EVENTTYPE_DEVICESTATECHANGE = 2,	/* HSA device state change
130*677dec6eSriastradh 						 * (start/stop)
131*677dec6eSriastradh 						 */
132d350ecf5Sriastradh 	HSA_EVENTTYPE_HW_EXCEPTION = 3,	/* GPU shader exception event */
133d350ecf5Sriastradh 	HSA_EVENTTYPE_SYSTEM_EVENT = 4,	/* GPU SYSCALL with parameter info */
134d350ecf5Sriastradh 	HSA_EVENTTYPE_DEBUG_EVENT = 5,	/* GPU signal for debugging */
135d350ecf5Sriastradh 	HSA_EVENTTYPE_PROFILE_EVENT = 6,/* GPU signal for profiling */
136d350ecf5Sriastradh 	HSA_EVENTTYPE_QUEUE_EVENT = 7,	/* GPU signal queue idle state
137*677dec6eSriastradh 					 * (EOP pm4)
138*677dec6eSriastradh 					 */
139d350ecf5Sriastradh 	/* ...  */
140d350ecf5Sriastradh 	HSA_EVENTTYPE_MAXID,
141d350ecf5Sriastradh 	HSA_EVENTTYPE_TYPE_SIZE = 0xFFFFFFFF
142d350ecf5Sriastradh };
143d350ecf5Sriastradh 
144d350ecf5Sriastradh /* Sub-definitions for various event types: Syncvar */
145d350ecf5Sriastradh struct HsaSyncVar {
146d350ecf5Sriastradh 	union SyncVar {
147d350ecf5Sriastradh 		void *UserData;	/* pointer to user mode data */
148d350ecf5Sriastradh 		uint64_t UserDataPtrValue; /* 64bit compatibility of value */
149d350ecf5Sriastradh 	} SyncVar;
150d350ecf5Sriastradh 	uint64_t SyncVarSize;
151d350ecf5Sriastradh };
152d350ecf5Sriastradh 
153d350ecf5Sriastradh /* Sub-definitions for various event types: NodeChange */
154d350ecf5Sriastradh 
155d350ecf5Sriastradh enum HSA_EVENTTYPE_NODECHANGE_FLAGS {
156d350ecf5Sriastradh 	HSA_EVENTTYPE_NODECHANGE_ADD = 0,
157d350ecf5Sriastradh 	HSA_EVENTTYPE_NODECHANGE_REMOVE = 1,
158d350ecf5Sriastradh 	HSA_EVENTTYPE_NODECHANGE_SIZE = 0xFFFFFFFF
159d350ecf5Sriastradh };
160d350ecf5Sriastradh 
161d350ecf5Sriastradh struct HsaNodeChange {
162d350ecf5Sriastradh 	/* HSA node added/removed on the platform */
163d350ecf5Sriastradh 	enum HSA_EVENTTYPE_NODECHANGE_FLAGS Flags;
164d350ecf5Sriastradh };
165d350ecf5Sriastradh 
166d350ecf5Sriastradh /* Sub-definitions for various event types: DeviceStateChange */
167d350ecf5Sriastradh enum HSA_EVENTTYPE_DEVICESTATECHANGE_FLAGS {
168d350ecf5Sriastradh 	/* device started (and available) */
169d350ecf5Sriastradh 	HSA_EVENTTYPE_DEVICESTATUSCHANGE_START = 0,
170d350ecf5Sriastradh 	/* device stopped (i.e. unavailable) */
171d350ecf5Sriastradh 	HSA_EVENTTYPE_DEVICESTATUSCHANGE_STOP = 1,
172d350ecf5Sriastradh 	HSA_EVENTTYPE_DEVICESTATUSCHANGE_SIZE = 0xFFFFFFFF
173d350ecf5Sriastradh };
174d350ecf5Sriastradh 
175d350ecf5Sriastradh enum HSA_DEVICE {
176d350ecf5Sriastradh 	HSA_DEVICE_CPU = 0,
177d350ecf5Sriastradh 	HSA_DEVICE_GPU = 1,
178d350ecf5Sriastradh 	MAX_HSA_DEVICE = 2
179d350ecf5Sriastradh };
180d350ecf5Sriastradh 
181d350ecf5Sriastradh struct HsaDeviceStateChange {
182d350ecf5Sriastradh 	uint32_t NodeId;	/* F-NUMA node that contains the device */
183d350ecf5Sriastradh 	enum HSA_DEVICE Device;	/* device type: GPU or CPU */
184d350ecf5Sriastradh 	enum HSA_EVENTTYPE_DEVICESTATECHANGE_FLAGS Flags; /* event flags */
185d350ecf5Sriastradh };
186d350ecf5Sriastradh 
187d350ecf5Sriastradh struct HsaEventData {
188d350ecf5Sriastradh 	enum HSA_EVENTTYPE EventType; /* event type */
189d350ecf5Sriastradh 	union EventData {
190d350ecf5Sriastradh 		/*
191d350ecf5Sriastradh 		 * return data associated with HSA_EVENTTYPE_SIGNAL
192d350ecf5Sriastradh 		 * and other events
193d350ecf5Sriastradh 		 */
194d350ecf5Sriastradh 		struct HsaSyncVar SyncVar;
195d350ecf5Sriastradh 
196d350ecf5Sriastradh 		/* data associated with HSA_EVENTTYPE_NODE_CHANGE */
197d350ecf5Sriastradh 		struct HsaNodeChange NodeChangeState;
198d350ecf5Sriastradh 
199d350ecf5Sriastradh 		/* data associated with HSA_EVENTTYPE_DEVICE_STATE_CHANGE */
200d350ecf5Sriastradh 		struct HsaDeviceStateChange DeviceState;
201d350ecf5Sriastradh 	} EventData;
202d350ecf5Sriastradh 
203d350ecf5Sriastradh 	/* the following data entries are internal to the KFD & thunk itself */
204d350ecf5Sriastradh 
205d350ecf5Sriastradh 	/* internal thunk store for Event data (OsEventHandle) */
206d350ecf5Sriastradh 	uint64_t HWData1;
207d350ecf5Sriastradh 	/* internal thunk store for Event data (HWAddress) */
208d350ecf5Sriastradh 	uint64_t HWData2;
209d350ecf5Sriastradh 	/* internal thunk store for Event data (HWData) */
210d350ecf5Sriastradh 	uint32_t HWData3;
211d350ecf5Sriastradh };
212d350ecf5Sriastradh 
213d350ecf5Sriastradh struct HsaEventDescriptor {
214d350ecf5Sriastradh 	/* event type to allocate */
215d350ecf5Sriastradh 	enum HSA_EVENTTYPE EventType;
216d350ecf5Sriastradh 	/* H-NUMA node containing GPU device that is event source */
217d350ecf5Sriastradh 	uint32_t NodeId;
218d350ecf5Sriastradh 	/* pointer to user mode syncvar data, syncvar->UserDataPtrValue
219d350ecf5Sriastradh 	 * may be NULL
220d350ecf5Sriastradh 	 */
221d350ecf5Sriastradh 	struct HsaSyncVar SyncVar;
222d350ecf5Sriastradh };
223d350ecf5Sriastradh 
224d350ecf5Sriastradh struct HsaEvent {
225d350ecf5Sriastradh 	uint32_t EventId;
226d350ecf5Sriastradh 	struct HsaEventData EventData;
227d350ecf5Sriastradh };
228d350ecf5Sriastradh 
229d350ecf5Sriastradh #pragma pack(pop)
230d350ecf5Sriastradh 
231d350ecf5Sriastradh enum DBGDEV_TYPE {
232d350ecf5Sriastradh 	DBGDEV_TYPE_ILLEGAL = 0,
233d350ecf5Sriastradh 	DBGDEV_TYPE_NODIQ = 1,
234d350ecf5Sriastradh 	DBGDEV_TYPE_DIQ = 2,
235d350ecf5Sriastradh 	DBGDEV_TYPE_TEST = 3
236d350ecf5Sriastradh };
237d350ecf5Sriastradh 
238d350ecf5Sriastradh struct dbg_address_watch_info {
239d350ecf5Sriastradh 	struct kfd_process *process;
240d350ecf5Sriastradh 	enum HSA_DBG_WATCH_MODE *watch_mode;
241d350ecf5Sriastradh 	uint64_t *watch_address;
242d350ecf5Sriastradh 	uint64_t *watch_mask;
243d350ecf5Sriastradh 	struct HsaEvent *watch_event;
244d350ecf5Sriastradh 	uint32_t num_watch_points;
245d350ecf5Sriastradh };
246d350ecf5Sriastradh 
247d350ecf5Sriastradh struct dbg_wave_control_info {
248d350ecf5Sriastradh 	struct kfd_process *process;
249d350ecf5Sriastradh 	uint32_t trapId;
250d350ecf5Sriastradh 	enum HSA_DBG_WAVEOP operand;
251d350ecf5Sriastradh 	enum HSA_DBG_WAVEMODE mode;
252d350ecf5Sriastradh 	struct HsaDbgWaveMessage dbgWave_msg;
253d350ecf5Sriastradh };
254d350ecf5Sriastradh 
255d350ecf5Sriastradh struct kfd_dbgdev {
256d350ecf5Sriastradh 
257d350ecf5Sriastradh 	/* The device that owns this data. */
258d350ecf5Sriastradh 	struct kfd_dev *dev;
259d350ecf5Sriastradh 
260d350ecf5Sriastradh 	/* kernel queue for DIQ */
261d350ecf5Sriastradh 	struct kernel_queue *kq;
262d350ecf5Sriastradh 
263d350ecf5Sriastradh 	/* a pointer to the pqm of the calling process */
264d350ecf5Sriastradh 	struct process_queue_manager *pqm;
265d350ecf5Sriastradh 
266d350ecf5Sriastradh 	/* type of debug device ( DIQ, non DIQ, etc. ) */
267d350ecf5Sriastradh 	enum DBGDEV_TYPE type;
268d350ecf5Sriastradh 
269d350ecf5Sriastradh 	/* virtualized function pointers to device dbg */
270d350ecf5Sriastradh 	int (*dbgdev_register)(struct kfd_dbgdev *dbgdev);
271d350ecf5Sriastradh 	int (*dbgdev_unregister)(struct kfd_dbgdev *dbgdev);
272d350ecf5Sriastradh 	int (*dbgdev_address_watch)(struct kfd_dbgdev *dbgdev,
273d350ecf5Sriastradh 				struct dbg_address_watch_info *adw_info);
274d350ecf5Sriastradh 	int (*dbgdev_wave_control)(struct kfd_dbgdev *dbgdev,
275d350ecf5Sriastradh 				struct dbg_wave_control_info *wac_info);
276d350ecf5Sriastradh 
277d350ecf5Sriastradh };
278d350ecf5Sriastradh 
279d350ecf5Sriastradh struct kfd_dbgmgr {
280d350ecf5Sriastradh 	unsigned int pasid;
281d350ecf5Sriastradh 	struct kfd_dev *dev;
282d350ecf5Sriastradh 	struct kfd_dbgdev *dbgdev;
283d350ecf5Sriastradh };
284d350ecf5Sriastradh 
285d350ecf5Sriastradh /* prototypes for debug manager functions */
286d350ecf5Sriastradh struct mutex *kfd_get_dbgmgr_mutex(void);
287d350ecf5Sriastradh void kfd_dbgmgr_destroy(struct kfd_dbgmgr *pmgr);
288d350ecf5Sriastradh bool kfd_dbgmgr_create(struct kfd_dbgmgr **ppmgr, struct kfd_dev *pdev);
289d350ecf5Sriastradh long kfd_dbgmgr_register(struct kfd_dbgmgr *pmgr, struct kfd_process *p);
290d350ecf5Sriastradh long kfd_dbgmgr_unregister(struct kfd_dbgmgr *pmgr, struct kfd_process *p);
291d350ecf5Sriastradh long kfd_dbgmgr_wave_control(struct kfd_dbgmgr *pmgr,
292d350ecf5Sriastradh 				struct dbg_wave_control_info *wac_info);
293d350ecf5Sriastradh long kfd_dbgmgr_address_watch(struct kfd_dbgmgr *pmgr,
294d350ecf5Sriastradh 			struct dbg_address_watch_info *adw_info);
295d350ecf5Sriastradh #endif /* KFD_DBGMGR_H_ */
296