kfd_priv.h 41.6 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
O
Oded Gabbay 已提交
2
/*
3
 * Copyright 2014-2022 Advanced Micro Devices, Inc.
O
Oded Gabbay 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef KFD_PRIV_H_INCLUDED
#define KFD_PRIV_H_INCLUDED

#include <linux/hashtable.h>
#include <linux/mmu_notifier.h>
29
#include <linux/memremap.h>
O
Oded Gabbay 已提交
30 31 32 33 34
#include <linux/mutex.h>
#include <linux/types.h>
#include <linux/atomic.h>
#include <linux/workqueue.h>
#include <linux/spinlock.h>
35
#include <linux/kfd_ioctl.h>
36
#include <linux/idr.h>
37
#include <linux/kfifo.h>
38
#include <linux/seq_file.h>
39
#include <linux/kref.h>
40
#include <linux/sysfs.h>
41
#include <linux/device_cgroup.h>
42 43 44
#include <drm/drm_file.h>
#include <drm/drm_drv.h>
#include <drm/drm_device.h>
45
#include <drm/drm_ioctl.h>
O
Oded Gabbay 已提交
46
#include <kgd_kfd_interface.h>
47
#include <linux/swap.h>
O
Oded Gabbay 已提交
48

49
#include "amd_shared.h"
50
#include "amdgpu.h"
51

L
Laura Abbott 已提交
52 53
#define KFD_MAX_RING_ENTRY_SIZE	8

54 55
#define KFD_SYSFS_FILE_MODE 0444

56 57 58 59 60 61 62 63 64 65 66
/* GPU ID hash width in bits */
#define KFD_GPU_ID_HASH_WIDTH 16

/* Use upper bits of mmap offset to store KFD driver specific information.
 * BITS[63:62] - Encode MMAP type
 * BITS[61:46] - Encode gpu_id. To identify to which GPU the offset belongs to
 * BITS[45:0]  - MMAP offset value
 *
 * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
 *  defines are w.r.t to PAGE_SIZE
 */
67
#define KFD_MMAP_TYPE_SHIFT	62
68 69 70 71
#define KFD_MMAP_TYPE_MASK	(0x3ULL << KFD_MMAP_TYPE_SHIFT)
#define KFD_MMAP_TYPE_DOORBELL	(0x3ULL << KFD_MMAP_TYPE_SHIFT)
#define KFD_MMAP_TYPE_EVENTS	(0x2ULL << KFD_MMAP_TYPE_SHIFT)
#define KFD_MMAP_TYPE_RESERVED_MEM	(0x1ULL << KFD_MMAP_TYPE_SHIFT)
O
Oak Zeng 已提交
72
#define KFD_MMAP_TYPE_MMIO	(0x0ULL << KFD_MMAP_TYPE_SHIFT)
73

74
#define KFD_MMAP_GPU_ID_SHIFT 46
75 76 77 78
#define KFD_MMAP_GPU_ID_MASK (((1ULL << KFD_GPU_ID_HASH_WIDTH) - 1) \
				<< KFD_MMAP_GPU_ID_SHIFT)
#define KFD_MMAP_GPU_ID(gpu_id) ((((uint64_t)gpu_id) << KFD_MMAP_GPU_ID_SHIFT)\
				& KFD_MMAP_GPU_ID_MASK)
79
#define KFD_MMAP_GET_GPU_ID(offset)    ((offset & KFD_MMAP_GPU_ID_MASK) \
80 81
				>> KFD_MMAP_GPU_ID_SHIFT)

B
Ben Goz 已提交
82 83
/*
 * When working with cp scheduler we should assign the HIQ manually or via
84
 * the amdgpu driver to a fixed hqd slot, here are the fixed HIQ hqd slot
B
Ben Goz 已提交
85 86 87 88 89 90 91
 * definitions for Kaveri. In Kaveri only the first ME queues participates
 * in the cp scheduling taking that in mind we set the HIQ slot in the
 * second ME.
 */
#define KFD_CIK_HIQ_PIPE 4
#define KFD_CIK_HIQ_QUEUE 0

92 93 94 95
/* Macro for allocating structures */
#define kfd_alloc_struct(ptr_to_struct)	\
	((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))

96
#define KFD_MAX_NUM_OF_PROCESSES 512
97
#define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
98

F
Felix Kuehling 已提交
99 100 101 102
/*
 * Size of the per-process TBA+TMA buffer: 2 pages
 *
 * The first page is the TBA used for the CWSR ISA code. The second
103
 * page is used as TMA for user-mode trap handler setup in daisy-chain mode.
F
Felix Kuehling 已提交
104 105 106 107
 */
#define KFD_CWSR_TBA_TMA_SIZE (PAGE_SIZE * 2)
#define KFD_CWSR_TMA_OFFSET PAGE_SIZE

108 109 110 111 112 113
#define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE		\
	(KFD_MAX_NUM_OF_PROCESSES *			\
			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)

#define KFD_KERNEL_QUEUE_SIZE 2048

114 115
#define KFD_UNMAP_LATENCY_MS	(4000)

116 117 118 119 120 121 122 123 124 125
/*
 * 512 = 0x200
 * The doorbell index distance between SDMA RLC (2*i) and (2*i+1) in the
 * same SDMA engine on SOC15, which has 8-byte doorbells for SDMA.
 * 512 8-byte doorbell distance (i.e. one page away) ensures that SDMA RLC
 * (2*i+1) doorbells (in terms of the lower 12 bit address) lie exactly in
 * the OFFSET and SIZE set in registers like BIF_SDMA0_DOORBELL_RANGE.
 */
#define KFD_QUEUE_DOORBELL_MIRROR_OFFSET 512

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/**
 * enum kfd_ioctl_flags - KFD ioctl flags
 * Various flags that can be set in &amdkfd_ioctl_desc.flags to control how
 * userspace can use a given ioctl.
 */
enum kfd_ioctl_flags {
	/*
	 * @KFD_IOC_FLAG_CHECKPOINT_RESTORE:
	 * Certain KFD ioctls such as AMDKFD_IOC_CRIU_OP can potentially
	 * perform privileged operations and load arbitrary data into MQDs and
	 * eventually HQD registers when the queue is mapped by HWS. In order to
	 * prevent this we should perform additional security checks.
	 *
	 * This is equivalent to callers with the CHECKPOINT_RESTORE capability.
	 *
	 * Note: Since earlier versions of docker do not support CHECKPOINT_RESTORE,
	 * we also allow ioctls with SYS_ADMIN capability.
	 */
	KFD_IOC_FLAG_CHECKPOINT_RESTORE = BIT(0),
};
146
/*
147 148
 * Kernel module parameter to specify maximum number of supported queues per
 * device
149
 */
150
extern int max_num_of_queues_per_device;
151

B
Ben Goz 已提交
152

153 154 155
/* Kernel module parameter to specify the scheduling policy */
extern int sched_policy;

156 157 158 159 160 161
/*
 * Kernel module parameter to specify the maximum process
 * number per HW scheduler
 */
extern int hws_max_conc_proc;

