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