1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef KFD_IOCTL_H_INCLUDED
24 #define KFD_IOCTL_H_INCLUDED
25 
26 #include <drm/drm.h>
27 #include <linux/ioctl.h>
28 
29 /*
30  * - 1.1 - initial version
31  * - 1.3 - Add SMI events support
32  * - 1.4 - Indicate new SRAM EDC bit in device properties
33  * - 1.5 - Add SVM API
34  * - 1.6 - Query clear flags in SVM get_attr API
35  * - 1.7 - Checkpoint Restore (CRIU) API
36  * - 1.8 - CRIU - Support for SDMA transfers with GTT BOs
37  * - 1.9 - Add available memory ioctl
38  * - 1.10 - Add SMI profiler event log
39  * - 1.11 - Add unified memory for ctx save/restore area
40  * - 1.12 - Add DMA buf export ioctl
41  * - 1.13 - Add debugger API
42  * - 1.14 - Update kfd_event_data
43  * - 1.15 - Enable managing mappings in compute VMs with GEM_VA ioctl
44  * - 1.16 - Add contiguous VRAM allocation flag
45  * - 1.17 - Add SDMA queue creation with target SDMA engine ID
46  */
47 #define KFD_IOCTL_MAJOR_VERSION 1
48 #define KFD_IOCTL_MINOR_VERSION 17
49 
50 struct kfd_ioctl_get_version_args {
51 	__u32 major_version;	/* from KFD */
52 	__u32 minor_version;	/* from KFD */
53 };
54 
55 /* For kfd_ioctl_create_queue_args.queue_type. */
56 #define KFD_IOC_QUEUE_TYPE_COMPUTE		0x0
57 #define KFD_IOC_QUEUE_TYPE_SDMA			0x1
58 #define KFD_IOC_QUEUE_TYPE_COMPUTE_AQL		0x2
59 #define KFD_IOC_QUEUE_TYPE_SDMA_XGMI		0x3
60 #define KFD_IOC_QUEUE_TYPE_SDMA_BY_ENG_ID	0x4
61 
62 #define KFD_MAX_QUEUE_PERCENTAGE	100
63 #define KFD_MAX_QUEUE_PRIORITY		15
64 
65 #define KFD_MIN_QUEUE_RING_SIZE		1024
66 
67 struct kfd_ioctl_create_queue_args {
68 	__u64 ring_base_address;	/* to KFD */
69 	__u64 write_pointer_address;	/* from KFD */
70 	__u64 read_pointer_address;	/* from KFD */
71 	__u64 doorbell_offset;	/* from KFD */
72 
73 	__u32 ring_size;		/* to KFD */
74 	__u32 gpu_id;		/* to KFD */
75 	__u32 queue_type;		/* to KFD */
76 	__u32 queue_percentage;	/* to KFD */
77 	__u32 queue_priority;	/* to KFD */
78 	__u32 queue_id;		/* from KFD */
79 
80 	__u64 eop_buffer_address;	/* to KFD */
81 	__u64 eop_buffer_size;	/* to KFD */
82 	__u64 ctx_save_restore_address; /* to KFD */
83 	__u32 ctx_save_restore_size;	/* to KFD */
84 	__u32 ctl_stack_size;		/* to KFD */
85 	__u32 sdma_engine_id;		/* to KFD */
86 	__u32 pad;
87 };
88 
89 struct kfd_ioctl_destroy_queue_args {
90 	__u32 queue_id;		/* to KFD */
91 	__u32 pad;
92 };
93 
94 struct kfd_ioctl_update_queue_args {
95 	__u64 ring_base_address;	/* to KFD */
96 
97 	__u32 queue_id;		/* to KFD */
98 	__u32 ring_size;		/* to KFD */
99 	__u32 queue_percentage;	/* to KFD */
100 	__u32 queue_priority;	/* to KFD */
101 };
102 
103 struct kfd_ioctl_set_cu_mask_args {
104 	__u32 queue_id;		/* to KFD */
105 	__u32 num_cu_mask;		/* to KFD */
106 	__u64 cu_mask_ptr;		/* to KFD */
107 };
108 
109 struct kfd_ioctl_get_queue_wave_state_args {
110 	__u64 ctl_stack_address;	/* to KFD */
111 	__u32 ctl_stack_used_size;	/* from KFD */
112 	__u32 save_area_used_size;	/* from KFD */
113 	__u32 queue_id;			/* to KFD */
114 	__u32 pad;
115 };
116 
117 struct kfd_ioctl_get_available_memory_args {
118 	__u64 available;	/* from KFD */
119 	__u32 gpu_id;		/* to KFD */
120 	__u32 pad;
121 };
122 
123 struct kfd_dbg_device_info_entry {
124 	__u64 exception_status;
125 	__u64 lds_base;
126 	__u64 lds_limit;
127 	__u64 scratch_base;
128 	__u64 scratch_limit;
129 	__u64 gpuvm_base;
130 	__u64 gpuvm_limit;
131 	__u32 gpu_id;
132 	__u32 location_id;
133 	__u32 vendor_id;
134 	__u32 device_id;
135 	__u32 revision_id;
136 	__u32 subsystem_vendor_id;
137 	__u32 subsystem_device_id;
138 	__u32 fw_version;
139 	__u32 gfx_target_version;
140 	__u32 simd_count;
141 	__u32 max_waves_per_simd;
142 	__u32 array_count;
143 	__u32 simd_arrays_per_engine;
144 	__u32 num_xcc;
145 	__u32 capability;
146 	__u32 debug_prop;
147 };
148 
149 /* For kfd_ioctl_set_memory_policy_args.default_policy and alternate_policy */
150 #define KFD_IOC_CACHE_POLICY_COHERENT 0
151 #define KFD_IOC_CACHE_POLICY_NONCOHERENT 1
152 
153 struct kfd_ioctl_set_memory_policy_args {
154 	__u64 alternate_aperture_base;	/* to KFD */
155 	__u64 alternate_aperture_size;	/* to KFD */
156 
157 	__u32 gpu_id;			/* to KFD */
158 	__u32 default_policy;		/* to KFD */
159 	__u32 alternate_policy;		/* to KFD */
160 	__u32 pad;
161 };
162 
163 /*
164  * All counters are monotonic. They are used for profiling of compute jobs.
165  * The profiling is done by userspace.
166  *
167  * In case of GPU reset, the counter should not be affected.
168  */
169 
170 struct kfd_ioctl_get_clock_counters_args {
171 	__u64 gpu_clock_counter;	/* from KFD */
172 	__u64 cpu_clock_counter;	/* from KFD */
173 	__u64 system_clock_counter;	/* from KFD */
174 	__u64 system_clock_freq;	/* from KFD */
175 
176 	__u32 gpu_id;		/* to KFD */
177 	__u32 pad;
178 };
179 
180 struct kfd_process_device_apertures {
181 	__u64 lds_base;		/* from KFD */
182 	__u64 lds_limit;		/* from KFD */
183 	__u64 scratch_base;		/* from KFD */
184 	__u64 scratch_limit;		/* from KFD */
185 	__u64 gpuvm_base;		/* from KFD */
186 	__u64 gpuvm_limit;		/* from KFD */
187 	__u32 gpu_id;		/* from KFD */
188 	__u32 pad;
189 };
190 
191 /*
192  * AMDKFD_IOC_GET_PROCESS_APERTURES is deprecated. Use
193  * AMDKFD_IOC_GET_PROCESS_APERTURES_NEW instead, which supports an
194  * unlimited number of GPUs.
195  */
196 #define NUM_OF_SUPPORTED_GPUS 7
197 struct kfd_ioctl_get_process_apertures_args {
198 	struct kfd_process_device_apertures
199 			process_apertures[NUM_OF_SUPPORTED_GPUS];/* from KFD */
200 
201 	/* from KFD, should be in the range [1 - NUM_OF_SUPPORTED_GPUS] */
202 	__u32 num_of_nodes;
203 	__u32 pad;
204 };
205 
206 struct kfd_ioctl_get_process_apertures_new_args {
207 	/* User allocated. Pointer to struct kfd_process_device_apertures
208 	 * filled in by Kernel
209 	 */
210 	__u64 kfd_process_device_apertures_ptr;
211 	/* to KFD - indicates amount of memory present in
212 	 *  kfd_process_device_apertures_ptr
213 	 * from KFD - Number of entries filled by KFD.
214 	 */
215 	__u32 num_of_nodes;
216 	__u32 pad;
217 };
218 
219 #define MAX_ALLOWED_NUM_POINTS    100
220 #define MAX_ALLOWED_AW_BUFF_SIZE 4096
221 #define MAX_ALLOWED_WAC_BUFF_SIZE  128
222 
223 struct kfd_ioctl_dbg_register_args {
224 	__u32 gpu_id;		/* to KFD */
225 	__u32 pad;
226 };
227 
228 struct kfd_ioctl_dbg_unregister_args {
229 	__u32 gpu_id;		/* to KFD */
230 	__u32 pad;
231 };
232 
233 struct kfd_ioctl_dbg_address_watch_args {
234 	__u64 content_ptr;		/* a pointer to the actual content */
235 	__u32 gpu_id;		/* to KFD */
236 	__u32 buf_size_in_bytes;	/*including gpu_id and buf_size */
237 };
238 
239 struct kfd_ioctl_dbg_wave_control_args {
240 	__u64 content_ptr;		/* a pointer to the actual content */
241 	__u32 gpu_id;		/* to KFD */
242 	__u32 buf_size_in_bytes;	/*including gpu_id and buf_size */
243 };
244 
245 #define KFD_INVALID_FD     0xffffffff
246 
247 /* Matching HSA_EVENTTYPE */
248 #define KFD_IOC_EVENT_SIGNAL			0
249 #define KFD_IOC_EVENT_NODECHANGE		1
250 #define KFD_IOC_EVENT_DEVICESTATECHANGE		2
251 #define KFD_IOC_EVENT_HW_EXCEPTION		3
252 #define KFD_IOC_EVENT_SYSTEM_EVENT		4
253 #define KFD_IOC_EVENT_DEBUG_EVENT		5
254 #define KFD_IOC_EVENT_PROFILE_EVENT		6
255 #define KFD_IOC_EVENT_QUEUE_EVENT		7
256 #define KFD_IOC_EVENT_MEMORY			8
257 
258 #define KFD_IOC_WAIT_RESULT_COMPLETE		0
259 #define KFD_IOC_WAIT_RESULT_TIMEOUT		1
260 #define KFD_IOC_WAIT_RESULT_FAIL		2
261 
262 #define KFD_SIGNAL_EVENT_LIMIT			4096
263 
264 /* For kfd_event_data.hw_exception_data.reset_type. */
265 #define KFD_HW_EXCEPTION_WHOLE_GPU_RESET	0
266 #define KFD_HW_EXCEPTION_PER_ENGINE_RESET	1
267 
268 /* For kfd_event_data.hw_exception_data.reset_cause. */
269 #define KFD_HW_EXCEPTION_GPU_HANG	0
270 #define KFD_HW_EXCEPTION_ECC		1
271 
272 /* For kfd_hsa_memory_exception_data.ErrorType */
273 #define KFD_MEM_ERR_NO_RAS		0
274 #define KFD_MEM_ERR_SRAM_ECC		1
275 #define KFD_MEM_ERR_POISON_CONSUMED	2
276 #define KFD_MEM_ERR_GPU_HANG		3
277 
278 struct kfd_ioctl_create_event_args {
279 	__u64 event_page_offset;	/* from KFD */
280 	__u32 event_trigger_data;	/* from KFD - signal events only */
281 	__u32 event_type;		/* to KFD */
282 	__u32 auto_reset;		/* to KFD */
283 	__u32 node_id;		/* to KFD - only valid for certain
284 							event types */
285 	__u32 event_id;		/* from KFD */
286 	__u32 event_slot_index;	/* from KFD */
287 };
288 
289 struct kfd_ioctl_destroy_event_args {
290 	__u32 event_id;		/* to KFD */
291 	__u32 pad;
292 };
293 
294 struct kfd_ioctl_set_event_args {
295 	__u32 event_id;		/* to KFD */
296 	__u32 pad;
297 };
298 
299 struct kfd_ioctl_reset_event_args {
300 	__u32 event_id;		/* to KFD */
301 	__u32 pad;
302 };
303 
304 struct kfd_memory_exception_failure {
305 	__u32 NotPresent;	/* Page not present or supervisor privilege */
306 	__u32 ReadOnly;	/* Write access to a read-only page */
307 	__u32 NoExecute;	/* Execute access to a page marked NX */
308 	__u32 imprecise;	/* Can't determine the	exact fault address */
309 };
310 
311 /* memory exception data */
312 struct kfd_hsa_memory_exception_data {
313 	struct kfd_memory_exception_failure failure;
314 	__u64 va;
315 	__u32 gpu_id;
316 	__u32 ErrorType; /* 0 = no RAS error,
317 			  * 1 = ECC_SRAM,
318 			  * 2 = Link_SYNFLOOD (poison),
319 			  * 3 = GPU hang (not attributable to a specific cause),
320 			  * other values reserved
321 			  */
322 };
323 
324 /* hw exception data */
325 struct kfd_hsa_hw_exception_data {
326 	__u32 reset_type;
327 	__u32 reset_cause;
328 	__u32 memory_lost;
329 	__u32 gpu_id;
330 };
331 
332 /* hsa signal event data */
333 struct kfd_hsa_signal_event_data {
334 	__u64 last_event_age;	/* to and from KFD */
335 };
336 
337 /* Event data */
338 struct kfd_event_data {
339 	union {
340 		/* From KFD */
341 		struct kfd_hsa_memory_exception_data memory_exception_data;
342 		struct kfd_hsa_hw_exception_data hw_exception_data;
343 		/* To and From KFD */
344 		struct kfd_hsa_signal_event_data signal_event_data;
345 	};
346 	__u64 kfd_event_data_ext;	/* pointer to an extension structure
347 					   for future exception types */
348 	__u32 event_id;		/* to KFD */
349 	__u32 pad;
350 };
351 
352 struct kfd_ioctl_wait_events_args {
353 	__u64 events_ptr;		/* pointed to struct
354 					   kfd_event_data array, to KFD */
355 	__u32 num_events;		/* to KFD */
356 	__u32 wait_for_all;		/* to KFD */
357 	__u32 timeout;		/* to KFD */
358 	__u32 wait_result;		/* from KFD */
359 };
360 
361 struct kfd_ioctl_set_scratch_backing_va_args {
362 	__u64 va_addr;	/* to KFD */
363 	__u32 gpu_id;	/* to KFD */
364 	__u32 pad;
365 };
366 
367 struct kfd_ioctl_get_tile_config_args {
368 	/* to KFD: pointer to tile array */
369 	__u64 tile_config_ptr;
370 	/* to KFD: pointer to macro tile array */
371 	__u64 macro_tile_config_ptr;
372 	/* to KFD: array size allocated by user mode
373 	 * from KFD: array size filled by kernel
374 	 */
375 	__u32 num_tile_configs;
376 	/* to KFD: array size allocated by user mode
377 	 * from KFD: array size filled by kernel
378 	 */
379 	__u32 num_macro_tile_configs;
380 
381 	__u32 gpu_id;		/* to KFD */
382 	__u32 gb_addr_config;	/* from KFD */
383 	__u32 num_banks;		/* from KFD */
384 	__u32 num_ranks;		/* from KFD */
385 	/* struct size can be extended later if needed
386 	 * without breaking ABI compatibility
387 	 */
388 };
389 
390 struct kfd_ioctl_set_trap_handler_args {
391 	__u64 tba_addr;		/* to KFD */
392 	__u64 tma_addr;		/* to KFD */
393 	__u32 gpu_id;		/* to KFD */
394 	__u32 pad;
395 };
396 
397 struct kfd_ioctl_acquire_vm_args {
398 	__u32 drm_fd;	/* to KFD */
399 	__u32 gpu_id;	/* to KFD */
400 };
401 
402 /* Allocation flags: memory types */
403 #define KFD_IOC_ALLOC_MEM_FLAGS_VRAM		(1 << 0)
404 #define KFD_IOC_ALLOC_MEM_FLAGS_GTT		(1 << 1)
405 #define KFD_IOC_ALLOC_MEM_FLAGS_USERPTR		(1 << 2)
406 #define KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL	(1 << 3)
407 #define KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP	(1 << 4)
408 /* Allocation flags: attributes/access options */
409 #define KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE	(1 << 31)
410 #define KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE	(1 << 30)
411 #define KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC		(1 << 29)
412 #define KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE	(1 << 28)
413 #define KFD_IOC_ALLOC_MEM_FLAGS_AQL_QUEUE_MEM	(1 << 27)
414 #define KFD_IOC_ALLOC_MEM_FLAGS_COHERENT	(1 << 26)
415 #define KFD_IOC_ALLOC_MEM_FLAGS_UNCACHED	(1 << 25)
416 #define KFD_IOC_ALLOC_MEM_FLAGS_EXT_COHERENT	(1 << 24)
417 #define KFD_IOC_ALLOC_MEM_FLAGS_CONTIGUOUS	(1 << 23)
418 
419 /* Allocate memory for later SVM (shared virtual memory) mapping.
420  *
421  * @va_addr:     virtual address of the memory to be allocated
422  *               all later mappings on all GPUs will use this address
423  * @size:        size in bytes
424  * @handle:      buffer handle returned to user mode, used to refer to
425  *               this allocation for mapping, unmapping and freeing
426  * @mmap_offset: for CPU-mapping the allocation by mmapping a render node
427  *               for userptrs this is overloaded to specify the CPU address
428  * @gpu_id:      device identifier
429  * @flags:       memory type and attributes. See KFD_IOC_ALLOC_MEM_FLAGS above
430  */
431 struct kfd_ioctl_alloc_memory_of_gpu_args {
432 	__u64 va_addr;		/* to KFD */
433 	__u64 size;		/* to KFD */
434 	__u64 handle;		/* from KFD */
435 	__u64 mmap_offset;	/* to KFD (userptr), from KFD (mmap offset) */
436 	__u32 gpu_id;		/* to KFD */
437 	__u32 flags;
438 };
439 
440 /* Free memory allocated with kfd_ioctl_alloc_memory_of_gpu
441  *
442  * @handle: memory handle returned by alloc
443  */
444 struct kfd_ioctl_free_memory_of_gpu_args {
445 	__u64 handle;		/* to KFD */
446 };
447 
448 /* Map memory to one or more GPUs
449  *
450  * @handle:                memory handle returned by alloc
451  * @device_ids_array_ptr:  array of gpu_ids (__u32 per device)
452  * @n_devices:             number of devices in the array
453  * @n_success:             number of devices mapped successfully
454  *
455  * @n_success returns information to the caller how many devices from
456  * the start of the array have mapped the buffer successfully. It can
457  * be passed into a subsequent retry call to skip those devices. For
458  * the first call the caller should initialize it to 0.
459  *
460  * If the ioctl completes with return code 0 (success), n_success ==
461  * n_devices.
462  */
463 struct kfd_ioctl_map_memory_to_gpu_args {
464 	__u64 handle;			/* to KFD */
465 	__u64 device_ids_array_ptr;	/* to KFD */
466 	__u32 n_devices;		/* to KFD */
467 	__u32 n_success;		/* to/from KFD */
468 };
469 
470 /* Unmap memory from one or more GPUs
471  *
472  * same arguments as for mapping
473  */
474 struct kfd_ioctl_unmap_memory_from_gpu_args {
475 	__u64 handle;			/* to KFD */
476 	__u64 device_ids_array_ptr;	/* to KFD */
477 	__u32 n_devices;		/* to KFD */
478 	__u32 n_success;		/* to/from KFD */
479 };
480 
481 /* Allocate GWS for specific queue
482  *
483  * @queue_id:    queue's id that GWS is allocated for
484  * @num_gws:     how many GWS to allocate
485  * @first_gws:   index of the first GWS allocated.
486  *               only support contiguous GWS allocation
487  */
488 struct kfd_ioctl_alloc_queue_gws_args {
489 	__u32 queue_id;		/* to KFD */
490 	__u32 num_gws;		/* to KFD */
491 	__u32 first_gws;	/* from KFD */
492 	__u32 pad;
493 };
494 
495 struct kfd_ioctl_get_dmabuf_info_args {
496 	__u64 size;		/* from KFD */
497 	__u64 metadata_ptr;	/* to KFD */
498 	__u32 metadata_size;	/* to KFD (space allocated by user)
499 				 * from KFD (actual metadata size)
500 				 */
501 	__u32 gpu_id;	/* from KFD */
502 	__u32 flags;		/* from KFD (KFD_IOC_ALLOC_MEM_FLAGS) */
503 	__u32 dmabuf_fd;	/* to KFD */
504 };
505 
506 struct kfd_ioctl_import_dmabuf_args {
507 	__u64 va_addr;	/* to KFD */
508 	__u64 handle;	/* from KFD */
509 	__u32 gpu_id;	/* to KFD */
510 	__u32 dmabuf_fd;	/* to KFD */
511 };
512 
513 struct kfd_ioctl_export_dmabuf_args {
514 	__u64 handle;		/* to KFD */
515 	__u32 flags;		/* to KFD */
516 	__u32 dmabuf_fd;	/* from KFD */
517 };
518 
519 /*
520  * KFD SMI(System Management Interface) events
521  */
522 enum kfd_smi_event {
523 	KFD_SMI_EVENT_NONE = 0, /* not used */
524 	KFD_SMI_EVENT_VMFAULT = 1, /* event start counting at 1 */
525 	KFD_SMI_EVENT_THERMAL_THROTTLE = 2,
526 	KFD_SMI_EVENT_GPU_PRE_RESET = 3,
527 	KFD_SMI_EVENT_GPU_POST_RESET = 4,
528 	KFD_SMI_EVENT_MIGRATE_START = 5,
529 	KFD_SMI_EVENT_MIGRATE_END = 6,
530 	KFD_SMI_EVENT_PAGE_FAULT_START = 7,
531 	KFD_SMI_EVENT_PAGE_FAULT_END = 8,
532 	KFD_SMI_EVENT_QUEUE_EVICTION = 9,
533 	KFD_SMI_EVENT_QUEUE_RESTORE = 10,
534 	KFD_SMI_EVENT_UNMAP_FROM_GPU = 11,
535 
536 	/*
537 	 * max event number, as a flag bit to get events from all processes,
538 	 * this requires super user permission, otherwise will not be able to
539 	 * receive event from any process. Without this flag to receive events
540 	 * from same process.
541 	 */
542 	KFD_SMI_EVENT_ALL_PROCESS = 64
543 };
544 
545 /* The reason of the page migration event */
546 enum KFD_MIGRATE_TRIGGERS {
547 	KFD_MIGRATE_TRIGGER_PREFETCH,		/* Prefetch to GPU VRAM or system memory */
548 	KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU,	/* GPU page fault recover */
549 	KFD_MIGRATE_TRIGGER_PAGEFAULT_CPU,	/* CPU page fault recover */
550 	KFD_MIGRATE_TRIGGER_TTM_EVICTION	/* TTM eviction */
551 };
552 
553 /* The reason of user queue evition event */
554 enum KFD_QUEUE_EVICTION_TRIGGERS {
555 	KFD_QUEUE_EVICTION_TRIGGER_SVM,		/* SVM buffer migration */
556 	KFD_QUEUE_EVICTION_TRIGGER_USERPTR,	/* userptr movement */
557 	KFD_QUEUE_EVICTION_TRIGGER_TTM,		/* TTM move buffer */
558 	KFD_QUEUE_EVICTION_TRIGGER_SUSPEND,	/* GPU suspend */
559 	KFD_QUEUE_EVICTION_CRIU_CHECKPOINT,	/* CRIU checkpoint */
560 	KFD_QUEUE_EVICTION_CRIU_RESTORE		/* CRIU restore */
561 };
562 
563 /* The reason of unmap buffer from GPU event */
564 enum KFD_SVM_UNMAP_TRIGGERS {
565 	KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY,	/* MMU notifier CPU buffer movement */
566 	KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY_MIGRATE,/* MMU notifier page migration */
567 	KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU	/* Unmap to free the buffer */
568 };
569 
570 #define KFD_SMI_EVENT_MASK_FROM_INDEX(i) (1ULL << ((i) - 1))
571 #define KFD_SMI_EVENT_MSG_SIZE	96
572 
573 struct kfd_ioctl_smi_events_args {
574 	__u32 gpuid;	/* to KFD */
575 	__u32 anon_fd;	/* from KFD */
576 };
577 
578 /*
579  * SVM event tracing via SMI system management interface
580  *
581  * Open event file descriptor
582  *    use ioctl AMDKFD_IOC_SMI_EVENTS, pass in gpuid and return a anonymous file
583  *    descriptor to receive SMI events.
584  *    If calling with sudo permission, then file descriptor can be used to receive
585  *    SVM events from all processes, otherwise, to only receive SVM events of same
586  *    process.
587  *
588  * To enable the SVM event
589  *    Write event file descriptor with KFD_SMI_EVENT_MASK_FROM_INDEX(event) bitmap
590  *    mask to start record the event to the kfifo, use bitmap mask combination
591  *    for multiple events. New event mask will overwrite the previous event mask.
592  *    KFD_SMI_EVENT_MASK_FROM_INDEX(KFD_SMI_EVENT_ALL_PROCESS) bit requires sudo
593  *    permisson to receive SVM events from all process.
594  *
595  * To receive the event
596  *    Application can poll file descriptor to wait for the events, then read event
597  *    from the file into a buffer. Each event is one line string message, starting
598  *    with the event id, then the event specific information.
599  *
600  * To decode event information
601  *    The following event format string macro can be used with sscanf to decode
602  *    the specific event information.
603  *    event triggers: the reason to generate the event, defined as enum for unmap,
604  *    eviction and migrate events.
605  *    node, from, to, prefetch_loc, preferred_loc: GPU ID, or 0 for system memory.
606  *    addr: user mode address, in pages
607  *    size: in pages
608  *    pid: the process ID to generate the event
609  *    ns: timestamp in nanosecond-resolution, starts at system boot time but
610  *        stops during suspend
611  *    migrate_update: GPU page fault is recovered by 'M' for migrate, 'U' for update
612  *    rw: 'W' for write page fault, 'R' for read page fault
613  *    rescheduled: 'R' if the queue restore failed and rescheduled to try again
614  *    error_code: migrate failure error code, 0 if no error
615  */
616 #define KFD_EVENT_FMT_UPDATE_GPU_RESET(reset_seq_num, reset_cause)\
617 		"%x %s\n", (reset_seq_num), (reset_cause)
618 
619 #define KFD_EVENT_FMT_THERMAL_THROTTLING(bitmask, counter)\
620 		"%llx:%llx\n", (bitmask), (counter)
621 
622 #define KFD_EVENT_FMT_VMFAULT(pid, task_name)\
623 		"%x:%s\n", (pid), (task_name)
624 
625 #define KFD_EVENT_FMT_PAGEFAULT_START(ns, pid, addr, node, rw)\
626 		"%lld -%d @%lx(%x) %c\n", (ns), (pid), (addr), (node), (rw)
627 
628 #define KFD_EVENT_FMT_PAGEFAULT_END(ns, pid, addr, node, migrate_update)\
629 		"%lld -%d @%lx(%x) %c\n", (ns), (pid), (addr), (node), (migrate_update)
630 
631 #define KFD_EVENT_FMT_MIGRATE_START(ns, pid, start, size, from, to, prefetch_loc,\
632 		preferred_loc, migrate_trigger)\
633 		"%lld -%d @%lx(%lx) %x->%x %x:%x %d\n", (ns), (pid), (start), (size),\
634 		(from), (to), (prefetch_loc), (preferred_loc), (migrate_trigger)
635 
636 #define KFD_EVENT_FMT_MIGRATE_END(ns, pid, start, size, from, to, migrate_trigger, error_code) \
637 		"%lld -%d @%lx(%lx) %x->%x %d %d\n", (ns), (pid), (start), (size),\
638 		(from), (to), (migrate_trigger), (error_code)
639 
640 #define KFD_EVENT_FMT_QUEUE_EVICTION(ns, pid, node, evict_trigger)\
641 		"%lld -%d %x %d\n", (ns), (pid), (node), (evict_trigger)
642 
643 #define KFD_EVENT_FMT_QUEUE_RESTORE(ns, pid, node, rescheduled)\
644 		"%lld -%d %x %c\n", (ns), (pid), (node), (rescheduled)
645 
646 #define KFD_EVENT_FMT_UNMAP_FROM_GPU(ns, pid, addr, size, node, unmap_trigger)\
647 		"%lld -%d @%lx(%lx) %x %d\n", (ns), (pid), (addr), (size),\
648 		(node), (unmap_trigger)
649 
650 /**************************************************************************************************
651  * CRIU IOCTLs (Checkpoint Restore In Userspace)
652  *
653  * When checkpointing a process, the userspace application will perform:
654  * 1. PROCESS_INFO op to determine current process information. This pauses execution and evicts
655  *    all the queues.
656  * 2. CHECKPOINT op to checkpoint process contents (BOs, queues, events, svm-ranges)
657  * 3. UNPAUSE op to un-evict all the queues
658  *
659  * When restoring a process, the CRIU userspace application will perform:
660  *
661  * 1. RESTORE op to restore process contents
662  * 2. RESUME op to start the process
663  *
664  * Note: Queues are forced into an evicted state after a successful PROCESS_INFO. User
665  * application needs to perform an UNPAUSE operation after calling PROCESS_INFO.
666  */
667 
668 enum kfd_criu_op {
669 	KFD_CRIU_OP_PROCESS_INFO,
670 	KFD_CRIU_OP_CHECKPOINT,
671 	KFD_CRIU_OP_UNPAUSE,
672 	KFD_CRIU_OP_RESTORE,
673 	KFD_CRIU_OP_RESUME,
674 };
675 
676 /**
677  * kfd_ioctl_criu_args - Arguments perform CRIU operation
678  * @devices:		[in/out] User pointer to memory location for devices information.
679  * 			This is an array of type kfd_criu_device_bucket.
680  * @bos:		[in/out] User pointer to memory location for BOs information
681  * 			This is an array of type kfd_criu_bo_bucket.
682  * @priv_data:		[in/out] User pointer to memory location for private data
683  * @priv_data_size:	[in/out] Size of priv_data in bytes
684  * @num_devices:	[in/out] Number of GPUs used by process. Size of @devices array.
685  * @num_bos		[in/out] Number of BOs used by process. Size of @bos array.
686  * @num_objects:	[in/out] Number of objects used by process. Objects are opaque to
687  *				 user application.
688  * @pid:		[in/out] PID of the process being checkpointed
689  * @op			[in] Type of operation (kfd_criu_op)
690  *
691  * Return: 0 on success, -errno on failure
692  */
693 struct kfd_ioctl_criu_args {
694 	__u64 devices;		/* Used during ops: CHECKPOINT, RESTORE */
695 	__u64 bos;		/* Used during ops: CHECKPOINT, RESTORE */
696 	__u64 priv_data;	/* Used during ops: CHECKPOINT, RESTORE */
697 	__u64 priv_data_size;	/* Used during ops: PROCESS_INFO, RESTORE */
698 	__u32 num_devices;	/* Used during ops: PROCESS_INFO, RESTORE */
699 	__u32 num_bos;		/* Used during ops: PROCESS_INFO, RESTORE */
700 	__u32 num_objects;	/* Used during ops: PROCESS_INFO, RESTORE */
701 	__u32 pid;		/* Used during ops: PROCESS_INFO, RESUME */
702 	__u32 op;
703 };
704 
705 struct kfd_criu_device_bucket {
706 	__u32 user_gpu_id;
707 	__u32 actual_gpu_id;
708 	__u32 drm_fd;
709 	__u32 pad;
710 };
711 
712 struct kfd_criu_bo_bucket {
713 	__u64 addr;
714 	__u64 size;
715 	__u64 offset;
716 	__u64 restored_offset;    /* During restore, updated offset for BO */
717 	__u32 gpu_id;             /* This is the user_gpu_id */
718 	__u32 alloc_flags;
719 	__u32 dmabuf_fd;
720 	__u32 pad;
721 };
722 
723 /* CRIU IOCTLs - END */
724 /**************************************************************************************************/
725 
726 /* Register offset inside the remapped mmio page
727  */
728 enum kfd_mmio_remap {
729 	KFD_MMIO_REMAP_HDP_MEM_FLUSH_CNTL = 0,
730 	KFD_MMIO_REMAP_HDP_REG_FLUSH_CNTL = 4,
731 };
732 
733 /* Guarantee host access to memory */
734 #define KFD_IOCTL_SVM_FLAG_HOST_ACCESS 0x00000001
735 /* Fine grained coherency between all devices with access */
736 #define KFD_IOCTL_SVM_FLAG_COHERENT    0x00000002
737 /* Use any GPU in same hive as preferred device */
738 #define KFD_IOCTL_SVM_FLAG_HIVE_LOCAL  0x00000004
739 /* GPUs only read, allows replication */
740 #define KFD_IOCTL_SVM_FLAG_GPU_RO      0x00000008
741 /* Allow execution on GPU */
742 #define KFD_IOCTL_SVM_FLAG_GPU_EXEC    0x00000010
743 /* GPUs mostly read, may allow similar optimizations as RO, but writes fault */
744 #define KFD_IOCTL_SVM_FLAG_GPU_READ_MOSTLY     0x00000020
745 /* Keep GPU memory mapping always valid as if XNACK is disable */
746 #define KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED   0x00000040
747 /* Fine grained coherency between all devices using device-scope atomics */
748 #define KFD_IOCTL_SVM_FLAG_EXT_COHERENT        0x00000080
749 
750 /**
751  * kfd_ioctl_svm_op - SVM ioctl operations
752  *
753  * @KFD_IOCTL_SVM_OP_SET_ATTR: Modify one or more attributes
754  * @KFD_IOCTL_SVM_OP_GET_ATTR: Query one or more attributes
755  */
756 enum kfd_ioctl_svm_op {
757 	KFD_IOCTL_SVM_OP_SET_ATTR,
758 	KFD_IOCTL_SVM_OP_GET_ATTR
759 };
760 
761 /** kfd_ioctl_svm_location - Enum for preferred and prefetch locations
762  *
763  * GPU IDs are used to specify GPUs as preferred and prefetch locations.
764  * Below definitions are used for system memory or for leaving the preferred
765  * location unspecified.
766  */
767 enum kfd_ioctl_svm_location {
768 	KFD_IOCTL_SVM_LOCATION_SYSMEM = 0,
769 	KFD_IOCTL_SVM_LOCATION_UNDEFINED = 0xffffffff
770 };
771 
772 /**
773  * kfd_ioctl_svm_attr_type - SVM attribute types
774  *
775  * @KFD_IOCTL_SVM_ATTR_PREFERRED_LOC: gpuid of the preferred location, 0 for
776  *                                    system memory
777  * @KFD_IOCTL_SVM_ATTR_PREFETCH_LOC: gpuid of the prefetch location, 0 for
778  *                                   system memory. Setting this triggers an
779  *                                   immediate prefetch (migration).
780  * @KFD_IOCTL_SVM_ATTR_ACCESS:
781  * @KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE:
782  * @KFD_IOCTL_SVM_ATTR_NO_ACCESS: specify memory access for the gpuid given
783  *                                by the attribute value
784  * @KFD_IOCTL_SVM_ATTR_SET_FLAGS: bitmask of flags to set (see
785  *                                KFD_IOCTL_SVM_FLAG_...)
786  * @KFD_IOCTL_SVM_ATTR_CLR_FLAGS: bitmask of flags to clear
787  * @KFD_IOCTL_SVM_ATTR_GRANULARITY: migration granularity
788  *                                  (log2 num pages)
789  */
790 enum kfd_ioctl_svm_attr_type {
791 	KFD_IOCTL_SVM_ATTR_PREFERRED_LOC,
792 	KFD_IOCTL_SVM_ATTR_PREFETCH_LOC,
793 	KFD_IOCTL_SVM_ATTR_ACCESS,
794 	KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE,
795 	KFD_IOCTL_SVM_ATTR_NO_ACCESS,
796 	KFD_IOCTL_SVM_ATTR_SET_FLAGS,
797 	KFD_IOCTL_SVM_ATTR_CLR_FLAGS,
798 	KFD_IOCTL_SVM_ATTR_GRANULARITY
799 };
800 
801 /**
802  * kfd_ioctl_svm_attribute - Attributes as pairs of type and value
803  *
804  * The meaning of the @value depends on the attribute type.
805  *
806  * @type: attribute type (see enum @kfd_ioctl_svm_attr_type)
807  * @value: attribute value
808  */
809 struct kfd_ioctl_svm_attribute {
810 	__u32 type;
811 	__u32 value;
812 };
813 
814 /**
815  * kfd_ioctl_svm_args - Arguments for SVM ioctl
816  *
817  * @op specifies the operation to perform (see enum
818  * @kfd_ioctl_svm_op).  @start_addr and @size are common for all
819  * operations.
820  *
821  * A variable number of attributes can be given in @attrs.
822  * @nattr specifies the number of attributes. New attributes can be
823  * added in the future without breaking the ABI. If unknown attributes
824  * are given, the function returns -EINVAL.
825  *
826  * @KFD_IOCTL_SVM_OP_SET_ATTR sets attributes for a virtual address
827  * range. It may overlap existing virtual address ranges. If it does,
828  * the existing ranges will be split such that the attribute changes
829  * only apply to the specified address range.
830  *
831  * @KFD_IOCTL_SVM_OP_GET_ATTR returns the intersection of attributes
832  * over all memory in the given range and returns the result as the
833  * attribute value. If different pages have different preferred or
834  * prefetch locations, 0xffffffff will be returned for
835  * @KFD_IOCTL_SVM_ATTR_PREFERRED_LOC or
836  * @KFD_IOCTL_SVM_ATTR_PREFETCH_LOC resepctively. For
837  * @KFD_IOCTL_SVM_ATTR_SET_FLAGS, flags of all pages will be
838  * aggregated by bitwise AND. That means, a flag will be set in the
839  * output, if that flag is set for all pages in the range. For
840  * @KFD_IOCTL_SVM_ATTR_CLR_FLAGS, flags of all pages will be
841  * aggregated by bitwise NOR. That means, a flag will be set in the
842  * output, if that flag is clear for all pages in the range.
843  * The minimum migration granularity throughout the range will be
844  * returned for @KFD_IOCTL_SVM_ATTR_GRANULARITY.
845  *
846  * Querying of accessibility attributes works by initializing the
847  * attribute type to @KFD_IOCTL_SVM_ATTR_ACCESS and the value to the
848  * GPUID being queried. Multiple attributes can be given to allow
849  * querying multiple GPUIDs. The ioctl function overwrites the
850  * attribute type to indicate the access for the specified GPU.
851  */
852 struct kfd_ioctl_svm_args {
853 	__u64 start_addr;
854 	__u64 size;
855 	__u32 op;
856 	__u32 nattr;
857 	/* Variable length array of attributes */
858 	struct kfd_ioctl_svm_attribute attrs[];
859 };
860 
861 /**
862  * kfd_ioctl_set_xnack_mode_args - Arguments for set_xnack_mode
863  *
864  * @xnack_enabled:       [in/out] Whether to enable XNACK mode for this process
865  *
866  * @xnack_enabled indicates whether recoverable page faults should be
867  * enabled for the current process. 0 means disabled, positive means
868  * enabled, negative means leave unchanged. If enabled, virtual address
869  * translations on GFXv9 and later AMD GPUs can return XNACK and retry
870  * the access until a valid PTE is available. This is used to implement
871  * device page faults.
872  *
873  * On output, @xnack_enabled returns the (new) current mode (0 or
874  * positive). Therefore, a negative input value can be used to query
875  * the current mode without changing it.
876  *
877  * The XNACK mode fundamentally changes the way SVM managed memory works
878  * in the driver, with subtle effects on application performance and
879  * functionality.
880  *
881  * Enabling XNACK mode requires shader programs to be compiled
882  * differently. Furthermore, not all GPUs support changing the mode
883  * per-process. Therefore changing the mode is only allowed while no
884  * user mode queues exist in the process. This ensure that no shader
885  * code is running that may be compiled for the wrong mode. And GPUs
886  * that cannot change to the requested mode will prevent the XNACK
887  * mode from occurring. All GPUs used by the process must be in the
888  * same XNACK mode.
889  *
890  * GFXv8 or older GPUs do not support 48 bit virtual addresses or SVM.
891  * Therefore those GPUs are not considered for the XNACK mode switch.
892  *
893  * Return: 0 on success, -errno on failure
894  */
895 struct kfd_ioctl_set_xnack_mode_args {
896 	__s32 xnack_enabled;
897 };
898 
899 /* Wave launch override modes */
900 enum kfd_dbg_trap_override_mode {
901 	KFD_DBG_TRAP_OVERRIDE_OR = 0,
902 	KFD_DBG_TRAP_OVERRIDE_REPLACE = 1
903 };
904 
905 /* Wave launch overrides */
906 enum kfd_dbg_trap_mask {
907 	KFD_DBG_TRAP_MASK_FP_INVALID = 1,
908 	KFD_DBG_TRAP_MASK_FP_INPUT_DENORMAL = 2,
909 	KFD_DBG_TRAP_MASK_FP_DIVIDE_BY_ZERO = 4,
910 	KFD_DBG_TRAP_MASK_FP_OVERFLOW = 8,
911 	KFD_DBG_TRAP_MASK_FP_UNDERFLOW = 16,
912 	KFD_DBG_TRAP_MASK_FP_INEXACT = 32,
913 	KFD_DBG_TRAP_MASK_INT_DIVIDE_BY_ZERO = 64,
914 	KFD_DBG_TRAP_MASK_DBG_ADDRESS_WATCH = 128,
915 	KFD_DBG_TRAP_MASK_DBG_MEMORY_VIOLATION = 256,
916 	KFD_DBG_TRAP_MASK_TRAP_ON_WAVE_START = (1 << 30),
917 	KFD_DBG_TRAP_MASK_TRAP_ON_WAVE_END = (1 << 31)
918 };
919 
920 /* Wave launch modes */
921 enum kfd_dbg_trap_wave_launch_mode {
922 	KFD_DBG_TRAP_WAVE_LAUNCH_MODE_NORMAL = 0,
923 	KFD_DBG_TRAP_WAVE_LAUNCH_MODE_HALT = 1,
924 	KFD_DBG_TRAP_WAVE_LAUNCH_MODE_DEBUG = 3
925 };
926 
927 /* Address watch modes */
928 enum kfd_dbg_trap_address_watch_mode {
929 	KFD_DBG_TRAP_ADDRESS_WATCH_MODE_READ = 0,
930 	KFD_DBG_TRAP_ADDRESS_WATCH_MODE_NONREAD = 1,
931 	KFD_DBG_TRAP_ADDRESS_WATCH_MODE_ATOMIC = 2,
932 	KFD_DBG_TRAP_ADDRESS_WATCH_MODE_ALL = 3
933 };
934 
935 /* Additional wave settings */
936 enum kfd_dbg_trap_flags {
937 	KFD_DBG_TRAP_FLAG_SINGLE_MEM_OP = 1,
938 	KFD_DBG_TRAP_FLAG_SINGLE_ALU_OP = 2,
939 };
940 
941 /* Trap exceptions */
942 enum kfd_dbg_trap_exception_code {
943 	EC_NONE = 0,
944 	/* per queue */
945 	EC_QUEUE_WAVE_ABORT = 1,
946 	EC_QUEUE_WAVE_TRAP = 2,
947 	EC_QUEUE_WAVE_MATH_ERROR = 3,
948 	EC_QUEUE_WAVE_ILLEGAL_INSTRUCTION = 4,
949 	EC_QUEUE_WAVE_MEMORY_VIOLATION = 5,
950 	EC_QUEUE_WAVE_APERTURE_VIOLATION = 6,
951 	EC_QUEUE_PACKET_DISPATCH_DIM_INVALID = 16,
952 	EC_QUEUE_PACKET_DISPATCH_GROUP_SEGMENT_SIZE_INVALID = 17,
953 	EC_QUEUE_PACKET_DISPATCH_CODE_INVALID = 18,
954 	EC_QUEUE_PACKET_RESERVED = 19,
955 	EC_QUEUE_PACKET_UNSUPPORTED = 20,
956 	EC_QUEUE_PACKET_DISPATCH_WORK_GROUP_SIZE_INVALID = 21,
957 	EC_QUEUE_PACKET_DISPATCH_REGISTER_INVALID = 22,
958 	EC_QUEUE_PACKET_VENDOR_UNSUPPORTED = 23,
959 	EC_QUEUE_PREEMPTION_ERROR = 30,
960 	EC_QUEUE_NEW = 31,
961 	/* per device */
962 	EC_DEVICE_QUEUE_DELETE = 32,
963 	EC_DEVICE_MEMORY_VIOLATION = 33,
964 	EC_DEVICE_RAS_ERROR = 34,
965 	EC_DEVICE_FATAL_HALT = 35,
966 	EC_DEVICE_NEW = 36,
967 	/* per process */
968 	EC_PROCESS_RUNTIME = 48,
969 	EC_PROCESS_DEVICE_REMOVE = 49,
970 	EC_MAX
971 };
972 
973 /* Mask generated by ecode in kfd_dbg_trap_exception_code */
974 #define KFD_EC_MASK(ecode)	(1ULL << (ecode - 1))
975 
976 /* Masks for exception code type checks below */
977 #define KFD_EC_MASK_QUEUE	(KFD_EC_MASK(EC_QUEUE_WAVE_ABORT) |	\
978 				 KFD_EC_MASK(EC_QUEUE_WAVE_TRAP) |	\
979 				 KFD_EC_MASK(EC_QUEUE_WAVE_MATH_ERROR) |	\
980 				 KFD_EC_MASK(EC_QUEUE_WAVE_ILLEGAL_INSTRUCTION) |	\
981 				 KFD_EC_MASK(EC_QUEUE_WAVE_MEMORY_VIOLATION) |	\
982 				 KFD_EC_MASK(EC_QUEUE_WAVE_APERTURE_VIOLATION) |	\
983 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_DIM_INVALID) |	\
984 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_GROUP_SEGMENT_SIZE_INVALID) |	\
985 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_CODE_INVALID) |	\
986 				 KFD_EC_MASK(EC_QUEUE_PACKET_RESERVED) |	\
987 				 KFD_EC_MASK(EC_QUEUE_PACKET_UNSUPPORTED) |	\
988 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_WORK_GROUP_SIZE_INVALID) |	\
989 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_REGISTER_INVALID) |	\
990 				 KFD_EC_MASK(EC_QUEUE_PACKET_VENDOR_UNSUPPORTED)	|	\
991 				 KFD_EC_MASK(EC_QUEUE_PREEMPTION_ERROR)	|	\
992 				 KFD_EC_MASK(EC_QUEUE_NEW))
993 #define KFD_EC_MASK_DEVICE	(KFD_EC_MASK(EC_DEVICE_QUEUE_DELETE) |		\
994 				 KFD_EC_MASK(EC_DEVICE_RAS_ERROR) |		\
995 				 KFD_EC_MASK(EC_DEVICE_FATAL_HALT) |		\
996 				 KFD_EC_MASK(EC_DEVICE_MEMORY_VIOLATION) |	\
997 				 KFD_EC_MASK(EC_DEVICE_NEW))
998 #define KFD_EC_MASK_PROCESS	(KFD_EC_MASK(EC_PROCESS_RUNTIME) |	\
999 				 KFD_EC_MASK(EC_PROCESS_DEVICE_REMOVE))
1000 #define KFD_EC_MASK_PACKET	(KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_DIM_INVALID) |	\
1001 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_GROUP_SEGMENT_SIZE_INVALID) |	\
1002 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_CODE_INVALID) |	\
1003 				 KFD_EC_MASK(EC_QUEUE_PACKET_RESERVED) |	\
1004 				 KFD_EC_MASK(EC_QUEUE_PACKET_UNSUPPORTED) |	\
1005 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_WORK_GROUP_SIZE_INVALID) |	\
1006 				 KFD_EC_MASK(EC_QUEUE_PACKET_DISPATCH_REGISTER_INVALID) |	\
1007 				 KFD_EC_MASK(EC_QUEUE_PACKET_VENDOR_UNSUPPORTED))
1008 
1009 /* Checks for exception code types for KFD search */
1010 #define KFD_DBG_EC_IS_VALID(ecode) (ecode > EC_NONE && ecode < EC_MAX)
1011 #define KFD_DBG_EC_TYPE_IS_QUEUE(ecode)					\
1012 			(KFD_DBG_EC_IS_VALID(ecode) && !!(KFD_EC_MASK(ecode) & KFD_EC_MASK_QUEUE))
1013 #define KFD_DBG_EC_TYPE_IS_DEVICE(ecode)				\
1014 			(KFD_DBG_EC_IS_VALID(ecode) && !!(KFD_EC_MASK(ecode) & KFD_EC_MASK_DEVICE))
1015 #define KFD_DBG_EC_TYPE_IS_PROCESS(ecode)				\
1016 			(KFD_DBG_EC_IS_VALID(ecode) && !!(KFD_EC_MASK(ecode) & KFD_EC_MASK_PROCESS))
1017 #define KFD_DBG_EC_TYPE_IS_PACKET(ecode)				\
1018 			(KFD_DBG_EC_IS_VALID(ecode) && !!(KFD_EC_MASK(ecode) & KFD_EC_MASK_PACKET))
1019 
1020 
1021 /* Runtime enable states */
1022 enum kfd_dbg_runtime_state {
1023 	DEBUG_RUNTIME_STATE_DISABLED = 0,
1024 	DEBUG_RUNTIME_STATE_ENABLED = 1,
1025 	DEBUG_RUNTIME_STATE_ENABLED_BUSY = 2,
1026 	DEBUG_RUNTIME_STATE_ENABLED_ERROR = 3
1027 };
1028 
1029 /* Runtime enable status */
1030 struct kfd_runtime_info {
1031 	__u64 r_debug;
1032 	__u32 runtime_state;
1033 	__u32 ttmp_setup;
1034 };
1035 
1036 /* Enable modes for runtime enable */
1037 #define KFD_RUNTIME_ENABLE_MODE_ENABLE_MASK	1
1038 #define KFD_RUNTIME_ENABLE_MODE_TTMP_SAVE_MASK	2
1039 
1040 /**
1041  * kfd_ioctl_runtime_enable_args - Arguments for runtime enable
1042  *
1043  * Coordinates debug exception signalling and debug device enablement with runtime.
1044  *
1045  * @r_debug - pointer to user struct for sharing information between ROCr and the debuggger
1046  * @mode_mask - mask to set mode
1047  *	KFD_RUNTIME_ENABLE_MODE_ENABLE_MASK - enable runtime for debugging, otherwise disable
1048  *	KFD_RUNTIME_ENABLE_MODE_TTMP_SAVE_MASK - enable trap temporary setup (ignore on disable)
1049  * @capabilities_mask - mask to notify runtime on what KFD supports
1050  *
1051  * Return - 0 on SUCCESS.
1052  *	  - EBUSY if runtime enable call already pending.
1053  *	  - EEXIST if user queues already active prior to call.
1054  *	    If process is debug enabled, runtime enable will enable debug devices and
1055  *	    wait for debugger process to send runtime exception EC_PROCESS_RUNTIME
1056  *	    to unblock - see kfd_ioctl_dbg_trap_args.
1057  *
1058  */
1059 struct kfd_ioctl_runtime_enable_args {
1060 	__u64 r_debug;
1061 	__u32 mode_mask;
1062 	__u32 capabilities_mask;
1063 };
1064 
1065 /* Queue information */
1066 struct kfd_queue_snapshot_entry {
1067 	__u64 exception_status;
1068 	__u64 ring_base_address;
1069 	__u64 write_pointer_address;
1070 	__u64 read_pointer_address;
1071 	__u64 ctx_save_restore_address;
1072 	__u32 queue_id;
1073 	__u32 gpu_id;
1074 	__u32 ring_size;
1075 	__u32 queue_type;
1076 	__u32 ctx_save_restore_area_size;
1077 	__u32 reserved;
1078 };
1079 
1080 /* Queue status return for suspend/resume */
1081 #define KFD_DBG_QUEUE_ERROR_BIT		30
1082 #define KFD_DBG_QUEUE_INVALID_BIT	31
1083 #define KFD_DBG_QUEUE_ERROR_MASK	(1 << KFD_DBG_QUEUE_ERROR_BIT)
1084 #define KFD_DBG_QUEUE_INVALID_MASK	(1 << KFD_DBG_QUEUE_INVALID_BIT)
1085 
1086 /* Context save area header information */
1087 struct kfd_context_save_area_header {
1088 	struct {
1089 		__u32 control_stack_offset;
1090 		__u32 control_stack_size;
1091 		__u32 wave_state_offset;
1092 		__u32 wave_state_size;
1093 	} wave_state;
1094 	__u32 debug_offset;
1095 	__u32 debug_size;
1096 	__u64 err_payload_addr;
1097 	__u32 err_event_id;
1098 	__u32 reserved1;
1099 };
1100 
1101 /*
1102  * Debug operations
1103  *
1104  * For specifics on usage and return values, see documentation per operation
1105  * below.  Otherwise, generic error returns apply:
1106  *	- ESRCH if the process to debug does not exist.
1107  *
1108  *	- EINVAL (with KFD_IOC_DBG_TRAP_ENABLE exempt) if operation
1109  *		 KFD_IOC_DBG_TRAP_ENABLE has not succeeded prior.
1110  *		 Also returns this error if GPU hardware scheduling is not supported.
1111  *
1112  *	- EPERM (with KFD_IOC_DBG_TRAP_DISABLE exempt) if target process is not
1113  *		 PTRACE_ATTACHED.  KFD_IOC_DBG_TRAP_DISABLE is exempt to allow
1114  *		 clean up of debug mode as long as process is debug enabled.
1115  *
1116  *	- EACCES if any DBG_HW_OP (debug hardware operation) is requested when
1117  *		 AMDKFD_IOC_RUNTIME_ENABLE has not succeeded prior.
1118  *
1119  *	- ENODEV if any GPU does not support debugging on a DBG_HW_OP call.
1120  *
1121  *	- Other errors may be returned when a DBG_HW_OP occurs while the GPU
1122  *	  is in a fatal state.
1123  *
1124  */
1125 enum kfd_dbg_trap_operations {
1126 	KFD_IOC_DBG_TRAP_ENABLE = 0,
1127 	KFD_IOC_DBG_TRAP_DISABLE = 1,
1128 	KFD_IOC_DBG_TRAP_SEND_RUNTIME_EVENT = 2,
1129 	KFD_IOC_DBG_TRAP_SET_EXCEPTIONS_ENABLED = 3,
1130 	KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_OVERRIDE = 4,  /* DBG_HW_OP */
1131 	KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE = 5,      /* DBG_HW_OP */
1132 	KFD_IOC_DBG_TRAP_SUSPEND_QUEUES = 6,		/* DBG_HW_OP */
1133 	KFD_IOC_DBG_TRAP_RESUME_QUEUES = 7,		/* DBG_HW_OP */
1134 	KFD_IOC_DBG_TRAP_SET_NODE_ADDRESS_WATCH = 8,	/* DBG_HW_OP */
1135 	KFD_IOC_DBG_TRAP_CLEAR_NODE_ADDRESS_WATCH = 9,	/* DBG_HW_OP */
1136 	KFD_IOC_DBG_TRAP_SET_FLAGS = 10,
1137 	KFD_IOC_DBG_TRAP_QUERY_DEBUG_EVENT = 11,
1138 	KFD_IOC_DBG_TRAP_QUERY_EXCEPTION_INFO = 12,
1139 	KFD_IOC_DBG_TRAP_GET_QUEUE_SNAPSHOT = 13,
1140 	KFD_IOC_DBG_TRAP_GET_DEVICE_SNAPSHOT = 14
1141 };
1142 
1143 /**
1144  * kfd_ioctl_dbg_trap_enable_args
1145  *
1146  *     Arguments for KFD_IOC_DBG_TRAP_ENABLE.
1147  *
1148  *     Enables debug session for target process. Call @op KFD_IOC_DBG_TRAP_DISABLE in
1149  *     kfd_ioctl_dbg_trap_args to disable debug session.
1150  *
1151  *     @exception_mask (IN)	- exceptions to raise to the debugger
1152  *     @rinfo_ptr      (IN)	- pointer to runtime info buffer (see kfd_runtime_info)
1153  *     @rinfo_size     (IN/OUT)	- size of runtime info buffer in bytes
1154  *     @dbg_fd	       (IN)	- fd the KFD will nofify the debugger with of raised
1155  *				  exceptions set in exception_mask.
1156  *
1157  *     Generic errors apply (see kfd_dbg_trap_operations).
1158  *     Return - 0 on SUCCESS.
1159  *		Copies KFD saved kfd_runtime_info to @rinfo_ptr on enable.
1160  *		Size of kfd_runtime saved by the KFD returned to @rinfo_size.
1161  *            - EBADF if KFD cannot get a reference to dbg_fd.
1162  *            - EFAULT if KFD cannot copy runtime info to rinfo_ptr.
1163  *            - EINVAL if target process is already debug enabled.
1164  *
1165  */
1166 struct kfd_ioctl_dbg_trap_enable_args {
1167 	__u64 exception_mask;
1168 	__u64 rinfo_ptr;
1169 	__u32 rinfo_size;
1170 	__u32 dbg_fd;
1171 };
1172 
1173 /**
1174  * kfd_ioctl_dbg_trap_send_runtime_event_args
1175  *
1176  *
1177  *     Arguments for KFD_IOC_DBG_TRAP_SEND_RUNTIME_EVENT.
1178  *     Raises exceptions to runtime.
1179  *
1180  *     @exception_mask (IN) - exceptions to raise to runtime
1181  *     @gpu_id	       (IN) - target device id
1182  *     @queue_id       (IN) - target queue id
1183  *
1184  *     Generic errors apply (see kfd_dbg_trap_operations).
1185  *     Return - 0 on SUCCESS.
1186  *	      - ENODEV if gpu_id not found.
1187  *		If exception_mask contains EC_PROCESS_RUNTIME, unblocks pending
1188  *		AMDKFD_IOC_RUNTIME_ENABLE call - see kfd_ioctl_runtime_enable_args.
1189  *		All other exceptions are raised to runtime through err_payload_addr.
1190  *		See kfd_context_save_area_header.
1191  */
1192 struct kfd_ioctl_dbg_trap_send_runtime_event_args {
1193 	__u64 exception_mask;
1194 	__u32 gpu_id;
1195 	__u32 queue_id;
1196 };
1197 
1198 /**
1199  * kfd_ioctl_dbg_trap_set_exceptions_enabled_args
1200  *
1201  *     Arguments for KFD_IOC_SET_EXCEPTIONS_ENABLED
1202  *     Set new exceptions to be raised to the debugger.
1203  *
1204  *     @exception_mask (IN) - new exceptions to raise the debugger
1205  *
1206  *     Generic errors apply (see kfd_dbg_trap_operations).
1207  *     Return - 0 on SUCCESS.
1208  */
1209 struct kfd_ioctl_dbg_trap_set_exceptions_enabled_args {
1210 	__u64 exception_mask;
1211 };
1212 
1213 /**
1214  * kfd_ioctl_dbg_trap_set_wave_launch_override_args
1215  *
1216  *     Arguments for KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_OVERRIDE
1217  *     Enable HW exceptions to raise trap.
1218  *
1219  *     @override_mode	     (IN)     - see kfd_dbg_trap_override_mode
1220  *     @enable_mask	     (IN/OUT) - reference kfd_dbg_trap_mask.
1221  *					IN is the override modes requested to be enabled.
1222  *					OUT is referenced in Return below.
1223  *     @support_request_mask (IN/OUT) - reference kfd_dbg_trap_mask.
1224  *					IN is the override modes requested for support check.
1225  *					OUT is referenced in Return below.
1226  *
1227  *     Generic errors apply (see kfd_dbg_trap_operations).
1228  *     Return - 0 on SUCCESS.
1229  *		Previous enablement is returned in @enable_mask.
1230  *		Actual override support is returned in @support_request_mask.
1231  *	      - EINVAL if override mode is not supported.
1232  *	      - EACCES if trap support requested is not actually supported.
1233  *		i.e. enable_mask (IN) is not a subset of support_request_mask (OUT).
1234  *		Otherwise it is considered a generic error (see kfd_dbg_trap_operations).
1235  */
1236 struct kfd_ioctl_dbg_trap_set_wave_launch_override_args {
1237 	__u32 override_mode;
1238 	__u32 enable_mask;
1239 	__u32 support_request_mask;
1240 	__u32 pad;
1241 };
1242 
1243 /**
1244  * kfd_ioctl_dbg_trap_set_wave_launch_mode_args
1245  *
1246  *     Arguments for KFD_IOC_DBG_TRAP_SET_WAVE_LAUNCH_MODE
1247  *     Set wave launch mode.
1248  *
1249  *     @mode (IN) - see kfd_dbg_trap_wave_launch_mode
1250  *
1251  *     Generic errors apply (see kfd_dbg_trap_operations).
1252  *     Return - 0 on SUCCESS.
1253  */
1254 struct kfd_ioctl_dbg_trap_set_wave_launch_mode_args {
1255 	__u32 launch_mode;
1256 	__u32 pad;
1257 };
1258 
1259 /**
1260  * kfd_ioctl_dbg_trap_suspend_queues_ags
1261  *
1262  *     Arguments for KFD_IOC_DBG_TRAP_SUSPEND_QUEUES
1263  *     Suspend queues.
1264  *
1265  *     @exception_mask	(IN) - raised exceptions to clear
1266  *     @queue_array_ptr (IN) - pointer to array of queue ids (u32 per queue id)
1267  *			       to suspend
1268  *     @num_queues	(IN) - number of queues to suspend in @queue_array_ptr
1269  *     @grace_period	(IN) - wave time allowance before preemption
1270  *			       per 1K GPU clock cycle unit
1271  *
1272  *     Generic errors apply (see kfd_dbg_trap_operations).
1273  *     Destruction of a suspended queue is blocked until the queue is
1274  *     resumed.  This allows the debugger to access queue information and
1275  *     the its context save area without running into a race condition on
1276  *     queue destruction.
1277  *     Automatically copies per queue context save area header information
1278  *     into the save area base
1279  *     (see kfd_queue_snapshot_entry and kfd_context_save_area_header).
1280  *
1281  *     Return - Number of queues suspended on SUCCESS.
1282  *	.	KFD_DBG_QUEUE_ERROR_MASK and KFD_DBG_QUEUE_INVALID_MASK masked
1283  *		for each queue id in @queue_array_ptr array reports unsuccessful
1284  *		suspend reason.
1285  *		KFD_DBG_QUEUE_ERROR_MASK = HW failure.
1286  *		KFD_DBG_QUEUE_INVALID_MASK = queue does not exist, is new or
1287  *		is being destroyed.
1288  */
1289 struct kfd_ioctl_dbg_trap_suspend_queues_args {
1290 	__u64 exception_mask;
1291 	__u64 queue_array_ptr;
1292 	__u32 num_queues;
1293 	__u32 grace_period;
1294 };
1295 
1296 /**
1297  * kfd_ioctl_dbg_trap_resume_queues_args
1298  *
1299  *     Arguments for KFD_IOC_DBG_TRAP_RESUME_QUEUES
1300  *     Resume queues.
1301  *
1302  *     @queue_array_ptr (IN) - pointer to array of queue ids (u32 per queue id)
1303  *			       to resume
1304  *     @num_queues	(IN) - number of queues to resume in @queue_array_ptr
1305  *
1306  *     Generic errors apply (see kfd_dbg_trap_operations).
1307  *     Return - Number of queues resumed on SUCCESS.
1308  *		KFD_DBG_QUEUE_ERROR_MASK and KFD_DBG_QUEUE_INVALID_MASK mask
1309  *		for each queue id in @queue_array_ptr array reports unsuccessful
1310  *		resume reason.
1311  *		KFD_DBG_QUEUE_ERROR_MASK = HW failure.
1312  *		KFD_DBG_QUEUE_INVALID_MASK = queue does not exist.
1313  */
1314 struct kfd_ioctl_dbg_trap_resume_queues_args {
1315 	__u64 queue_array_ptr;
1316 	__u32 num_queues;
1317 	__u32 pad;
1318 };
1319 
1320 /**
1321  * kfd_ioctl_dbg_trap_set_node_address_watch_args
1322  *
1323  *     Arguments for KFD_IOC_DBG_TRAP_SET_NODE_ADDRESS_WATCH
1324  *     Sets address watch for device.
1325  *
1326  *     @address	(IN)  - watch address to set
1327  *     @mode    (IN)  - see kfd_dbg_trap_address_watch_mode
1328  *     @mask    (IN)  - watch address mask
1329  *     @gpu_id  (IN)  - target gpu to set watch point
1330  *     @id      (OUT) - watch id allocated
1331  *
1332  *     Generic errors apply (see kfd_dbg_trap_operations).
1333  *     Return - 0 on SUCCESS.
1334  *		Allocated watch ID returned to @id.
1335  *	      - ENODEV if gpu_id not found.
1336  *	      - ENOMEM if watch IDs can be allocated
1337  */
1338 struct kfd_ioctl_dbg_trap_set_node_address_watch_args {
1339 	__u64 address;
1340 	__u32 mode;
1341 	__u32 mask;
1342 	__u32 gpu_id;
1343 	__u32 id;
1344 };
1345 
1346 /**
1347  * kfd_ioctl_dbg_trap_clear_node_address_watch_args
1348  *
1349  *     Arguments for KFD_IOC_DBG_TRAP_CLEAR_NODE_ADDRESS_WATCH
1350  *     Clear address watch for device.
1351  *
1352  *     @gpu_id  (IN)  - target device to clear watch point
1353  *     @id      (IN) - allocated watch id to clear
1354  *
1355  *     Generic errors apply (see kfd_dbg_trap_operations).
1356  *     Return - 0 on SUCCESS.
1357  *	      - ENODEV if gpu_id not found.
1358  *	      - EINVAL if watch ID has not been allocated.
1359  */
1360 struct kfd_ioctl_dbg_trap_clear_node_address_watch_args {
1361 	__u32 gpu_id;
1362 	__u32 id;
1363 };
1364 
1365 /**
1366  * kfd_ioctl_dbg_trap_set_flags_args
1367  *
1368  *     Arguments for KFD_IOC_DBG_TRAP_SET_FLAGS
1369  *     Sets flags for wave behaviour.
1370  *
1371  *     @flags (IN/OUT) - IN = flags to enable, OUT = flags previously enabled
1372  *
1373  *     Generic errors apply (see kfd_dbg_trap_operations).
1374  *     Return - 0 on SUCCESS.
1375  *	      - EACCESS if any debug device does not allow flag options.
1376  */
1377 struct kfd_ioctl_dbg_trap_set_flags_args {
1378 	__u32 flags;
1379 	__u32 pad;
1380 };
1381 
1382 /**
1383  * kfd_ioctl_dbg_trap_query_debug_event_args
1384  *
1385  *     Arguments for KFD_IOC_DBG_TRAP_QUERY_DEBUG_EVENT
1386  *
1387  *     Find one or more raised exceptions. This function can return multiple
1388  *     exceptions from a single queue or a single device with one call. To find
1389  *     all raised exceptions, this function must be called repeatedly until it
1390  *     returns -EAGAIN. Returned exceptions can optionally be cleared by
1391  *     setting the corresponding bit in the @exception_mask input parameter.
1392  *     However, clearing an exception prevents retrieving further information
1393  *     about it with KFD_IOC_DBG_TRAP_QUERY_EXCEPTION_INFO.
1394  *
1395  *     @exception_mask (IN/OUT) - exception to clear (IN) and raised (OUT)
1396  *     @gpu_id	       (OUT)    - gpu id of exceptions raised
1397  *     @queue_id       (OUT)    - queue id of exceptions raised
1398  *
1399  *     Generic errors apply (see kfd_dbg_trap_operations).
1400  *     Return - 0 on raised exception found
1401  *              Raised exceptions found are returned in @exception mask
1402  *              with reported source id returned in @gpu_id or @queue_id.
1403  *            - EAGAIN if no raised exception has been found
1404  */
1405 struct kfd_ioctl_dbg_trap_query_debug_event_args {
1406 	__u64 exception_mask;
1407 	__u32 gpu_id;
1408 	__u32 queue_id;
1409 };
1410 
1411 /**
1412  * kfd_ioctl_dbg_trap_query_exception_info_args
1413  *
1414  *     Arguments KFD_IOC_DBG_TRAP_QUERY_EXCEPTION_INFO
1415  *     Get additional info on raised exception.
1416  *
1417  *     @info_ptr	(IN)	 - pointer to exception info buffer to copy to
1418  *     @info_size	(IN/OUT) - exception info buffer size (bytes)
1419  *     @source_id	(IN)     - target gpu or queue id
1420  *     @exception_code	(IN)     - target exception
1421  *     @clear_exception	(IN)     - clear raised @exception_code exception
1422  *				   (0 = false, 1 = true)
1423  *
1424  *     Generic errors apply (see kfd_dbg_trap_operations).
1425  *     Return - 0 on SUCCESS.
1426  *              If @exception_code is EC_DEVICE_MEMORY_VIOLATION, copy @info_size(OUT)
1427  *		bytes of memory exception data to @info_ptr.
1428  *              If @exception_code is EC_PROCESS_RUNTIME, copy saved
1429  *              kfd_runtime_info to @info_ptr.
1430  *              Actual required @info_ptr size (bytes) is returned in @info_size.
1431  */
1432 struct kfd_ioctl_dbg_trap_query_exception_info_args {
1433 	__u64 info_ptr;
1434 	__u32 info_size;
1435 	__u32 source_id;
1436 	__u32 exception_code;
1437 	__u32 clear_exception;
1438 };
1439 
1440 /**
1441  * kfd_ioctl_dbg_trap_get_queue_snapshot_args
1442  *
1443  *     Arguments KFD_IOC_DBG_TRAP_GET_QUEUE_SNAPSHOT
1444  *     Get queue information.
1445  *
1446  *     @exception_mask	 (IN)	  - exceptions raised to clear
1447  *     @snapshot_buf_ptr (IN)	  - queue snapshot entry buffer (see kfd_queue_snapshot_entry)
1448  *     @num_queues	 (IN/OUT) - number of queue snapshot entries
1449  *         The debugger specifies the size of the array allocated in @num_queues.
1450  *         KFD returns the number of queues that actually existed. If this is
1451  *         larger than the size specified by the debugger, KFD will not overflow
1452  *         the array allocated by the debugger.
1453  *
1454  *     @entry_size	 (IN/OUT) - size per entry in bytes
1455  *         The debugger specifies sizeof(struct kfd_queue_snapshot_entry) in
1456  *         @entry_size. KFD returns the number of bytes actually populated per
1457  *         entry. The debugger should use the KFD_IOCTL_MINOR_VERSION to determine,
1458  *         which fields in struct kfd_queue_snapshot_entry are valid. This allows
1459  *         growing the ABI in a backwards compatible manner.
1460  *         Note that entry_size(IN) should still be used to stride the snapshot buffer in the
1461  *         event that it's larger than actual kfd_queue_snapshot_entry.
1462  *
1463  *     Generic errors apply (see kfd_dbg_trap_operations).
1464  *     Return - 0 on SUCCESS.
1465  *              Copies @num_queues(IN) queue snapshot entries of size @entry_size(IN)
1466  *              into @snapshot_buf_ptr if @num_queues(IN) > 0.
1467  *              Otherwise return @num_queues(OUT) queue snapshot entries that exist.
1468  */
1469 struct kfd_ioctl_dbg_trap_queue_snapshot_args {
1470 	__u64 exception_mask;
1471 	__u64 snapshot_buf_ptr;
1472 	__u32 num_queues;
1473 	__u32 entry_size;
1474 };
1475 
1476 /**
1477  * kfd_ioctl_dbg_trap_get_device_snapshot_args
1478  *
1479  *     Arguments for KFD_IOC_DBG_TRAP_GET_DEVICE_SNAPSHOT
1480  *     Get device information.
1481  *
1482  *     @exception_mask	 (IN)	  - exceptions raised to clear
1483  *     @snapshot_buf_ptr (IN)	  - pointer to snapshot buffer (see kfd_dbg_device_info_entry)
1484  *     @num_devices	 (IN/OUT) - number of debug devices to snapshot
1485  *         The debugger specifies the size of the array allocated in @num_devices.
1486  *         KFD returns the number of devices that actually existed. If this is
1487  *         larger than the size specified by the debugger, KFD will not overflow
1488  *         the array allocated by the debugger.
1489  *
1490  *     @entry_size	 (IN/OUT) - size per entry in bytes
1491  *         The debugger specifies sizeof(struct kfd_dbg_device_info_entry) in
1492  *         @entry_size. KFD returns the number of bytes actually populated. The
1493  *         debugger should use KFD_IOCTL_MINOR_VERSION to determine, which fields
1494  *         in struct kfd_dbg_device_info_entry are valid. This allows growing the
1495  *         ABI in a backwards compatible manner.
1496  *         Note that entry_size(IN) should still be used to stride the snapshot buffer in the
1497  *         event that it's larger than actual kfd_dbg_device_info_entry.
1498  *
1499  *     Generic errors apply (see kfd_dbg_trap_operations).
1500  *     Return - 0 on SUCCESS.
1501  *              Copies @num_devices(IN) device snapshot entries of size @entry_size(IN)
1502  *              into @snapshot_buf_ptr if @num_devices(IN) > 0.
1503  *              Otherwise return @num_devices(OUT) queue snapshot entries that exist.
1504  */
1505 struct kfd_ioctl_dbg_trap_device_snapshot_args {
1506 	__u64 exception_mask;
1507 	__u64 snapshot_buf_ptr;
1508 	__u32 num_devices;
1509 	__u32 entry_size;
1510 };
1511 
1512 /**
1513  * kfd_ioctl_dbg_trap_args
1514  *
1515  * Arguments to debug target process.
1516  *
1517  *     @pid - target process to debug
1518  *     @op  - debug operation (see kfd_dbg_trap_operations)
1519  *
1520  *     @op determines which union struct args to use.
1521  *     Refer to kern docs for each kfd_ioctl_dbg_trap_*_args struct.
1522  */
1523 struct kfd_ioctl_dbg_trap_args {
1524 	__u32 pid;
1525 	__u32 op;
1526 
1527 	union {
1528 		struct kfd_ioctl_dbg_trap_enable_args enable;
1529 		struct kfd_ioctl_dbg_trap_send_runtime_event_args send_runtime_event;
1530 		struct kfd_ioctl_dbg_trap_set_exceptions_enabled_args set_exceptions_enabled;
1531 		struct kfd_ioctl_dbg_trap_set_wave_launch_override_args launch_override;
1532 		struct kfd_ioctl_dbg_trap_set_wave_launch_mode_args launch_mode;
1533 		struct kfd_ioctl_dbg_trap_suspend_queues_args suspend_queues;
1534 		struct kfd_ioctl_dbg_trap_resume_queues_args resume_queues;
1535 		struct kfd_ioctl_dbg_trap_set_node_address_watch_args set_node_address_watch;
1536 		struct kfd_ioctl_dbg_trap_clear_node_address_watch_args clear_node_address_watch;
1537 		struct kfd_ioctl_dbg_trap_set_flags_args set_flags;
1538 		struct kfd_ioctl_dbg_trap_query_debug_event_args query_debug_event;
1539 		struct kfd_ioctl_dbg_trap_query_exception_info_args query_exception_info;
1540 		struct kfd_ioctl_dbg_trap_queue_snapshot_args queue_snapshot;
1541 		struct kfd_ioctl_dbg_trap_device_snapshot_args device_snapshot;
1542 	};
1543 };
1544 
1545 #define AMDKFD_IOCTL_BASE 'K'
1546 #define AMDKFD_IO(nr)			_IO(AMDKFD_IOCTL_BASE, nr)
1547 #define AMDKFD_IOR(nr, type)		_IOR(AMDKFD_IOCTL_BASE, nr, type)
1548 #define AMDKFD_IOW(nr, type)		_IOW(AMDKFD_IOCTL_BASE, nr, type)
1549 #define AMDKFD_IOWR(nr, type)		_IOWR(AMDKFD_IOCTL_BASE, nr, type)
1550 
1551 #define AMDKFD_IOC_GET_VERSION			\
1552 		AMDKFD_IOR(0x01, struct kfd_ioctl_get_version_args)
1553 
1554 #define AMDKFD_IOC_CREATE_QUEUE			\
1555 		AMDKFD_IOWR(0x02, struct kfd_ioctl_create_queue_args)
1556 
1557 #define AMDKFD_IOC_DESTROY_QUEUE		\
1558 		AMDKFD_IOWR(0x03, struct kfd_ioctl_destroy_queue_args)
1559 
1560 #define AMDKFD_IOC_SET_MEMORY_POLICY		\
1561 		AMDKFD_IOW(0x04, struct kfd_ioctl_set_memory_policy_args)
1562 
1563 #define AMDKFD_IOC_GET_CLOCK_COUNTERS		\
1564 		AMDKFD_IOWR(0x05, struct kfd_ioctl_get_clock_counters_args)
1565 
1566 #define AMDKFD_IOC_GET_PROCESS_APERTURES	\
1567 		AMDKFD_IOR(0x06, struct kfd_ioctl_get_process_apertures_args)
1568 
1569 #define AMDKFD_IOC_UPDATE_QUEUE			\
1570 		AMDKFD_IOW(0x07, struct kfd_ioctl_update_queue_args)
1571 
1572 #define AMDKFD_IOC_CREATE_EVENT			\
1573 		AMDKFD_IOWR(0x08, struct kfd_ioctl_create_event_args)
1574 
1575 #define AMDKFD_IOC_DESTROY_EVENT		\
1576 		AMDKFD_IOW(0x09, struct kfd_ioctl_destroy_event_args)
1577 
1578 #define AMDKFD_IOC_SET_EVENT			\
1579 		AMDKFD_IOW(0x0A, struct kfd_ioctl_set_event_args)
1580 
1581 #define AMDKFD_IOC_RESET_EVENT			\
1582 		AMDKFD_IOW(0x0B, struct kfd_ioctl_reset_event_args)
1583 
1584 #define AMDKFD_IOC_WAIT_EVENTS			\
1585 		AMDKFD_IOWR(0x0C, struct kfd_ioctl_wait_events_args)
1586 
1587 #define AMDKFD_IOC_DBG_REGISTER_DEPRECATED	\
1588 		AMDKFD_IOW(0x0D, struct kfd_ioctl_dbg_register_args)
1589 
1590 #define AMDKFD_IOC_DBG_UNREGISTER_DEPRECATED	\
1591 		AMDKFD_IOW(0x0E, struct kfd_ioctl_dbg_unregister_args)
1592 
1593 #define AMDKFD_IOC_DBG_ADDRESS_WATCH_DEPRECATED	\
1594 		AMDKFD_IOW(0x0F, struct kfd_ioctl_dbg_address_watch_args)
1595 
1596 #define AMDKFD_IOC_DBG_WAVE_CONTROL_DEPRECATED	\
1597 		AMDKFD_IOW(0x10, struct kfd_ioctl_dbg_wave_control_args)
1598 
1599 #define AMDKFD_IOC_SET_SCRATCH_BACKING_VA	\
1600 		AMDKFD_IOWR(0x11, struct kfd_ioctl_set_scratch_backing_va_args)
1601 
1602 #define AMDKFD_IOC_GET_TILE_CONFIG                                      \
1603 		AMDKFD_IOWR(0x12, struct kfd_ioctl_get_tile_config_args)
1604 
1605 #define AMDKFD_IOC_SET_TRAP_HANDLER		\
1606 		AMDKFD_IOW(0x13, struct kfd_ioctl_set_trap_handler_args)
1607 
1608 #define AMDKFD_IOC_GET_PROCESS_APERTURES_NEW	\
1609 		AMDKFD_IOWR(0x14,		\
1610 			struct kfd_ioctl_get_process_apertures_new_args)
1611 
1612 #define AMDKFD_IOC_ACQUIRE_VM			\
1613 		AMDKFD_IOW(0x15, struct kfd_ioctl_acquire_vm_args)
1614 
1615 #define AMDKFD_IOC_ALLOC_MEMORY_OF_GPU		\
1616 		AMDKFD_IOWR(0x16, struct kfd_ioctl_alloc_memory_of_gpu_args)
1617 
1618 #define AMDKFD_IOC_FREE_MEMORY_OF_GPU		\
1619 		AMDKFD_IOW(0x17, struct kfd_ioctl_free_memory_of_gpu_args)
1620 
1621 #define AMDKFD_IOC_MAP_MEMORY_TO_GPU		\
1622 		AMDKFD_IOWR(0x18, struct kfd_ioctl_map_memory_to_gpu_args)
1623 
1624 #define AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU	\
1625 		AMDKFD_IOWR(0x19, struct kfd_ioctl_unmap_memory_from_gpu_args)
1626 
1627 #define AMDKFD_IOC_SET_CU_MASK		\
1628 		AMDKFD_IOW(0x1A, struct kfd_ioctl_set_cu_mask_args)
1629 
1630 #define AMDKFD_IOC_GET_QUEUE_WAVE_STATE		\
1631 		AMDKFD_IOWR(0x1B, struct kfd_ioctl_get_queue_wave_state_args)
1632 
1633 #define AMDKFD_IOC_GET_DMABUF_INFO		\
1634 		AMDKFD_IOWR(0x1C, struct kfd_ioctl_get_dmabuf_info_args)
1635 
1636 #define AMDKFD_IOC_IMPORT_DMABUF		\
1637 		AMDKFD_IOWR(0x1D, struct kfd_ioctl_import_dmabuf_args)
1638 
1639 #define AMDKFD_IOC_ALLOC_QUEUE_GWS		\
1640 		AMDKFD_IOWR(0x1E, struct kfd_ioctl_alloc_queue_gws_args)
1641 
1642 #define AMDKFD_IOC_SMI_EVENTS			\
1643 		AMDKFD_IOWR(0x1F, struct kfd_ioctl_smi_events_args)
1644 
1645 #define AMDKFD_IOC_SVM	AMDKFD_IOWR(0x20, struct kfd_ioctl_svm_args)
1646 
1647 #define AMDKFD_IOC_SET_XNACK_MODE		\
1648 		AMDKFD_IOWR(0x21, struct kfd_ioctl_set_xnack_mode_args)
1649 
1650 #define AMDKFD_IOC_CRIU_OP			\
1651 		AMDKFD_IOWR(0x22, struct kfd_ioctl_criu_args)
1652 
1653 #define AMDKFD_IOC_AVAILABLE_MEMORY		\
1654 		AMDKFD_IOWR(0x23, struct kfd_ioctl_get_available_memory_args)
1655 
1656 #define AMDKFD_IOC_EXPORT_DMABUF		\
1657 		AMDKFD_IOWR(0x24, struct kfd_ioctl_export_dmabuf_args)
1658 
1659 #define AMDKFD_IOC_RUNTIME_ENABLE		\
1660 		AMDKFD_IOWR(0x25, struct kfd_ioctl_runtime_enable_args)
1661 
1662 #define AMDKFD_IOC_DBG_TRAP			\
1663 		AMDKFD_IOWR(0x26, struct kfd_ioctl_dbg_trap_args)
1664 
1665 #define AMDKFD_COMMAND_START		0x01
1666 #define AMDKFD_COMMAND_END		0x27
1667 
1668 #endif
1669