F
Felix Kuehling 已提交
162 163
extern int cwsr_enable;

164 165 166 167 168 169
/*
 * Kernel module parameter to specify whether to send sigterm to HSA process on
 * unhandled exception
 */
extern int send_sigterm;

170 171 172 173 174 175
/*
 * This kernel module is used to simulate large bar machine on non-large bar
 * enabled machines.
 */
extern int debug_largebar;

176 177 178 179 180 181
/*
 * Ignore CRAT table during KFD initialization, can be used to work around
 * broken CRAT tables on some AMD systems
 */
extern int ignore_crat;

182
/* Set sh_mem_config.retry_disable on GFX v9 */
183
extern int amdgpu_noretry;
184

185
/* Halt if HWS hang is detected */
186 187
extern int halt_if_hws_hang;

188
/* Whether MEC FW support GWS barriers */
189 190
extern bool hws_gws_support;

191
/* Queue preemption timeout in ms */
192 193
extern int queue_preemption_timeout_ms;

194 195 196 197 198
/*
 * Don't evict process queues on vm fault
 */
extern int amdgpu_no_queue_eviction_on_vm_fault;

199
/* Enable eviction debug messages */
200 201
extern bool debug_evictions;

B
Ben Goz 已提交
202 203 204 205 206
enum cache_policy {
	cache_policy_coherent,
	cache_policy_noncoherent
};

207 208
#define KFD_GC_VERSION(dev) ((dev)->adev->ip_versions[GC_HWIP][0])
#define KFD_IS_SOC15(dev)   ((KFD_GC_VERSION(dev)) >= (IP_VERSION(9, 0, 1)))
209

210 211
struct kfd_event_interrupt_class {
	bool (*interrupt_isr)(struct kfd_dev *dev,
212 213
			const uint32_t *ih_ring_entry, uint32_t *patched_ihre,
			bool *patched_flag);
214
	void (*interrupt_wq)(struct kfd_dev *dev,
215
			const uint32_t *ih_ring_entry);
216 217
};

O
Oded Gabbay 已提交
218
struct kfd_device_info {
219
	uint32_t gfx_target_version;
220
	const struct kfd_event_interrupt_class *event_interrupt_class;
O
Oded Gabbay 已提交
221
	unsigned int max_pasid_bits;
222
	unsigned int max_no_of_hqd;
223
	unsigned int doorbell_size;
O
Oded Gabbay 已提交
224
	size_t ih_ring_entry_size;
225
	uint8_t num_of_watch_points;
226
	uint16_t mqd_size_aligned;
F
Felix Kuehling 已提交
227
	bool supports_cwsr;
228
	bool needs_iommu_device;
229
	bool needs_pci_atomics;
230
	uint32_t no_atomic_fw_version;
231
	unsigned int num_sdma_queues_per_engine;
232 233
	unsigned int num_reserved_sdma_queues_per_engine;
	uint64_t reserved_sdma_queues_bitmap;
O
Oded Gabbay 已提交
234 235
};

236 237 238
unsigned int kfd_get_num_sdma_engines(struct kfd_dev *kdev);
unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_dev *kdev);

239 240 241 242 243
struct kfd_mem_obj {
	uint32_t range_start;
	uint32_t range_end;
	uint64_t gpu_addr;
	uint32_t *cpu_ptr;
244
	void *gtt_mem;
245 246
};

247 248 249 250 251 252
struct kfd_vmid_info {
	uint32_t first_vmid_kfd;
	uint32_t last_vmid_kfd;
	uint32_t vmid_num_kfd;
};

O
Oded Gabbay 已提交
253
struct kfd_dev {
254
	struct amdgpu_device *adev;
O
Oded Gabbay 已提交
255

256
	struct kfd_device_info device_info;
O
Oded Gabbay 已提交
257
	struct pci_dev *pdev;
258
	struct drm_device *ddev;
O
Oded Gabbay 已提交
259 260 261

	unsigned int id;		/* topology stub index */

262 263 264 265
	phys_addr_t doorbell_base;	/* Start of actual doorbells used by
					 * KFD. It is aligned for mapping
					 * into user mode
					 */
266 267 268 269
	size_t doorbell_base_dw_offset;	/* Offset from the start of the PCI
					 * doorbell BAR to the first KFD
					 * doorbell in dwords. GFX reserves
					 * the segment before this offset.
270 271 272 273 274
					 */
	u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
					   * page used by kernel queue
					   */

O
Oded Gabbay 已提交
275
	struct kgd2kfd_shared_resources shared_resources;
276
	struct kfd_vmid_info vm_info;
277
	struct kfd_local_mem_info local_mem_info;
O
Oded Gabbay 已提交
278

279 280
	const struct kfd2kgd_calls *kfd2kgd;
	struct mutex doorbell_mutex;
J
Joe Perches 已提交
281 282
	DECLARE_BITMAP(doorbell_available_index,
			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
283

284 285 286 287 288 289 290 291
	void *gtt_mem;
	uint64_t gtt_start_gpu_addr;
	void *gtt_start_cpu_ptr;
	void *gtt_sa_bitmap;
	struct mutex gtt_sa_lock;
	unsigned int gtt_sa_chunk_size;
	unsigned int gtt_sa_num_of_chunks;

292
	/* Interrupts */
293
	struct kfifo ih_fifo;
294
	struct workqueue_struct *ih_wq;
295 296 297
	struct work_struct interrupt_work;
	spinlock_t interrupt_lock;

B
Ben Goz 已提交
298 299
	/* QCM Device instance */
	struct device_queue_manager *dqm;
O
Oded Gabbay 已提交
300

B
Ben Goz 已提交
301
	bool init_complete;
302 303 304 305 306
	/*
	 * Interrupts of interest to KFD are copied
	 * from the HW ring into a SW ring.
	 */
	bool interrupts_active;
307

308 309
	/* Firmware versions */
	uint16_t mec_fw_version;
310
	uint16_t mec2_fw_version;
311 312
	uint16_t sdma_fw_version;

313 314 315
	/* Maximum process number mapped to HW scheduler */
	unsigned int max_proc_per_quantum;

F
Felix Kuehling 已提交
316 317 318 319
	/* CWSR */
	bool cwsr_enabled;
	const void *cwsr_isa;
	unsigned int cwsr_isa_size;
320 321 322

	/* xGMI */
	uint64_t hive_id;
323

324
	bool pci_atomic_requested;
325

326 327 328
	/* Use IOMMU v2 flag */
	bool use_iommu_v2;

329 330
	/* SRAM ECC flag */
	atomic_t sram_ecc_flag;
331 332 333

	/* Compute Profile ref. count */
	atomic_t compute_profile;
334

335
	/* Global GWS resource shared between processes */
336
	void *gws;
A
Amber Lin 已提交
337 338 339 340

	/* Clients watching SMI events */
	struct list_head smi_clients;
	spinlock_t smi_lock;
341 342

	uint32_t reset_seq_num;
343 344 345

	struct ida doorbell_ida;
	unsigned int max_doorbell_slices;
346 347

	int noretry;
348 349 350

	/* HMM page migration MEMORY_DEVICE_PRIVATE mapping */
	struct dev_pagemap pgmap;
O
Oded Gabbay 已提交
351 352
};

353 354 355 356 357 358
enum kfd_mempool {
	KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
	KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
	KFD_MEMPOOL_FRAMEBUFFER = 3,
};

O
Oded Gabbay 已提交
359 360 361 362
/* Character device interface */
int kfd_chardev_init(void);
void kfd_chardev_exit(void);

B
Ben Goz 已提交
363
/**
364
 * enum kfd_unmap_queues_filter - Enum for queue filters.
B
Ben Goz 已提交
365
 *
366
 * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the
B
Ben Goz 已提交
367 368
 *						running queues list.
 *
369 370 371
 * @KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES: Preempts all non-static queues
 *						in the run list.
 *
372
 * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to
B
Ben Goz 已提交
373 374 375
 *						specific process.
 *
 */
376
enum kfd_unmap_queues_filter {
377 378 379
	KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES = 1,
	KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES = 2,
	KFD_UNMAP_QUEUES_FILTER_BY_PASID = 3
B
Ben Goz 已提交
380
};
381

B
Ben Goz 已提交
382
/**
383
 * enum kfd_queue_type - Enum for various queue types.
B
Ben Goz 已提交
384 385 386
 *
 * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
 *
387
 * @KFD_QUEUE_TYPE_SDMA: SDMA user mode queue type.
B
Ben Goz 已提交
388 389 390 391
 *
 * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
 *
 * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
392 393
 *
 * @KFD_QUEUE_TYPE_SDMA_XGMI: Special SDMA queue for XGMI interface.
B
Ben Goz 已提交
394 395 396 397 398
 */
enum kfd_queue_type  {
	KFD_QUEUE_TYPE_COMPUTE,
	KFD_QUEUE_TYPE_SDMA,
	KFD_QUEUE_TYPE_HIQ,
399 400
	KFD_QUEUE_TYPE_DIQ,
	KFD_QUEUE_TYPE_SDMA_XGMI
B
Ben Goz 已提交
401 402
};

B
Ben Goz 已提交
403 404 405 406 407
enum kfd_queue_format {
	KFD_QUEUE_FORMAT_PM4,
	KFD_QUEUE_FORMAT_AQL
};

408 409 410 411 412
enum KFD_QUEUE_PRIORITY {
	KFD_QUEUE_PRIORITY_MINIMUM = 0,
	KFD_QUEUE_PRIORITY_MAXIMUM = 15
};

B
Ben Goz 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
/**
 * struct queue_properties
 *
 * @type: The queue type.
 *
 * @queue_id: Queue identifier.
 *
 * @queue_address: Queue ring buffer address.
 *
 * @queue_size: Queue ring buffer size.
 *
 * @priority: Defines the queue priority relative to other queues in the
 * process.
 * This is just an indication and HW scheduling may override the priority as
 * necessary while keeping the relative prioritization.
 * the priority granularity is from 0 to f which f is the highest priority.
 * currently all queues are initialized with the highest priority.
 *
 * @queue_percent: This field is partially implemented and currently a zero in
 * this field defines that the queue is non active.
 *
 * @read_ptr: User space address which points to the number of dwords the
 * cp read from the ring buffer. This field updates automatically by the H/W.
 *
 * @write_ptr: Defines the number of dwords written to the ring buffer.
 *
439 440 441
 * @doorbell_ptr: Notifies the H/W of new packet written to the queue ring
 * buffer. This field should be similar to write_ptr and the user should
 * update this field after updating the write_ptr.
B
Ben Goz 已提交
442 443 444
 *
 * @doorbell_off: The doorbell offset in the doorbell pci-bar.
 *
445 446
 * @is_interop: Defines if this is a interop queue. Interop queue means that
 * the queue can access both graphics and compute resources.
B
Ben Goz 已提交
447
 *
448 449 450 451 452
 * @is_evicted: Defines if the queue is evicted. Only active queues
 * are evicted, rendering them inactive.
 *
 * @is_active: Defines if the queue is active or not. @is_active and
 * @is_evicted are protected by the DQM lock.
B
Ben Goz 已提交
453
 *
454 455 456 457
 * @is_gws: Defines if the queue has been updated to be GWS-capable or not.
 * @is_gws should be protected by the DQM lock, since changing it can yield the
 * possibility of updating DQM state on number of GWS queues.
 *
B
Ben Goz 已提交
458 459 460 461 462 463 464
 * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
 * of the queue.
 *
 * This structure represents the queue properties for each queue no matter if
 * it's user mode or kernel mode queue.
 *
 */
465

B
Ben Goz 已提交
466 467
struct queue_properties {
	enum kfd_queue_type type;
B
Ben Goz 已提交
468
	enum kfd_queue_format format;
B
Ben Goz 已提交
469 470 471 472 473 474 475
	unsigned int queue_id;
	uint64_t queue_address;
	uint64_t  queue_size;
	uint32_t priority;
	uint32_t queue_percent;
	uint32_t *read_ptr;
	uint32_t *write_ptr;
476
	void __iomem *doorbell_ptr;
B
Ben Goz 已提交
477 478
	uint32_t doorbell_off;
	bool is_interop;
479
	bool is_evicted;
B
Ben Goz 已提交
480
	bool is_active;
481
	bool is_gws;
B
Ben Goz 已提交
482 483
	/* Not relevant for user mode queues in cp scheduling */
	unsigned int vmid;
B
Ben Goz 已提交
484 485 486 487
	/* Relevant only for sdma queues*/
	uint32_t sdma_engine_id;
	uint32_t sdma_queue_id;
	uint32_t sdma_vm_addr;
488 489 490 491 492
	/* Relevant only for VI */
	uint64_t eop_ring_buffer_address;
	uint32_t eop_ring_buffer_size;
	uint64_t ctx_save_restore_area_address;
	uint32_t ctx_save_restore_area_size;
F
Felix Kuehling 已提交
493 494 495
	uint32_t ctl_stack_size;
	uint64_t tba_addr;
	uint64_t tma_addr;
B
Ben Goz 已提交
496 497
};

498 499 500 501 502
#define QUEUE_IS_ACTIVE(q) ((q).queue_size > 0 &&	\
			    (q).queue_address != 0 &&	\
			    (q).queue_percent > 0 &&	\
			    !(q).is_evicted)

503 504 505 506 507 508 509 510 511 512 513 514 515
enum mqd_update_flag {
	UPDATE_FLAG_CU_MASK = 0,
};

struct mqd_update_info {
	union {
		struct {
			uint32_t count; /* Must be a multiple of 32 */
			uint32_t *ptr;
		} cu_mask;
	};
	enum mqd_update_flag update_flag;
};
516

B
Ben Goz 已提交
517 518 519 520 521
/**
 * struct queue
 *
 * @list: Queue linked list.
 *
522
 * @mqd: The queue MQD (memory queue descriptor).
B
Ben Goz 已提交
523 524 525 526 527 528 529 530
 *
 * @mqd_mem_obj: The MQD local gpu memory object.
 *
 * @gart_mqd_addr: The MQD gart mc address.
 *
 * @properties: The queue properties.
 *
 * @mec: Used only in no cp scheduling mode and identifies to micro engine id
531
 *	 that the queue should be executed on.
B
Ben Goz 已提交
532
 *
533 534
 * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
 *	  id.
B
Ben Goz 已提交
535 536 537 538 539 540 541
 *
 * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
 *
 * @process: The kfd process that created this queue.
 *
 * @device: The kfd device that created this queue.
 *
542 543 544
 * @gws: Pointing to gws kgd_mem if this is a gws control queue; NULL
 * otherwise.
 *
B
Ben Goz 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
 * This structure represents user mode compute queues.
 * It contains all the necessary data to handle such queues.
 *
 */

struct queue {
	struct list_head list;
	void *mqd;
	struct kfd_mem_obj *mqd_mem_obj;
	uint64_t gart_mqd_addr;
	struct queue_properties properties;

	uint32_t mec;
	uint32_t pipe;
	uint32_t queue;

B
Ben Goz 已提交
561
	unsigned int sdma_id;
562
	unsigned int doorbell_id;
B
Ben Goz 已提交
563

B
Ben Goz 已提交
564 565
	struct kfd_process	*process;
	struct kfd_dev		*device;
566
	void *gws;
567 568 569

	/* procfs */
	struct kobject kobj;
570 571 572 573

	void *gang_ctx_bo;
	uint64_t gang_ctx_gpu_addr;
	void *gang_ctx_cpu_ptr;
574 575

	struct amdgpu_bo *wptr_bo;
B
Ben Goz 已提交
576 577
};

B
Ben Goz 已提交
578
enum KFD_MQD_TYPE {
579
	KFD_MQD_TYPE_HIQ = 0,		/* for hiq */
580 581
	KFD_MQD_TYPE_CP,		/* for cp queues and diq */
	KFD_MQD_TYPE_SDMA,		/* for sdma queues */
582
	KFD_MQD_TYPE_DIQ,		/* for diq */
B
Ben Goz 已提交
583 584 585
	KFD_MQD_TYPE_MAX
};

586 587 588 589 590 591
enum KFD_PIPE_PRIORITY {
	KFD_PIPE_PRIORITY_CS_LOW = 0,
	KFD_PIPE_PRIORITY_CS_MEDIUM,
	KFD_PIPE_PRIORITY_CS_HIGH
};

B
Ben Goz 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
struct scheduling_resources {
	unsigned int vmid_mask;
	enum kfd_queue_type type;
	uint64_t queue_mask;
	uint64_t gws_mask;
	uint32_t oac_mask;
	uint32_t gds_heap_base;
	uint32_t gds_heap_size;
};

struct process_queue_manager {
	/* data */
	struct kfd_process	*process;
	struct list_head	queues;
	unsigned long		*queue_slot_bitmap;
};

struct qcm_process_device {
	/* The Device Queue Manager that owns this data */
	struct device_queue_manager *dqm;
	struct process_queue_manager *pqm;
	/* Queues list */
	struct list_head queues_list;
	struct list_head priv_queue_list;

	unsigned int queue_count;
	unsigned int vmid;
	bool is_debug;
620
	unsigned int evicted; /* eviction counter, 0=active */
621 622 623 624 625 626

	/* This flag tells if we should reset all wavefronts on
	 * process termination
	 */
	bool reset_wavefronts;

627 628 629 630 631 632 633 634
	/* This flag tells us if this process has a GWS-capable
	 * queue that will be mapped into the runlist. It's
	 * possible to request a GWS BO, but not have the queue
	 * currently mapped, and this changes how the MAP_PROCESS
	 * PM4 packet is configured.
	 */
	bool mapped_gws_queue;

635
	/* All the memory management data should be here too */
B
Ben Goz 已提交
636
	uint64_t gds_context_area;
637
	/* Contains page table flags such as AMDGPU_PTE_VALID since gfx9 */
638
	uint64_t page_table_base;
B
Ben Goz 已提交
639 640 641 642 643 644 645
	uint32_t sh_mem_config;
	uint32_t sh_mem_bases;
	uint32_t sh_mem_ape1_base;
	uint32_t sh_mem_ape1_limit;
	uint32_t gds_size;
	uint32_t num_gws;
	uint32_t num_oac;
646
	uint32_t sh_hidden_private_base;
F
Felix Kuehling 已提交
647 648

	/* CWSR memory */
649
	struct kgd_mem *cwsr_mem;
F
Felix Kuehling 已提交
650
	void *cwsr_kaddr;
651
	uint64_t cwsr_base;
F
Felix Kuehling 已提交
652 653
	uint64_t tba_addr;
	uint64_t tma_addr;
654 655

	/* IB memory */
656
	struct kgd_mem *ib_mem;
657
	uint64_t ib_base;
658
	void *ib_kaddr;
659 660 661

	/* doorbell resources per process per device */
	unsigned long *doorbell_bitmap;
B
Ben Goz 已提交
662 663
};

664 665 666 667 668 669 670 671 672
/* KFD Memory Eviction */

/* Approx. wait time before attempting to restore evicted BOs */
#define PROCESS_RESTORE_TIME_MS 100
/* Approx. back off time if restore fails due to lack of memory */
#define PROCESS_BACK_OFF_TIME_MS 100
/* Approx. time before evicting the process again */
#define PROCESS_ACTIVE_TIME_MS 10

673 674 675 676 677 678 679 680
/* 8 byte handle containing GPU ID in the most significant 4 bytes and
 * idr_handle in the least significant 4 bytes
 */
#define MAKE_HANDLE(gpu_id, idr_handle) \
	(((uint64_t)(gpu_id) << 32) + idr_handle)
#define GET_GPU_ID(handle) (handle >> 32)
#define GET_IDR_HANDLE(handle) (handle & 0xFFFFFFFF)

681 682 683 684 685 686
enum kfd_pdd_bound {
	PDD_UNBOUND = 0,
	PDD_BOUND,
	PDD_BOUND_SUSPENDED,
};

687
#define MAX_SYSFS_FILENAME_LEN 15
688 689 690 691 692 693 694

/*
 * SDMA counter runs at 100MHz frequency.
 * We display SDMA activity in microsecond granularity in sysfs.
 * As a result, the divisor is 100.
 */
#define SDMA_ACTIVITY_DIVISOR  100
695

696 697 698 699 700
/* Data that is per-process-per device. */
struct kfd_process_device {
	/* The device that owns this data. */
	struct kfd_dev *dev;

701 702
	/* The process that owns this kfd_process_device. */
	struct kfd_process *process;
703

704 705 706
	/* per-process-per device QCM data structure */
	struct qcm_process_device qpd;

707 708 709 710 711 712 713 714
	/*Apertures*/
	uint64_t lds_base;
	uint64_t lds_limit;
	uint64_t gpuvm_base;
	uint64_t gpuvm_limit;
	uint64_t scratch_base;
	uint64_t scratch_limit;

715
	/* VM context for GPUVM allocations */
716
	struct file *drm_file;
717
	void *drm_priv;
718
	atomic64_t tlb_seq;
719

720 721 722
	/* GPUVM allocations storage */
	struct idr alloc_idr;

723 724 725 726
	/* Flag used to tell the pdd has dequeued from the dqm.
	 * This is used to prevent dev->dqm->ops.process_termination() from
	 * being called twice when it is already called in IOMMU callback
	 * function.
727
	 */
728
	bool already_dequeued;
729
	bool runtime_inuse;
730 731 732

	/* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
	enum kfd_pdd_bound bound;
733 734 735 736

	/* VRAM usage */
	uint64_t vram_usage;
	struct attribute attr_vram;
737 738 739 740 741 742
	char vram_filename[MAX_SYSFS_FILENAME_LEN];

	/* SDMA activity tracking */
	uint64_t sdma_past_activity_counter;
	struct attribute attr_sdma;
	char sdma_filename[MAX_SYSFS_FILENAME_LEN];
743 744 745 746 747 748 749

	/* Eviction activity tracking */
	uint64_t last_evict_timestamp;
	atomic64_t evict_duration_counter;
	struct attribute attr_evict;

	struct kobject *kobj_stats;
750
	unsigned int doorbell_index;
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

	/*
	 * @cu_occupancy: Reports occupancy of Compute Units (CU) of a process
	 * that is associated with device encoded by "this" struct instance. The
	 * value reflects CU usage by all of the waves launched by this process
	 * on this device. A very important property of occupancy parameter is
	 * that its value is a snapshot of current use.
	 *
	 * Following is to be noted regarding how this parameter is reported:
	 *
	 *  The number of waves that a CU can launch is limited by couple of
	 *  parameters. These are encoded by struct amdgpu_cu_info instance
	 *  that is part of every device definition. For GFX9 devices this
	 *  translates to 40 waves (simd_per_cu * max_waves_per_simd) when waves
	 *  do not use scratch memory and 32 waves (max_scratch_slots_per_cu)
	 *  when they do use scratch memory. This could change for future
	 *  devices and therefore this example should be considered as a guide.
	 *
	 *  All CU's of a device are available for the process. This may not be true
	 *  under certain conditions - e.g. CU masking.
	 *
	 *  Finally number of CU's that are occupied by a process is affected by both
	 *  number of CU's a device has along with number of other competing processes
	 */
	struct attribute attr_cu_occupancy;
776 777 778 779 780 781 782 783 784

	/* sysfs counters for GPU retry fault and page migration tracking */
	struct kobject *kobj_counters;
	struct attribute attr_faults;
	struct attribute attr_page_in;
	struct attribute attr_page_out;
	uint64_t faults;
	uint64_t page_in;
	uint64_t page_out;
785 786 787 788 789 790
	/*
	 * If this process has been checkpointed before, then the user
	 * application will use the original gpu_id on the
	 * checkpointed node to refer to this device.
	 */
	uint32_t user_gpu_id;
791 792 793 794

	void *proc_ctx_bo;
	uint64_t proc_ctx_gpu_addr;
	void *proc_ctx_cpu_ptr;
795 796
};

797 798
#define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)

P
Philip Yang 已提交
799 800 801 802
struct svm_range_list {
	struct mutex			lock;
	struct rb_root_cached		objects;
	struct list_head		list;
P
Philip Yang 已提交
803 804
	struct work_struct		deferred_list_work;
	struct list_head		deferred_range_list;
805
	struct list_head                criu_svm_metadata_list;
P
Philip Yang 已提交
806
	spinlock_t			deferred_list_lock;
807
	atomic_t			evicted_ranges;
808
	atomic_t			drain_pagefaults;
809
	struct delayed_work		restore_work;
810
	DECLARE_BITMAP(bitmap_supported, MAX_GPU_INSTANCE);
811
	struct task_struct		*faulting_task;
P
Philip Yang 已提交
812 813
};

O
Oded Gabbay 已提交
814 815
/* Process data */
struct kfd_process {
816 817 818 819 820 821
	/*
	 * kfd_process are stored in an mm_struct*->kfd_process*
	 * hash table (kfd_processes in kfd_process.c)
	 */
	struct hlist_node kfd_processes;

822 823 824 825 826 827
	/*
	 * Opaque pointer to mm_struct. We don't hold a reference to
	 * it so it should never be dereferenced from here. This is
	 * only used for looking up processes by their mm.
	 */
	void *mm;
828

829 830 831
	struct kref ref;
	struct work_struct release_work;

832 833 834 835 836 837
	struct mutex mutex;

	/*
	 * In any process, the thread that started main() is the lead
	 * thread and outlives the rest.
	 * It is here because amd_iommu_bind_pasid wants a task_struct.
F
Felix Kuehling 已提交
838 839
	 * It can also be used for safely getting a reference to the
	 * mm_struct of the process.
840 841 842 843 844 845
	 */
	struct task_struct *lead_thread;

	/* We want to receive a notification when the mm_struct is destroyed */
	struct mmu_notifier mmu_notifier;

846
	u32 pasid;
847 848

	/*
849
	 * Array of kfd_process_device pointers,
850 851
	 * one for each device the process is using.
	 */
852 853
	struct kfd_process_device *pdds[MAX_GPU_INSTANCE];
	uint32_t n_pdds;
854

855 856
	struct process_queue_manager pqm;

857 858
	/*Is the user space process 32 bit?*/
	bool is_32bit_user_mode;
859 860 861

	/* Event-related data */
	struct mutex event_mutex;
862 863
	/* Event ID allocator and lookup */
	struct idr event_idr;
864
	/* Event page */
865
	u64 signal_handle;
866
	struct kfd_signal_page *signal_page;
867
	size_t signal_mapped_size;
868
	size_t signal_event_count;
869
	bool signal_event_limit_reached;
870 871 872 873 874 875 876 877

	/* Information used for memory eviction */
	void *kgd_process_info;
	/* Eviction fence that is attached to all the BOs of this process. The
	 * fence will be triggered during eviction and new one will be created
	 * during restore
	 */
	struct dma_fence *ef;
878 879 880 881 882 883 884 885 886 887

	/* Work items for evicting and restoring BOs */
	struct delayed_work eviction_work;
	struct delayed_work restore_work;
	/* seqno of the last scheduled eviction */
	unsigned int last_eviction_seqno;
	/* Approx. the last timestamp (in jiffies) when the process was
	 * restored after an eviction
	 */
	unsigned long last_restore_timestamp;
888 889 890

	/* Kobj for our procfs */
	struct kobject *kobj;
891
	struct kobject *kobj_queues;
892
	struct attribute attr_pasid;
P
Philip Yang 已提交
893

P
Philip Yang 已提交
894 895
	/* shared virtual memory registered by this process */
	struct svm_range_list svms;
896 897

	bool xnack_enabled;
898 899

	atomic_t poison;
900 901
	/* Queues are in paused stated because we are in the process of doing a CRIU checkpoint */
	bool queues_paused;
O
Oded Gabbay 已提交
902 903
};

904 905 906 907
#define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
extern DECLARE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
extern struct srcu_struct kfd_processes_srcu;

908
/**
909 910 911 912 913
 * typedef amdkfd_ioctl_t - typedef for ioctl function pointer.
 *
 * @filep: pointer to file structure.
 * @p: amdkfd process pointer.
 * @data: pointer to arg that was copied from user.
914
 *
915
 * Return: returns ioctl completion code.
916 917 918 919 920 921 922 923 924 925 926
 */
typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
				void *data);

struct amdkfd_ioctl_desc {
	unsigned int cmd;
	int flags;
	amdkfd_ioctl_t *func;
	unsigned int cmd_drv;
	const char *name;
};
927
bool kfd_dev_is_large_bar(struct kfd_dev *dev);
928

929
int kfd_process_create_wq(void);
930
void kfd_process_destroy_wq(void);
F
Felix Kuehling 已提交
931
struct kfd_process *kfd_create_process(struct file *filep);
932
struct kfd_process *kfd_get_process(const struct task_struct *task);
933
struct kfd_process *kfd_lookup_process_by_pasid(u32 pasid);
934
struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
935 936

int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id);
937
int kfd_process_gpuid_from_adev(struct kfd_process *p,
938 939
			       struct amdgpu_device *adev, uint32_t *gpuid,
			       uint32_t *gpuidx);
940 941 942 943 944 945 946 947 948
static inline int kfd_process_gpuid_from_gpuidx(struct kfd_process *p,
				uint32_t gpuidx, uint32_t *gpuid) {
	return gpuidx < p->n_pdds ? p->pdds[gpuidx]->dev->id : -EINVAL;
}
static inline struct kfd_process_device *kfd_process_device_from_gpuidx(
				struct kfd_process *p, uint32_t gpuidx) {
	return gpuidx < p->n_pdds ? p->pdds[gpuidx] : NULL;
}

949
void kfd_unref_process(struct kfd_process *p);
950
int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger);
951
int kfd_process_restore_queues(struct kfd_process *p);
952 953
void kfd_suspend_all_processes(void);
int kfd_resume_all_processes(void);
954

955 956 957 958 959
struct kfd_process_device *kfd_process_device_data_by_id(struct kfd_process *process,
							 uint32_t gpu_id);

int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id);

960 961
int kfd_process_device_init_vm(struct kfd_process_device *pdd,
			       struct file *drm_file);
962
struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
963
						struct kfd_process *p);
964
struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
965 966 967
							struct kfd_process *p);
struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
							struct kfd_process *p);
968

969 970
bool kfd_process_xnack_mode(struct kfd_process *p, bool supported);

971
int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process,
F
Felix Kuehling 已提交
972 973
			  struct vm_area_struct *vma);

974 975 976 977 978 979 980
/* KFD process API for creating and translating handles */
int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
					void *mem);
void *kfd_process_device_translate_handle(struct kfd_process_device *p,
					int handle);
void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
					int handle);
981
struct kfd_process *kfd_lookup_process_by_pid(struct pid *pid);
982

983 984 985 986 987
/* PASIDs */
int kfd_pasid_init(void);
void kfd_pasid_exit(void);
bool kfd_set_pasid_limit(unsigned int new_limit);
unsigned int kfd_get_pasid_limit(void);
988 989
u32 kfd_pasid_alloc(void);
void kfd_pasid_free(u32 pasid);
990 991

/* Doorbells */
992
size_t kfd_doorbell_process_slice(struct kfd_dev *kfd);
993 994
int kfd_doorbell_init(struct kfd_dev *kfd);
void kfd_doorbell_fini(struct kfd_dev *kfd);
995 996
int kfd_doorbell_mmap(struct kfd_dev *dev, struct kfd_process *process,
		      struct vm_area_struct *vma);
997
void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
998 999 1000
					unsigned int *doorbell_off);
void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
u32 read_kernel_doorbell(u32 __iomem *db);
1001
void write_kernel_doorbell(void __iomem *db, u32 value);
1002
void write_kernel_doorbell64(void __iomem *db, u64 value);
1003
unsigned int kfd_get_doorbell_dw_offset_in_bar(struct kfd_dev *kfd,
1004
					struct kfd_process_device *pdd,
1005
					unsigned int doorbell_id);
1006 1007 1008 1009 1010
phys_addr_t kfd_get_process_doorbells(struct kfd_process_device *pdd);
int kfd_alloc_process_doorbells(struct kfd_dev *kfd,
				unsigned int *doorbell_index);
void kfd_free_process_doorbells(struct kfd_dev *kfd,
				unsigned int doorbell_index);
1011 1012 1013 1014 1015 1016 1017
/* GTT Sub-Allocator */

int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
			struct kfd_mem_obj **mem_obj);

int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj);

O
Oded Gabbay 已提交
1018 1019
extern struct device *kfd_device;

1020 1021 1022
/* KFD's procfs */
void kfd_procfs_init(void);
void kfd_procfs_shutdown(void);
1023 1024
int kfd_procfs_add_queue(struct queue *q);
void kfd_procfs_del_queue(struct queue *q);
1025

1026 1027 1028 1029 1030
/* Topology */
int kfd_topology_init(void);
void kfd_topology_shutdown(void);
int kfd_topology_add_device(struct kfd_dev *gpu);
int kfd_topology_remove_device(struct kfd_dev *gpu);
1031 1032
struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
						uint32_t proximity_domain);
1033 1034
struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
						uint32_t proximity_domain);
1035
struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id);
1036 1037
struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
1038
struct kfd_dev *kfd_device_by_adev(const struct amdgpu_device *adev);
1039
int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev);
1040
int kfd_numa_node_to_apic_id(int numa_node_id);
1041
void kfd_double_confirm_iommu_support(struct kfd_dev *gpu);
1042

O
Oded Gabbay 已提交
1043
/* Interrupts */
1044 1045 1046
int kfd_interrupt_init(struct kfd_dev *dev);
void kfd_interrupt_exit(struct kfd_dev *dev);
bool enqueue_ih_ring_entry(struct kfd_dev *kfd,	const void *ih_ring_entry);
1047 1048 1049
bool interrupt_is_wanted(struct kfd_dev *dev,
				const uint32_t *ih_ring_entry,
				uint32_t *patched_ihre, bool *flag);
O
Oded Gabbay 已提交
1050

1051 1052 1053
/* amdkfd Apertures */
int kfd_init_apertures(struct kfd_process *process);

1054 1055 1056 1057
void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
				  uint64_t tba_addr,
				  uint64_t tma_addr);

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
/* CRIU */
/*
 * Need to increment KFD_CRIU_PRIV_VERSION each time a change is made to any of the CRIU private
 * structures:
 * kfd_criu_process_priv_data
 * kfd_criu_device_priv_data
 * kfd_criu_bo_priv_data
 * kfd_criu_queue_priv_data
 * kfd_criu_event_priv_data
 * kfd_criu_svm_range_priv_data
 */

#define KFD_CRIU_PRIV_VERSION 1

struct kfd_criu_process_priv_data {
	uint32_t version;
1074
	uint32_t xnack_mode;
1075 1076 1077 1078 1079 1080 1081 1082
};

struct kfd_criu_device_priv_data {
	/* For future use */
	uint64_t reserved;
};

struct kfd_criu_bo_priv_data {
1083 1084 1085
	uint64_t user_addr;
	uint32_t idr_handle;
	uint32_t mapped_gpuids[MAX_GPU_INSTANCE];
1086 1087
};

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
/*
 * The first 4 bytes of kfd_criu_queue_priv_data, kfd_criu_event_priv_data,
 * kfd_criu_svm_range_priv_data is the object type
 */
enum kfd_criu_object_type {
	KFD_CRIU_OBJECT_TYPE_QUEUE,
	KFD_CRIU_OBJECT_TYPE_EVENT,
	KFD_CRIU_OBJECT_TYPE_SVM_RANGE,
};

1098 1099
struct kfd_criu_svm_range_priv_data {
	uint32_t object_type;
1100 1101 1102
	uint64_t start_addr;
	uint64_t size;
	/* Variable length array of attributes */
1103
	struct kfd_ioctl_svm_attribute attrs[];
1104 1105 1106 1107
};

struct kfd_criu_queue_priv_data {
	uint32_t object_type;
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	uint64_t q_address;
	uint64_t q_size;
	uint64_t read_ptr_addr;
	uint64_t write_ptr_addr;
	uint64_t doorbell_off;
	uint64_t eop_ring_buffer_address;
	uint64_t ctx_save_restore_area_address;
	uint32_t gpu_id;
	uint32_t type;
	uint32_t format;
	uint32_t q_id;
	uint32_t priority;
	uint32_t q_percent;
	uint32_t doorbell_id;
1122
	uint32_t gws;
1123 1124 1125 1126 1127
	uint32_t sdma_id;
	uint32_t eop_ring_buffer_size;
	uint32_t ctx_save_restore_area_size;
	uint32_t ctl_stack_size;
	uint32_t mqd_size;
1128 1129 1130 1131
};

struct kfd_criu_event_priv_data {
	uint32_t object_type;
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	uint64_t user_handle;
	uint32_t event_id;
	uint32_t auto_reset;
	uint32_t type;
	uint32_t signaled;

	union {
		struct kfd_hsa_memory_exception_data memory_exception_data;
		struct kfd_hsa_hw_exception_data hw_exception_data;
	};
1142 1143
};

1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
int kfd_process_get_queue_info(struct kfd_process *p,
			       uint32_t *num_queues,
			       uint64_t *priv_data_sizes);

int kfd_criu_checkpoint_queues(struct kfd_process *p,
			 uint8_t __user *user_priv_data,
			 uint64_t *priv_data_offset);

int kfd_criu_restore_queue(struct kfd_process *p,
			   uint8_t __user *user_priv_data,
			   uint64_t *priv_data_offset,
			   uint64_t max_priv_data_size);
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

int kfd_criu_checkpoint_events(struct kfd_process *p,
			 uint8_t __user *user_priv_data,
			 uint64_t *priv_data_offset);

int kfd_criu_restore_event(struct file *devkfd,
			   struct kfd_process *p,
			   uint8_t __user *user_priv_data,
			   uint64_t *priv_data_offset,
			   uint64_t max_priv_data_size);
1166 1167
/* CRIU - End */

B
Ben Goz 已提交
1168
/* Queue Context Management */
1169
int init_queue(struct queue **q, const struct queue_properties *properties);
B
Ben Goz 已提交
1170
void uninit_queue(struct queue *q);
1171
void print_queue_properties(struct queue_properties *q);
B
Ben Goz 已提交
1172 1173
void print_queue(struct queue *q);

1174 1175
struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
		struct kfd_dev *dev);
1176 1177
struct mqd_manager *mqd_manager_init_cik_hawaii(enum KFD_MQD_TYPE type,
		struct kfd_dev *dev);
1178 1179
struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
		struct kfd_dev *dev);
1180 1181
struct mqd_manager *mqd_manager_init_vi_tonga(enum KFD_MQD_TYPE type,
		struct kfd_dev *dev);
1182 1183
struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type,
		struct kfd_dev *dev);
1184 1185
struct mqd_manager *mqd_manager_init_v10(enum KFD_MQD_TYPE type,
		struct kfd_dev *dev);
1186 1187
struct mqd_manager *mqd_manager_init_v11(enum KFD_MQD_TYPE type,
		struct kfd_dev *dev);
1188 1189
struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev);
void device_queue_manager_uninit(struct device_queue_manager *dqm);
B
Ben Goz 已提交
1190 1191
struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
					enum kfd_queue_type type);
1192
void kernel_queue_uninit(struct kernel_queue *kq, bool hanging);
1193
int kfd_dqm_evict_pasid(struct device_queue_manager *dqm, u32 pasid);
B
Ben Goz 已提交
1194

1195 1196 1197 1198 1199 1200 1201
/* Process Queue Manager */
struct process_queue_node {
	struct queue *q;
	struct kernel_queue *kq;
	struct list_head process_queue_list;
};

1202 1203
void kfd_process_dequeue_from_device(struct kfd_process_device *pdd);
void kfd_process_dequeue_from_all_devices(struct kfd_process *p);
1204 1205 1206 1207 1208 1209
int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
void pqm_uninit(struct process_queue_manager *pqm);
int pqm_create_queue(struct process_queue_manager *pqm,
			    struct kfd_dev *dev,
			    struct file *f,
			    struct queue_properties *properties,
1210
			    unsigned int *qid,
1211
			    struct amdgpu_bo *wptr_bo,
1212
			    const struct kfd_criu_queue_priv_data *q_data,
1213
			    const void *restore_mqd,
1214
			    const void *restore_ctl_stack,
1215
			    uint32_t *p_doorbell_offset_in_process);
1216
int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
1217
int pqm_update_queue_properties(struct process_queue_manager *pqm, unsigned int qid,
1218
			struct queue_properties *p);
1219 1220
int pqm_update_mqd(struct process_queue_manager *pqm, unsigned int qid,
			struct mqd_update_info *minfo);
1221 1222
int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
			void *gws);
1223 1224
struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
						unsigned int qid);
1225 1226
struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
						unsigned int qid);
1227 1228 1229 1230 1231
int pqm_get_wave_state(struct process_queue_manager *pqm,
		       unsigned int qid,
		       void __user *ctl_stack,
		       u32 *ctl_stack_used_size,
		       u32 *save_area_used_size);
1232

1233 1234
int amdkfd_fence_wait_timeout(uint64_t *fence_addr,
			      uint64_t fence_value,
1235
			      unsigned int timeout_ms);
1236

1237 1238
int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm,
				  unsigned int qid,
1239 1240
				  u32 *mqd_size,
				  u32 *ctl_stack_size);
B
Ben Goz 已提交
1241 1242
/* Packet Manager */

1243 1244
#define KFD_FENCE_COMPLETED (100)
#define KFD_FENCE_INIT   (10)
B
Ben Goz 已提交
1245

B
Ben Goz 已提交
1246 1247 1248 1249 1250 1251
struct packet_manager {
	struct device_queue_manager *dqm;
	struct kernel_queue *priv_queue;
	struct mutex lock;
	bool allocated;
	struct kfd_mem_obj *ib_buffer_obj;
1252
	unsigned int ib_size_bytes;
1253
	bool is_over_subscription;
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

	const struct packet_manager_funcs *pmf;
};

struct packet_manager_funcs {
	/* Support ASIC-specific packet formats for PM4 packets */
	int (*map_process)(struct packet_manager *pm, uint32_t *buffer,
			struct qcm_process_device *qpd);
	int (*runlist)(struct packet_manager *pm, uint32_t *buffer,
			uint64_t ib, size_t ib_size_in_dwords, bool chain);
	int (*set_resources)(struct packet_manager *pm, uint32_t *buffer,
			struct scheduling_resources *res);
	int (*map_queues)(struct packet_manager *pm, uint32_t *buffer,
			struct queue *q, bool is_static);
	int (*unmap_queues)(struct packet_manager *pm, uint32_t *buffer,
			enum kfd_unmap_queues_filter mode,
1270
			uint32_t filter_param, bool reset);
1271
	int (*query_status)(struct packet_manager *pm, uint32_t *buffer,
1272
			uint64_t fence_address,	uint64_t fence_value);
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	int (*release_mem)(uint64_t gpu_addr, uint32_t *buffer);

	/* Packet sizes */
	int map_process_size;
	int runlist_size;
	int set_resources_size;
	int map_queues_size;
	int unmap_queues_size;
	int query_status_size;
	int release_mem_size;
B
Ben Goz 已提交
1283 1284
};

1285
extern const struct packet_manager_funcs kfd_vi_pm_funcs;
1286
extern const struct packet_manager_funcs kfd_v9_pm_funcs;
1287
extern const struct packet_manager_funcs kfd_aldebaran_pm_funcs;
1288

1289
int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
1290
void pm_uninit(struct packet_manager *pm, bool hanging);
1291 1292 1293 1294
int pm_send_set_resources(struct packet_manager *pm,
				struct scheduling_resources *res);
int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
1295
				uint64_t fence_value);
1296

1297
int pm_send_unmap_queue(struct packet_manager *pm,
1298
			enum kfd_unmap_queues_filter mode,
1299
			uint32_t filter_param, bool reset);
1300

B
Ben Goz 已提交
1301 1302
void pm_release_ib(struct packet_manager *pm);

1303 1304 1305
/* Following PM funcs can be shared among VI and AI */
unsigned int pm_build_pm4_header(unsigned int opcode, size_t packet_size);

1306 1307
uint64_t kfd_get_number_elems(struct kfd_dev *kfd);

1308 1309
/* Events */
extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
1310
extern const struct kfd_event_interrupt_class event_interrupt_class_v9;
1311
extern const struct kfd_event_interrupt_class event_interrupt_class_v11;
1312

1313
extern const struct kfd_device_global_init_class device_global_init_class_cik;
1314

1315
int kfd_event_init_process(struct kfd_process *p);
1316 1317 1318
void kfd_event_free_process(struct kfd_process *p);
int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
int kfd_wait_on_events(struct kfd_process *p,
1319
		       uint32_t num_events, void __user *data,
1320
		       bool all, uint32_t user_timeout_ms,
1321
		       uint32_t *wait_result);
1322
void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
1323
				uint32_t valid_id_bits);
1324
void kfd_signal_iommu_event(struct kfd_dev *dev,
1325 1326 1327
			    u32 pasid, unsigned long address,
			    bool is_write_requested, bool is_execute_requested);
void kfd_signal_hw_exception_event(u32 pasid);
1328 1329
int kfd_set_event(struct kfd_process *p, uint32_t event_id);
int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
1330 1331
int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset);

1332 1333 1334 1335
int kfd_event_create(struct file *devkfd, struct kfd_process *p,
		     uint32_t event_type, bool auto_reset, uint32_t node_id,
		     uint32_t *event_id, uint32_t *event_trigger_data,
		     uint64_t *event_page_offset, uint32_t *event_slot_index);
1336 1337

int kfd_get_num_events(struct kfd_process *p);
1338 1339
int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);

1340
void kfd_signal_vm_fault_event(struct kfd_dev *dev, u32 pasid,
S
shaoyunl 已提交
1341 1342
				struct kfd_vm_fault_info *info);

1343 1344
void kfd_signal_reset_event(struct kfd_dev *dev);

1345 1346
void kfd_signal_poison_consumed_event(struct kfd_dev *dev, u32 pasid);

1347
void kfd_flush_tlb(struct kfd_process_device *pdd, enum TLB_FLUSH_TYPE type);
1348

1349 1350 1351 1352 1353 1354 1355 1356
static inline bool kfd_flush_tlb_after_unmap(struct kfd_dev *dev)
{
	return KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 2) ||
	       (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 1) &&
	       dev->adev->sdma.instance[0].fw_version >= 18) ||
	       KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 0);
}

1357 1358
bool kfd_is_locked(void);

1359 1360 1361 1362
/* Compute profile */
void kfd_inc_compute_active(struct kfd_dev *dev);
void kfd_dec_compute_active(struct kfd_dev *dev);

1363 1364 1365 1366
/* Cgroup Support */
/* Check with device cgroup if @kfd device is accessible */
static inline int kfd_devcgroup_check_permission(struct kfd_dev *kfd)
{
1367
#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)
1368 1369
	struct drm_device *ddev = kfd->ddev;

1370
	return devcgroup_check_permission(DEVCG_DEV_CHAR, DRM_MAJOR,
1371 1372 1373 1374 1375 1376 1377
					  ddev->render->index,
					  DEVCG_ACC_WRITE | DEVCG_ACC_READ);
#else
	return 0;
#endif
}

1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
/* Debugfs */
#if defined(CONFIG_DEBUG_FS)

void kfd_debugfs_init(void);
void kfd_debugfs_fini(void);
int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data);
int pqm_debugfs_mqds(struct seq_file *m, void *data);
int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data);
int dqm_debugfs_hqds(struct seq_file *m, void *data);
int kfd_debugfs_rls_by_device(struct seq_file *m, void *data);
int pm_debugfs_runlist(struct seq_file *m, void *data);

1390 1391
int kfd_debugfs_hang_hws(struct kfd_dev *dev);
int pm_debugfs_hang_hws(struct packet_manager *pm);
1392
int dqm_debugfs_hang_hws(struct device_queue_manager *dqm);
1393

1394 1395 1396 1397 1398 1399 1400
#else

static inline void kfd_debugfs_init(void) {}
static inline void kfd_debugfs_fini(void) {}

#endif

O
Oded Gabbay 已提交
1401
#endif