xref: /openbmc/linux/drivers/gpu/drm/amd/amdkfd/kfd_priv.h (revision 7c9631af)
14a488a7aSOded Gabbay /*
24a488a7aSOded Gabbay  * Copyright 2014 Advanced Micro Devices, Inc.
34a488a7aSOded Gabbay  *
44a488a7aSOded Gabbay  * Permission is hereby granted, free of charge, to any person obtaining a
54a488a7aSOded Gabbay  * copy of this software and associated documentation files (the "Software"),
64a488a7aSOded Gabbay  * to deal in the Software without restriction, including without limitation
74a488a7aSOded Gabbay  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84a488a7aSOded Gabbay  * and/or sell copies of the Software, and to permit persons to whom the
94a488a7aSOded Gabbay  * Software is furnished to do so, subject to the following conditions:
104a488a7aSOded Gabbay  *
114a488a7aSOded Gabbay  * The above copyright notice and this permission notice shall be included in
124a488a7aSOded Gabbay  * all copies or substantial portions of the Software.
134a488a7aSOded Gabbay  *
144a488a7aSOded Gabbay  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154a488a7aSOded Gabbay  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164a488a7aSOded Gabbay  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174a488a7aSOded Gabbay  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184a488a7aSOded Gabbay  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194a488a7aSOded Gabbay  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204a488a7aSOded Gabbay  * OTHER DEALINGS IN THE SOFTWARE.
214a488a7aSOded Gabbay  */
224a488a7aSOded Gabbay 
234a488a7aSOded Gabbay #ifndef KFD_PRIV_H_INCLUDED
244a488a7aSOded Gabbay #define KFD_PRIV_H_INCLUDED
254a488a7aSOded Gabbay 
264a488a7aSOded Gabbay #include <linux/hashtable.h>
274a488a7aSOded Gabbay #include <linux/mmu_notifier.h>
284a488a7aSOded Gabbay #include <linux/mutex.h>
294a488a7aSOded Gabbay #include <linux/types.h>
304a488a7aSOded Gabbay #include <linux/atomic.h>
314a488a7aSOded Gabbay #include <linux/workqueue.h>
324a488a7aSOded Gabbay #include <linux/spinlock.h>
3319f6d2a6SOded Gabbay #include <linux/kfd_ioctl.h>
34482f0777SFelix Kuehling #include <linux/idr.h>
3504ad47bdSAndres Rodriguez #include <linux/kfifo.h>
36851a645eSFelix Kuehling #include <linux/seq_file.h>
375ce10687SFelix Kuehling #include <linux/kref.h>
38de9f26bbSKent Russell #include <linux/sysfs.h>
396b855f7bSHarish Kasiviswanathan #include <linux/device_cgroup.h>
401cd4d9eeSStephen Rothwell #include <drm/drm_file.h>
411cd4d9eeSStephen Rothwell #include <drm/drm_drv.h>
421cd4d9eeSStephen Rothwell #include <drm/drm_device.h>
4399c7b309SLorenz Brun #include <drm/drm_ioctl.h>
444a488a7aSOded Gabbay #include <kgd_kfd_interface.h>
456d220a7eSAmber Lin #include <linux/swap.h>
464a488a7aSOded Gabbay 
47e596b903SYong Zhao #include "amd_shared.h"
48e596b903SYong Zhao 
49af47b390SLaura Abbott #define KFD_MAX_RING_ENTRY_SIZE	8
50af47b390SLaura Abbott 
515b5c4e40SEvgeny Pinchuk #define KFD_SYSFS_FILE_MODE 0444
525b5c4e40SEvgeny Pinchuk 
53df03ef93SHarish Kasiviswanathan /* GPU ID hash width in bits */
54df03ef93SHarish Kasiviswanathan #define KFD_GPU_ID_HASH_WIDTH 16
55df03ef93SHarish Kasiviswanathan 
56df03ef93SHarish Kasiviswanathan /* Use upper bits of mmap offset to store KFD driver specific information.
57df03ef93SHarish Kasiviswanathan  * BITS[63:62] - Encode MMAP type
58df03ef93SHarish Kasiviswanathan  * BITS[61:46] - Encode gpu_id. To identify to which GPU the offset belongs to
59df03ef93SHarish Kasiviswanathan  * BITS[45:0]  - MMAP offset value
60df03ef93SHarish Kasiviswanathan  *
61df03ef93SHarish Kasiviswanathan  * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
62df03ef93SHarish Kasiviswanathan  *  defines are w.r.t to PAGE_SIZE
63df03ef93SHarish Kasiviswanathan  */
6429453755SYong Zhao #define KFD_MMAP_TYPE_SHIFT	62
65df03ef93SHarish Kasiviswanathan #define KFD_MMAP_TYPE_MASK	(0x3ULL << KFD_MMAP_TYPE_SHIFT)
66df03ef93SHarish Kasiviswanathan #define KFD_MMAP_TYPE_DOORBELL	(0x3ULL << KFD_MMAP_TYPE_SHIFT)
67df03ef93SHarish Kasiviswanathan #define KFD_MMAP_TYPE_EVENTS	(0x2ULL << KFD_MMAP_TYPE_SHIFT)
68df03ef93SHarish Kasiviswanathan #define KFD_MMAP_TYPE_RESERVED_MEM	(0x1ULL << KFD_MMAP_TYPE_SHIFT)
69d33ea570SOak Zeng #define KFD_MMAP_TYPE_MMIO	(0x0ULL << KFD_MMAP_TYPE_SHIFT)
70df03ef93SHarish Kasiviswanathan 
7129453755SYong Zhao #define KFD_MMAP_GPU_ID_SHIFT 46
72df03ef93SHarish Kasiviswanathan #define KFD_MMAP_GPU_ID_MASK (((1ULL << KFD_GPU_ID_HASH_WIDTH) - 1) \
73df03ef93SHarish Kasiviswanathan 				<< KFD_MMAP_GPU_ID_SHIFT)
74df03ef93SHarish Kasiviswanathan #define KFD_MMAP_GPU_ID(gpu_id) ((((uint64_t)gpu_id) << KFD_MMAP_GPU_ID_SHIFT)\
75df03ef93SHarish Kasiviswanathan 				& KFD_MMAP_GPU_ID_MASK)
7629453755SYong Zhao #define KFD_MMAP_GET_GPU_ID(offset)    ((offset & KFD_MMAP_GPU_ID_MASK) \
77df03ef93SHarish Kasiviswanathan 				>> KFD_MMAP_GPU_ID_SHIFT)
78df03ef93SHarish Kasiviswanathan 
79ed6e6a34SBen Goz /*
80ed6e6a34SBen Goz  * When working with cp scheduler we should assign the HIQ manually or via
81e7016d8eSYong Zhao  * the amdgpu driver to a fixed hqd slot, here are the fixed HIQ hqd slot
82ed6e6a34SBen Goz  * definitions for Kaveri. In Kaveri only the first ME queues participates
83ed6e6a34SBen Goz  * in the cp scheduling taking that in mind we set the HIQ slot in the
84ed6e6a34SBen Goz  * second ME.
85ed6e6a34SBen Goz  */
86ed6e6a34SBen Goz #define KFD_CIK_HIQ_PIPE 4
87ed6e6a34SBen Goz #define KFD_CIK_HIQ_QUEUE 0
88ed6e6a34SBen Goz 
895b5c4e40SEvgeny Pinchuk /* Macro for allocating structures */
905b5c4e40SEvgeny Pinchuk #define kfd_alloc_struct(ptr_to_struct)	\
915b5c4e40SEvgeny Pinchuk 	((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
925b5c4e40SEvgeny Pinchuk 
9319f6d2a6SOded Gabbay #define KFD_MAX_NUM_OF_PROCESSES 512
94b8cbab04SOded Gabbay #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
9519f6d2a6SOded Gabbay 
9619f6d2a6SOded Gabbay /*
97373d7080SFelix Kuehling  * Size of the per-process TBA+TMA buffer: 2 pages
98373d7080SFelix Kuehling  *
99373d7080SFelix Kuehling  * The first page is the TBA used for the CWSR ISA code. The second
100a4497974SRajneesh Bhardwaj  * page is used as TMA for user-mode trap handler setup in daisy-chain mode.
101373d7080SFelix Kuehling  */
102373d7080SFelix Kuehling #define KFD_CWSR_TBA_TMA_SIZE (PAGE_SIZE * 2)
103373d7080SFelix Kuehling #define KFD_CWSR_TMA_OFFSET PAGE_SIZE
104373d7080SFelix Kuehling 
10574523943SYong Zhao #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE		\
10674523943SYong Zhao 	(KFD_MAX_NUM_OF_PROCESSES *			\
10774523943SYong Zhao 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
10874523943SYong Zhao 
10974523943SYong Zhao #define KFD_KERNEL_QUEUE_SIZE 2048
11074523943SYong Zhao 
11114328aa5SPhilip Cox #define KFD_UNMAP_LATENCY_MS	(4000)
11214328aa5SPhilip Cox 
113373d7080SFelix Kuehling /*
1141f86805aSYong Zhao  * 512 = 0x200
1151f86805aSYong Zhao  * The doorbell index distance between SDMA RLC (2*i) and (2*i+1) in the
1161f86805aSYong Zhao  * same SDMA engine on SOC15, which has 8-byte doorbells for SDMA.
1171f86805aSYong Zhao  * 512 8-byte doorbell distance (i.e. one page away) ensures that SDMA RLC
1181f86805aSYong Zhao  * (2*i+1) doorbells (in terms of the lower 12 bit address) lie exactly in
1191f86805aSYong Zhao  * the OFFSET and SIZE set in registers like BIF_SDMA0_DOORBELL_RANGE.
1201f86805aSYong Zhao  */
1211f86805aSYong Zhao #define KFD_QUEUE_DOORBELL_MIRROR_OFFSET 512
1221f86805aSYong Zhao 
1231f86805aSYong Zhao 
1241f86805aSYong Zhao /*
125b8cbab04SOded Gabbay  * Kernel module parameter to specify maximum number of supported queues per
126b8cbab04SOded Gabbay  * device
12719f6d2a6SOded Gabbay  */
128b8cbab04SOded Gabbay extern int max_num_of_queues_per_device;
12919f6d2a6SOded Gabbay 
130ed6e6a34SBen Goz 
13131c21fecSBen Goz /* Kernel module parameter to specify the scheduling policy */
13231c21fecSBen Goz extern int sched_policy;
13331c21fecSBen Goz 
134a99c6d4fSFelix Kuehling /*
135a99c6d4fSFelix Kuehling  * Kernel module parameter to specify the maximum process
136a99c6d4fSFelix Kuehling  * number per HW scheduler
137a99c6d4fSFelix Kuehling  */
138a99c6d4fSFelix Kuehling extern int hws_max_conc_proc;
139a99c6d4fSFelix Kuehling 
140373d7080SFelix Kuehling extern int cwsr_enable;
141373d7080SFelix Kuehling 
14281663016SOded Gabbay /*
14381663016SOded Gabbay  * Kernel module parameter to specify whether to send sigterm to HSA process on
14481663016SOded Gabbay  * unhandled exception
14581663016SOded Gabbay  */
14681663016SOded Gabbay extern int send_sigterm;
14781663016SOded Gabbay 
148ebcfd1e2SFelix Kuehling /*
149374200b1SFelix Kuehling  * This kernel module is used to simulate large bar machine on non-large bar
150374200b1SFelix Kuehling  * enabled machines.
151374200b1SFelix Kuehling  */
152374200b1SFelix Kuehling extern int debug_largebar;
153374200b1SFelix Kuehling 
154374200b1SFelix Kuehling /*
155ebcfd1e2SFelix Kuehling  * Ignore CRAT table during KFD initialization, can be used to work around
156ebcfd1e2SFelix Kuehling  * broken CRAT tables on some AMD systems
157ebcfd1e2SFelix Kuehling  */
158ebcfd1e2SFelix Kuehling extern int ignore_crat;
159ebcfd1e2SFelix Kuehling 
160a4497974SRajneesh Bhardwaj /* Set sh_mem_config.retry_disable on GFX v9 */
16175ee6487SFelix Kuehling extern int amdgpu_noretry;
162bed4f110SFelix Kuehling 
163a4497974SRajneesh Bhardwaj /* Halt if HWS hang is detected */
1640e9a860cSYong Zhao extern int halt_if_hws_hang;
1650e9a860cSYong Zhao 
166a4497974SRajneesh Bhardwaj /* Whether MEC FW support GWS barriers */
16729e76462SOak Zeng extern bool hws_gws_support;
16829e76462SOak Zeng 
169a4497974SRajneesh Bhardwaj /* Queue preemption timeout in ms */
17014328aa5SPhilip Cox extern int queue_preemption_timeout_ms;
17114328aa5SPhilip Cox 
172a4497974SRajneesh Bhardwaj /* Enable eviction debug messages */
173b2057956SFelix Kuehling extern bool debug_evictions;
174b2057956SFelix Kuehling 
175ed6e6a34SBen Goz enum cache_policy {
176ed6e6a34SBen Goz 	cache_policy_coherent,
177ed6e6a34SBen Goz 	cache_policy_noncoherent
178ed6e6a34SBen Goz };
179ed6e6a34SBen Goz 
180ef568db7SFelix Kuehling #define KFD_IS_SOC15(chip) ((chip) >= CHIP_VEGA10)
181ef568db7SFelix Kuehling 
182f3a39818SAndrew Lewycky struct kfd_event_interrupt_class {
183f3a39818SAndrew Lewycky 	bool (*interrupt_isr)(struct kfd_dev *dev,
18458e69886SLan Xiao 			const uint32_t *ih_ring_entry, uint32_t *patched_ihre,
18558e69886SLan Xiao 			bool *patched_flag);
186f3a39818SAndrew Lewycky 	void (*interrupt_wq)(struct kfd_dev *dev,
187f3a39818SAndrew Lewycky 			const uint32_t *ih_ring_entry);
188f3a39818SAndrew Lewycky };
189f3a39818SAndrew Lewycky 
1904a488a7aSOded Gabbay struct kfd_device_info {
191e596b903SYong Zhao 	enum amd_asic_type asic_family;
192c181159aSYong Zhao 	const char *asic_name;
193f3a39818SAndrew Lewycky 	const struct kfd_event_interrupt_class *event_interrupt_class;
1944a488a7aSOded Gabbay 	unsigned int max_pasid_bits;
195992839adSYair Shachar 	unsigned int max_no_of_hqd;
196ada2b29cSFelix Kuehling 	unsigned int doorbell_size;
1974a488a7aSOded Gabbay 	size_t ih_ring_entry_size;
198f7c826adSAlexey Skidanov 	uint8_t num_of_watch_points;
19919f6d2a6SOded Gabbay 	uint16_t mqd_size_aligned;
200373d7080SFelix Kuehling 	bool supports_cwsr;
20164d1c3a4SFelix Kuehling 	bool needs_iommu_device;
2023ee2d00cSFelix Kuehling 	bool needs_pci_atomics;
20398bb9222SYong Zhao 	unsigned int num_sdma_engines;
2041b4670f6SOak Zeng 	unsigned int num_xgmi_sdma_engines;
205d5094189SShaoyun Liu 	unsigned int num_sdma_queues_per_engine;
2064a488a7aSOded Gabbay };
2074a488a7aSOded Gabbay 
20836b5c08fSOded Gabbay struct kfd_mem_obj {
20936b5c08fSOded Gabbay 	uint32_t range_start;
21036b5c08fSOded Gabbay 	uint32_t range_end;
21136b5c08fSOded Gabbay 	uint64_t gpu_addr;
21236b5c08fSOded Gabbay 	uint32_t *cpu_ptr;
213b91d43ddSFelix Kuehling 	void *gtt_mem;
21436b5c08fSOded Gabbay };
21536b5c08fSOded Gabbay 
21644008d7aSYong Zhao struct kfd_vmid_info {
21744008d7aSYong Zhao 	uint32_t first_vmid_kfd;
21844008d7aSYong Zhao 	uint32_t last_vmid_kfd;
21944008d7aSYong Zhao 	uint32_t vmid_num_kfd;
22044008d7aSYong Zhao };
22144008d7aSYong Zhao 
2224a488a7aSOded Gabbay struct kfd_dev {
2234a488a7aSOded Gabbay 	struct kgd_dev *kgd;
2244a488a7aSOded Gabbay 
2254a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
2264a488a7aSOded Gabbay 	struct pci_dev *pdev;
2273a0c3423SHarish Kasiviswanathan 	struct drm_device *ddev;
2284a488a7aSOded Gabbay 
2294a488a7aSOded Gabbay 	unsigned int id;		/* topology stub index */
2304a488a7aSOded Gabbay 
23119f6d2a6SOded Gabbay 	phys_addr_t doorbell_base;	/* Start of actual doorbells used by
23219f6d2a6SOded Gabbay 					 * KFD. It is aligned for mapping
23319f6d2a6SOded Gabbay 					 * into user mode
23419f6d2a6SOded Gabbay 					 */
235339903faSYong Zhao 	size_t doorbell_base_dw_offset;	/* Offset from the start of the PCI
236339903faSYong Zhao 					 * doorbell BAR to the first KFD
237339903faSYong Zhao 					 * doorbell in dwords. GFX reserves
238339903faSYong Zhao 					 * the segment before this offset.
23919f6d2a6SOded Gabbay 					 */
24019f6d2a6SOded Gabbay 	u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
24119f6d2a6SOded Gabbay 					   * page used by kernel queue
24219f6d2a6SOded Gabbay 					   */
24319f6d2a6SOded Gabbay 
2444a488a7aSOded Gabbay 	struct kgd2kfd_shared_resources shared_resources;
24544008d7aSYong Zhao 	struct kfd_vmid_info vm_info;
2464a488a7aSOded Gabbay 
247cea405b1SXihan Zhang 	const struct kfd2kgd_calls *kfd2kgd;
248cea405b1SXihan Zhang 	struct mutex doorbell_mutex;
249f761d8bdSJoe Perches 	DECLARE_BITMAP(doorbell_available_index,
250f761d8bdSJoe Perches 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
251cea405b1SXihan Zhang 
25236b5c08fSOded Gabbay 	void *gtt_mem;
25336b5c08fSOded Gabbay 	uint64_t gtt_start_gpu_addr;
25436b5c08fSOded Gabbay 	void *gtt_start_cpu_ptr;
25536b5c08fSOded Gabbay 	void *gtt_sa_bitmap;
25636b5c08fSOded Gabbay 	struct mutex gtt_sa_lock;
25736b5c08fSOded Gabbay 	unsigned int gtt_sa_chunk_size;
25836b5c08fSOded Gabbay 	unsigned int gtt_sa_num_of_chunks;
25936b5c08fSOded Gabbay 
2602249d558SAndrew Lewycky 	/* Interrupts */
26104ad47bdSAndres Rodriguez 	struct kfifo ih_fifo;
26248e876a2SAndres Rodriguez 	struct workqueue_struct *ih_wq;
2632249d558SAndrew Lewycky 	struct work_struct interrupt_work;
2642249d558SAndrew Lewycky 	spinlock_t interrupt_lock;
2652249d558SAndrew Lewycky 
266ed6e6a34SBen Goz 	/* QCM Device instance */
267ed6e6a34SBen Goz 	struct device_queue_manager *dqm;
2684a488a7aSOded Gabbay 
269ed6e6a34SBen Goz 	bool init_complete;
2702249d558SAndrew Lewycky 	/*
2712249d558SAndrew Lewycky 	 * Interrupts of interest to KFD are copied
2722249d558SAndrew Lewycky 	 * from the HW ring into a SW ring.
2732249d558SAndrew Lewycky 	 */
2742249d558SAndrew Lewycky 	bool interrupts_active;
275fbeb661bSYair Shachar 
276fbeb661bSYair Shachar 	/* Debug manager */
277fbeb661bSYair Shachar 	struct kfd_dbgmgr *dbgmgr;
278373d7080SFelix Kuehling 
2795ade6c9cSFelix Kuehling 	/* Firmware versions */
2805ade6c9cSFelix Kuehling 	uint16_t mec_fw_version;
28129633d0eSJoseph Greathouse 	uint16_t mec2_fw_version;
2825ade6c9cSFelix Kuehling 	uint16_t sdma_fw_version;
2835ade6c9cSFelix Kuehling 
284a99c6d4fSFelix Kuehling 	/* Maximum process number mapped to HW scheduler */
285a99c6d4fSFelix Kuehling 	unsigned int max_proc_per_quantum;
286a99c6d4fSFelix Kuehling 
287373d7080SFelix Kuehling 	/* CWSR */
288373d7080SFelix Kuehling 	bool cwsr_enabled;
289373d7080SFelix Kuehling 	const void *cwsr_isa;
290373d7080SFelix Kuehling 	unsigned int cwsr_isa_size;
2910c1690e3SShaoyun Liu 
2920c1690e3SShaoyun Liu 	/* xGMI */
2930c1690e3SShaoyun Liu 	uint64_t hive_id;
2940c663695SDivya Shikre 
295d35f00d8SEric Huang 	bool pci_atomic_requested;
2969b54d201SEric Huang 
2976127896fSHuang Rui 	/* Use IOMMU v2 flag */
2986127896fSHuang Rui 	bool use_iommu_v2;
2996127896fSHuang Rui 
3009b54d201SEric Huang 	/* SRAM ECC flag */
3019b54d201SEric Huang 	atomic_t sram_ecc_flag;
302f756e631SHarish Kasiviswanathan 
303f756e631SHarish Kasiviswanathan 	/* Compute Profile ref. count */
304f756e631SHarish Kasiviswanathan 	atomic_t compute_profile;
305e09d4fc8SOak Zeng 
306a4497974SRajneesh Bhardwaj 	/* Global GWS resource shared between processes */
307e09d4fc8SOak Zeng 	void *gws;
308938a0650SAmber Lin 
309938a0650SAmber Lin 	/* Clients watching SMI events */
310938a0650SAmber Lin 	struct list_head smi_clients;
311938a0650SAmber Lin 	spinlock_t smi_lock;
31255977744SMukul Joshi 
31355977744SMukul Joshi 	uint32_t reset_seq_num;
31459d7115dSMukul Joshi 
31559d7115dSMukul Joshi 	struct ida doorbell_ida;
31659d7115dSMukul Joshi 	unsigned int max_doorbell_slices;
3179b498efaSAlex Deucher 
3189b498efaSAlex Deucher 	int noretry;
3194a488a7aSOded Gabbay };
3204a488a7aSOded Gabbay 
32119f6d2a6SOded Gabbay enum kfd_mempool {
32219f6d2a6SOded Gabbay 	KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
32319f6d2a6SOded Gabbay 	KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
32419f6d2a6SOded Gabbay 	KFD_MEMPOOL_FRAMEBUFFER = 3,
32519f6d2a6SOded Gabbay };
32619f6d2a6SOded Gabbay 
3274a488a7aSOded Gabbay /* Character device interface */
3284a488a7aSOded Gabbay int kfd_chardev_init(void);
3294a488a7aSOded Gabbay void kfd_chardev_exit(void);
3304a488a7aSOded Gabbay struct device *kfd_chardev(void);
3314a488a7aSOded Gabbay 
332241f24f8SBen Goz /**
333a4497974SRajneesh Bhardwaj  * enum kfd_unmap_queues_filter - Enum for queue filters.
334241f24f8SBen Goz  *
3357da2bcf8SYong Zhao  * @KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE: Preempts single queue.
336241f24f8SBen Goz  *
3377da2bcf8SYong Zhao  * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the
338241f24f8SBen Goz  *						running queues list.
339241f24f8SBen Goz  *
3407da2bcf8SYong Zhao  * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to
341241f24f8SBen Goz  *						specific process.
342241f24f8SBen Goz  *
343241f24f8SBen Goz  */
3447da2bcf8SYong Zhao enum kfd_unmap_queues_filter {
3457da2bcf8SYong Zhao 	KFD_UNMAP_QUEUES_FILTER_SINGLE_QUEUE,
3467da2bcf8SYong Zhao 	KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES,
3477da2bcf8SYong Zhao 	KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
3487da2bcf8SYong Zhao 	KFD_UNMAP_QUEUES_FILTER_BY_PASID
349241f24f8SBen Goz };
35019f6d2a6SOded Gabbay 
351ed8aab45SBen Goz /**
352a4497974SRajneesh Bhardwaj  * enum kfd_queue_type - Enum for various queue types.
353ed8aab45SBen Goz  *
354ed8aab45SBen Goz  * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
355ed8aab45SBen Goz  *
356a4497974SRajneesh Bhardwaj  * @KFD_QUEUE_TYPE_SDMA: SDMA user mode queue type.
357ed8aab45SBen Goz  *
358ed8aab45SBen Goz  * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
359ed8aab45SBen Goz  *
360ed8aab45SBen Goz  * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
361a4497974SRajneesh Bhardwaj  *
362a4497974SRajneesh Bhardwaj  * @KFD_QUEUE_TYPE_SDMA_XGMI: Special SDMA queue for XGMI interface.
363ed8aab45SBen Goz  */
364ed8aab45SBen Goz enum kfd_queue_type  {
365ed8aab45SBen Goz 	KFD_QUEUE_TYPE_COMPUTE,
366ed8aab45SBen Goz 	KFD_QUEUE_TYPE_SDMA,
367ed8aab45SBen Goz 	KFD_QUEUE_TYPE_HIQ,
3681b4670f6SOak Zeng 	KFD_QUEUE_TYPE_DIQ,
3691b4670f6SOak Zeng 	KFD_QUEUE_TYPE_SDMA_XGMI
370ed8aab45SBen Goz };
371ed8aab45SBen Goz 
3726e99df57SBen Goz enum kfd_queue_format {
3736e99df57SBen Goz 	KFD_QUEUE_FORMAT_PM4,
3746e99df57SBen Goz 	KFD_QUEUE_FORMAT_AQL
3756e99df57SBen Goz };
3766e99df57SBen Goz 
3770ccbc7cdSOak Zeng enum KFD_QUEUE_PRIORITY {
3780ccbc7cdSOak Zeng 	KFD_QUEUE_PRIORITY_MINIMUM = 0,
3790ccbc7cdSOak Zeng 	KFD_QUEUE_PRIORITY_MAXIMUM = 15
3800ccbc7cdSOak Zeng };
3810ccbc7cdSOak Zeng 
382ed8aab45SBen Goz /**
383ed8aab45SBen Goz  * struct queue_properties
384ed8aab45SBen Goz  *
385ed8aab45SBen Goz  * @type: The queue type.
386ed8aab45SBen Goz  *
387ed8aab45SBen Goz  * @queue_id: Queue identifier.
388ed8aab45SBen Goz  *
389ed8aab45SBen Goz  * @queue_address: Queue ring buffer address.
390ed8aab45SBen Goz  *
391ed8aab45SBen Goz  * @queue_size: Queue ring buffer size.
392ed8aab45SBen Goz  *
393ed8aab45SBen Goz  * @priority: Defines the queue priority relative to other queues in the
394ed8aab45SBen Goz  * process.
395ed8aab45SBen Goz  * This is just an indication and HW scheduling may override the priority as
396ed8aab45SBen Goz  * necessary while keeping the relative prioritization.
397ed8aab45SBen Goz  * the priority granularity is from 0 to f which f is the highest priority.
398ed8aab45SBen Goz  * currently all queues are initialized with the highest priority.
399ed8aab45SBen Goz  *
400ed8aab45SBen Goz  * @queue_percent: This field is partially implemented and currently a zero in
401ed8aab45SBen Goz  * this field defines that the queue is non active.
402ed8aab45SBen Goz  *
403ed8aab45SBen Goz  * @read_ptr: User space address which points to the number of dwords the
404ed8aab45SBen Goz  * cp read from the ring buffer. This field updates automatically by the H/W.
405ed8aab45SBen Goz  *
406ed8aab45SBen Goz  * @write_ptr: Defines the number of dwords written to the ring buffer.
407ed8aab45SBen Goz  *
408a4497974SRajneesh Bhardwaj  * @doorbell_ptr: Notifies the H/W of new packet written to the queue ring
409a4497974SRajneesh Bhardwaj  * buffer. This field should be similar to write_ptr and the user should
410a4497974SRajneesh Bhardwaj  * update this field after updating the write_ptr.
411ed8aab45SBen Goz  *
412ed8aab45SBen Goz  * @doorbell_off: The doorbell offset in the doorbell pci-bar.
413ed8aab45SBen Goz  *
4148eabaf54SKent Russell  * @is_interop: Defines if this is a interop queue. Interop queue means that
4158eabaf54SKent Russell  * the queue can access both graphics and compute resources.
416ed8aab45SBen Goz  *
41726103436SFelix Kuehling  * @is_evicted: Defines if the queue is evicted. Only active queues
41826103436SFelix Kuehling  * are evicted, rendering them inactive.
41926103436SFelix Kuehling  *
42026103436SFelix Kuehling  * @is_active: Defines if the queue is active or not. @is_active and
42126103436SFelix Kuehling  * @is_evicted are protected by the DQM lock.
422ed8aab45SBen Goz  *
423b8020b03SJoseph Greathouse  * @is_gws: Defines if the queue has been updated to be GWS-capable or not.
424b8020b03SJoseph Greathouse  * @is_gws should be protected by the DQM lock, since changing it can yield the
425b8020b03SJoseph Greathouse  * possibility of updating DQM state on number of GWS queues.
426b8020b03SJoseph Greathouse  *
427ed8aab45SBen Goz  * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
428ed8aab45SBen Goz  * of the queue.
429ed8aab45SBen Goz  *
430ed8aab45SBen Goz  * This structure represents the queue properties for each queue no matter if
431ed8aab45SBen Goz  * it's user mode or kernel mode queue.
432ed8aab45SBen Goz  *
433ed8aab45SBen Goz  */
434ed8aab45SBen Goz struct queue_properties {
435ed8aab45SBen Goz 	enum kfd_queue_type type;
4366e99df57SBen Goz 	enum kfd_queue_format format;
437ed8aab45SBen Goz 	unsigned int queue_id;
438ed8aab45SBen Goz 	uint64_t queue_address;
439ed8aab45SBen Goz 	uint64_t  queue_size;
440ed8aab45SBen Goz 	uint32_t priority;
441ed8aab45SBen Goz 	uint32_t queue_percent;
442ed8aab45SBen Goz 	uint32_t *read_ptr;
443ed8aab45SBen Goz 	uint32_t *write_ptr;
444ada2b29cSFelix Kuehling 	void __iomem *doorbell_ptr;
445ed8aab45SBen Goz 	uint32_t doorbell_off;
446ed8aab45SBen Goz 	bool is_interop;
44726103436SFelix Kuehling 	bool is_evicted;
448ed8aab45SBen Goz 	bool is_active;
449b8020b03SJoseph Greathouse 	bool is_gws;
450ed8aab45SBen Goz 	/* Not relevant for user mode queues in cp scheduling */
451ed8aab45SBen Goz 	unsigned int vmid;
45277669eb8SBen Goz 	/* Relevant only for sdma queues*/
45377669eb8SBen Goz 	uint32_t sdma_engine_id;
45477669eb8SBen Goz 	uint32_t sdma_queue_id;
45577669eb8SBen Goz 	uint32_t sdma_vm_addr;
456ff3d04a1SBen Goz 	/* Relevant only for VI */
457ff3d04a1SBen Goz 	uint64_t eop_ring_buffer_address;
458ff3d04a1SBen Goz 	uint32_t eop_ring_buffer_size;
459ff3d04a1SBen Goz 	uint64_t ctx_save_restore_area_address;
460ff3d04a1SBen Goz 	uint32_t ctx_save_restore_area_size;
461373d7080SFelix Kuehling 	uint32_t ctl_stack_size;
462373d7080SFelix Kuehling 	uint64_t tba_addr;
463373d7080SFelix Kuehling 	uint64_t tma_addr;
46439e7f331SFelix Kuehling 	/* Relevant for CU */
46539e7f331SFelix Kuehling 	uint32_t cu_mask_count; /* Must be a multiple of 32 */
46639e7f331SFelix Kuehling 	uint32_t *cu_mask;
467ed8aab45SBen Goz };
468ed8aab45SBen Goz 
469bb2d2128SFelix Kuehling #define QUEUE_IS_ACTIVE(q) ((q).queue_size > 0 &&	\
470bb2d2128SFelix Kuehling 			    (q).queue_address != 0 &&	\
471bb2d2128SFelix Kuehling 			    (q).queue_percent > 0 &&	\
472bb2d2128SFelix Kuehling 			    !(q).is_evicted)
473bb2d2128SFelix Kuehling 
474ed8aab45SBen Goz /**
475ed8aab45SBen Goz  * struct queue
476ed8aab45SBen Goz  *
477ed8aab45SBen Goz  * @list: Queue linked list.
478ed8aab45SBen Goz  *
479a4497974SRajneesh Bhardwaj  * @mqd: The queue MQD (memory queue descriptor).
480ed8aab45SBen Goz  *
481ed8aab45SBen Goz  * @mqd_mem_obj: The MQD local gpu memory object.
482ed8aab45SBen Goz  *
483ed8aab45SBen Goz  * @gart_mqd_addr: The MQD gart mc address.
484ed8aab45SBen Goz  *
485ed8aab45SBen Goz  * @properties: The queue properties.
486ed8aab45SBen Goz  *
487ed8aab45SBen Goz  * @mec: Used only in no cp scheduling mode and identifies to micro engine id
488a4497974SRajneesh Bhardwaj  *	 that the queue should be executed on.
489ed8aab45SBen Goz  *
4908eabaf54SKent Russell  * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
4918eabaf54SKent Russell  *	  id.
492ed8aab45SBen Goz  *
493ed8aab45SBen Goz  * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
494ed8aab45SBen Goz  *
495ed8aab45SBen Goz  * @process: The kfd process that created this queue.
496ed8aab45SBen Goz  *
497ed8aab45SBen Goz  * @device: The kfd device that created this queue.
498ed8aab45SBen Goz  *
499eb82da1dSOak Zeng  * @gws: Pointing to gws kgd_mem if this is a gws control queue; NULL
500eb82da1dSOak Zeng  * otherwise.
501eb82da1dSOak Zeng  *
502ed8aab45SBen Goz  * This structure represents user mode compute queues.
503ed8aab45SBen Goz  * It contains all the necessary data to handle such queues.
504ed8aab45SBen Goz  *
505ed8aab45SBen Goz  */
506ed8aab45SBen Goz 
507ed8aab45SBen Goz struct queue {
508ed8aab45SBen Goz 	struct list_head list;
509ed8aab45SBen Goz 	void *mqd;
510ed8aab45SBen Goz 	struct kfd_mem_obj *mqd_mem_obj;
511ed8aab45SBen Goz 	uint64_t gart_mqd_addr;
512ed8aab45SBen Goz 	struct queue_properties properties;
513ed8aab45SBen Goz 
514ed8aab45SBen Goz 	uint32_t mec;
515ed8aab45SBen Goz 	uint32_t pipe;
516ed8aab45SBen Goz 	uint32_t queue;
517ed8aab45SBen Goz 
51877669eb8SBen Goz 	unsigned int sdma_id;
519ef568db7SFelix Kuehling 	unsigned int doorbell_id;
52077669eb8SBen Goz 
521ed8aab45SBen Goz 	struct kfd_process	*process;
522ed8aab45SBen Goz 	struct kfd_dev		*device;
523eb82da1dSOak Zeng 	void *gws;
5246d220a7eSAmber Lin 
5256d220a7eSAmber Lin 	/* procfs */
5266d220a7eSAmber Lin 	struct kobject kobj;
527ed8aab45SBen Goz };
528ed8aab45SBen Goz 
5296e99df57SBen Goz enum KFD_MQD_TYPE {
530d7c0b047SYong Zhao 	KFD_MQD_TYPE_HIQ = 0,		/* for hiq */
53185d258f9SBen Goz 	KFD_MQD_TYPE_CP,		/* for cp queues and diq */
53285d258f9SBen Goz 	KFD_MQD_TYPE_SDMA,		/* for sdma queues */
53359f650a0SOak Zeng 	KFD_MQD_TYPE_DIQ,		/* for diq */
5346e99df57SBen Goz 	KFD_MQD_TYPE_MAX
5356e99df57SBen Goz };
5366e99df57SBen Goz 
5370ccbc7cdSOak Zeng enum KFD_PIPE_PRIORITY {
5380ccbc7cdSOak Zeng 	KFD_PIPE_PRIORITY_CS_LOW = 0,
5390ccbc7cdSOak Zeng 	KFD_PIPE_PRIORITY_CS_MEDIUM,
5400ccbc7cdSOak Zeng 	KFD_PIPE_PRIORITY_CS_HIGH
5410ccbc7cdSOak Zeng };
5420ccbc7cdSOak Zeng 
543241f24f8SBen Goz struct scheduling_resources {
544241f24f8SBen Goz 	unsigned int vmid_mask;
545241f24f8SBen Goz 	enum kfd_queue_type type;
546241f24f8SBen Goz 	uint64_t queue_mask;
547241f24f8SBen Goz 	uint64_t gws_mask;
548241f24f8SBen Goz 	uint32_t oac_mask;
549241f24f8SBen Goz 	uint32_t gds_heap_base;
550241f24f8SBen Goz 	uint32_t gds_heap_size;
551241f24f8SBen Goz };
552241f24f8SBen Goz 
553241f24f8SBen Goz struct process_queue_manager {
554241f24f8SBen Goz 	/* data */
555241f24f8SBen Goz 	struct kfd_process	*process;
556241f24f8SBen Goz 	struct list_head	queues;
557241f24f8SBen Goz 	unsigned long		*queue_slot_bitmap;
558241f24f8SBen Goz };
559241f24f8SBen Goz 
560241f24f8SBen Goz struct qcm_process_device {
561241f24f8SBen Goz 	/* The Device Queue Manager that owns this data */
562241f24f8SBen Goz 	struct device_queue_manager *dqm;
563241f24f8SBen Goz 	struct process_queue_manager *pqm;
564241f24f8SBen Goz 	/* Queues list */
565241f24f8SBen Goz 	struct list_head queues_list;
566241f24f8SBen Goz 	struct list_head priv_queue_list;
567241f24f8SBen Goz 
568241f24f8SBen Goz 	unsigned int queue_count;
569241f24f8SBen Goz 	unsigned int vmid;
570241f24f8SBen Goz 	bool is_debug;
57126103436SFelix Kuehling 	unsigned int evicted; /* eviction counter, 0=active */
5729fd3f1bfSFelix Kuehling 
5739fd3f1bfSFelix Kuehling 	/* This flag tells if we should reset all wavefronts on
5749fd3f1bfSFelix Kuehling 	 * process termination
5759fd3f1bfSFelix Kuehling 	 */
5769fd3f1bfSFelix Kuehling 	bool reset_wavefronts;
5779fd3f1bfSFelix Kuehling 
578b8020b03SJoseph Greathouse 	/* This flag tells us if this process has a GWS-capable
579b8020b03SJoseph Greathouse 	 * queue that will be mapped into the runlist. It's
580b8020b03SJoseph Greathouse 	 * possible to request a GWS BO, but not have the queue
581b8020b03SJoseph Greathouse 	 * currently mapped, and this changes how the MAP_PROCESS
582b8020b03SJoseph Greathouse 	 * PM4 packet is configured.
583b8020b03SJoseph Greathouse 	 */
584b8020b03SJoseph Greathouse 	bool mapped_gws_queue;
585b8020b03SJoseph Greathouse 
586a4497974SRajneesh Bhardwaj 	/* All the memory management data should be here too */
587241f24f8SBen Goz 	uint64_t gds_context_area;
588435e2f97SYong Zhao 	/* Contains page table flags such as AMDGPU_PTE_VALID since gfx9 */
589e715c6d0SShaoyun Liu 	uint64_t page_table_base;
590241f24f8SBen Goz 	uint32_t sh_mem_config;
591241f24f8SBen Goz 	uint32_t sh_mem_bases;
592241f24f8SBen Goz 	uint32_t sh_mem_ape1_base;
593241f24f8SBen Goz 	uint32_t sh_mem_ape1_limit;
594241f24f8SBen Goz 	uint32_t gds_size;
595241f24f8SBen Goz 	uint32_t num_gws;
596241f24f8SBen Goz 	uint32_t num_oac;
5976a1c9510SMoses Reuben 	uint32_t sh_hidden_private_base;
598373d7080SFelix Kuehling 
599373d7080SFelix Kuehling 	/* CWSR memory */
600373d7080SFelix Kuehling 	void *cwsr_kaddr;
601d01994c2SFelix Kuehling 	uint64_t cwsr_base;
602373d7080SFelix Kuehling 	uint64_t tba_addr;
603373d7080SFelix Kuehling 	uint64_t tma_addr;
604d01994c2SFelix Kuehling 
605d01994c2SFelix Kuehling 	/* IB memory */
606d01994c2SFelix Kuehling 	uint64_t ib_base;
607552764b6SFelix Kuehling 	void *ib_kaddr;
608ef568db7SFelix Kuehling 
609ef568db7SFelix Kuehling 	/* doorbell resources per process per device */
610ef568db7SFelix Kuehling 	unsigned long *doorbell_bitmap;
611241f24f8SBen Goz };
612241f24f8SBen Goz 
61326103436SFelix Kuehling /* KFD Memory Eviction */
61426103436SFelix Kuehling 
61526103436SFelix Kuehling /* Approx. wait time before attempting to restore evicted BOs */
61626103436SFelix Kuehling #define PROCESS_RESTORE_TIME_MS 100
61726103436SFelix Kuehling /* Approx. back off time if restore fails due to lack of memory */
61826103436SFelix Kuehling #define PROCESS_BACK_OFF_TIME_MS 100
61926103436SFelix Kuehling /* Approx. time before evicting the process again */
62026103436SFelix Kuehling #define PROCESS_ACTIVE_TIME_MS 10
62126103436SFelix Kuehling 
6225ec7e028SFelix Kuehling /* 8 byte handle containing GPU ID in the most significant 4 bytes and
6235ec7e028SFelix Kuehling  * idr_handle in the least significant 4 bytes
6245ec7e028SFelix Kuehling  */
6255ec7e028SFelix Kuehling #define MAKE_HANDLE(gpu_id, idr_handle) \
6265ec7e028SFelix Kuehling 	(((uint64_t)(gpu_id) << 32) + idr_handle)
6275ec7e028SFelix Kuehling #define GET_GPU_ID(handle) (handle >> 32)
6285ec7e028SFelix Kuehling #define GET_IDR_HANDLE(handle) (handle & 0xFFFFFFFF)
6295ec7e028SFelix Kuehling 
630733fa1f7SYong Zhao enum kfd_pdd_bound {
631733fa1f7SYong Zhao 	PDD_UNBOUND = 0,
632733fa1f7SYong Zhao 	PDD_BOUND,
633733fa1f7SYong Zhao 	PDD_BOUND_SUSPENDED,
634733fa1f7SYong Zhao };
635733fa1f7SYong Zhao 
6364327bed2SPhilip Cox #define MAX_SYSFS_FILENAME_LEN 15
63732cb59f3SMukul Joshi 
63832cb59f3SMukul Joshi /*
63932cb59f3SMukul Joshi  * SDMA counter runs at 100MHz frequency.
64032cb59f3SMukul Joshi  * We display SDMA activity in microsecond granularity in sysfs.
64132cb59f3SMukul Joshi  * As a result, the divisor is 100.
64232cb59f3SMukul Joshi  */
64332cb59f3SMukul Joshi #define SDMA_ACTIVITY_DIVISOR  100
644d4566deeSMukul Joshi 
64519f6d2a6SOded Gabbay /* Data that is per-process-per device. */
64619f6d2a6SOded Gabbay struct kfd_process_device {
64719f6d2a6SOded Gabbay 	/*
64819f6d2a6SOded Gabbay 	 * List of all per-device data for a process.
64919f6d2a6SOded Gabbay 	 * Starts from kfd_process.per_device_data.
65019f6d2a6SOded Gabbay 	 */
65119f6d2a6SOded Gabbay 	struct list_head per_device_list;
65219f6d2a6SOded Gabbay 
65319f6d2a6SOded Gabbay 	/* The device that owns this data. */
65419f6d2a6SOded Gabbay 	struct kfd_dev *dev;
65519f6d2a6SOded Gabbay 
6569fd3f1bfSFelix Kuehling 	/* The process that owns this kfd_process_device. */
6579fd3f1bfSFelix Kuehling 	struct kfd_process *process;
65819f6d2a6SOded Gabbay 
65945102048SBen Goz 	/* per-process-per device QCM data structure */
66045102048SBen Goz 	struct qcm_process_device qpd;
66145102048SBen Goz 
66219f6d2a6SOded Gabbay 	/*Apertures*/
66319f6d2a6SOded Gabbay 	uint64_t lds_base;
66419f6d2a6SOded Gabbay 	uint64_t lds_limit;
66519f6d2a6SOded Gabbay 	uint64_t gpuvm_base;
66619f6d2a6SOded Gabbay 	uint64_t gpuvm_limit;
66719f6d2a6SOded Gabbay 	uint64_t scratch_base;
66819f6d2a6SOded Gabbay 	uint64_t scratch_limit;
66919f6d2a6SOded Gabbay 
670403575c4SFelix Kuehling 	/* VM context for GPUVM allocations */
671b84394e2SFelix Kuehling 	struct file *drm_file;
672403575c4SFelix Kuehling 	void *vm;
673403575c4SFelix Kuehling 
67452b29d73SFelix Kuehling 	/* GPUVM allocations storage */
67552b29d73SFelix Kuehling 	struct idr alloc_idr;
67652b29d73SFelix Kuehling 
6779fd3f1bfSFelix Kuehling 	/* Flag used to tell the pdd has dequeued from the dqm.
6789fd3f1bfSFelix Kuehling 	 * This is used to prevent dev->dqm->ops.process_termination() from
6799fd3f1bfSFelix Kuehling 	 * being called twice when it is already called in IOMMU callback
6809fd3f1bfSFelix Kuehling 	 * function.
681a82918f1SBen Goz 	 */
6829fd3f1bfSFelix Kuehling 	bool already_dequeued;
6839593f4d6SRajneesh Bhardwaj 	bool runtime_inuse;
68464d1c3a4SFelix Kuehling 
68564d1c3a4SFelix Kuehling 	/* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
68664d1c3a4SFelix Kuehling 	enum kfd_pdd_bound bound;
687d4566deeSMukul Joshi 
688d4566deeSMukul Joshi 	/* VRAM usage */
689d4566deeSMukul Joshi 	uint64_t vram_usage;
690d4566deeSMukul Joshi 	struct attribute attr_vram;
69132cb59f3SMukul Joshi 	char vram_filename[MAX_SYSFS_FILENAME_LEN];
69232cb59f3SMukul Joshi 
69332cb59f3SMukul Joshi 	/* SDMA activity tracking */
69432cb59f3SMukul Joshi 	uint64_t sdma_past_activity_counter;
69532cb59f3SMukul Joshi 	struct attribute attr_sdma;
69632cb59f3SMukul Joshi 	char sdma_filename[MAX_SYSFS_FILENAME_LEN];
6974327bed2SPhilip Cox 
6984327bed2SPhilip Cox 	/* Eviction activity tracking */
6994327bed2SPhilip Cox 	uint64_t last_evict_timestamp;
7004327bed2SPhilip Cox 	atomic64_t evict_duration_counter;
7014327bed2SPhilip Cox 	struct attribute attr_evict;
7024327bed2SPhilip Cox 
7034327bed2SPhilip Cox 	struct kobject *kobj_stats;
70459d7115dSMukul Joshi 	unsigned int doorbell_index;
705f2fa07b3SRamesh Errabolu 
706f2fa07b3SRamesh Errabolu 	/*
707f2fa07b3SRamesh Errabolu 	 * @cu_occupancy: Reports occupancy of Compute Units (CU) of a process
708f2fa07b3SRamesh Errabolu 	 * that is associated with device encoded by "this" struct instance. The
709f2fa07b3SRamesh Errabolu 	 * value reflects CU usage by all of the waves launched by this process
710f2fa07b3SRamesh Errabolu 	 * on this device. A very important property of occupancy parameter is
711f2fa07b3SRamesh Errabolu 	 * that its value is a snapshot of current use.
712f2fa07b3SRamesh Errabolu 	 *
713f2fa07b3SRamesh Errabolu 	 * Following is to be noted regarding how this parameter is reported:
714f2fa07b3SRamesh Errabolu 	 *
715f2fa07b3SRamesh Errabolu 	 *  The number of waves that a CU can launch is limited by couple of
716f2fa07b3SRamesh Errabolu 	 *  parameters. These are encoded by struct amdgpu_cu_info instance
717f2fa07b3SRamesh Errabolu 	 *  that is part of every device definition. For GFX9 devices this
718f2fa07b3SRamesh Errabolu 	 *  translates to 40 waves (simd_per_cu * max_waves_per_simd) when waves
719f2fa07b3SRamesh Errabolu 	 *  do not use scratch memory and 32 waves (max_scratch_slots_per_cu)
720f2fa07b3SRamesh Errabolu 	 *  when they do use scratch memory. This could change for future
721f2fa07b3SRamesh Errabolu 	 *  devices and therefore this example should be considered as a guide.
722f2fa07b3SRamesh Errabolu 	 *
723f2fa07b3SRamesh Errabolu 	 *  All CU's of a device are available for the process. This may not be true
724f2fa07b3SRamesh Errabolu 	 *  under certain conditions - e.g. CU masking.
725f2fa07b3SRamesh Errabolu 	 *
726f2fa07b3SRamesh Errabolu 	 *  Finally number of CU's that are occupied by a process is affected by both
727f2fa07b3SRamesh Errabolu 	 *  number of CU's a device has along with number of other competing processes
728f2fa07b3SRamesh Errabolu 	 */
729f2fa07b3SRamesh Errabolu 	struct attribute attr_cu_occupancy;
73019f6d2a6SOded Gabbay };
73119f6d2a6SOded Gabbay 
73252a5fdceSAlexey Skidanov #define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
73352a5fdceSAlexey Skidanov 
7344a488a7aSOded Gabbay /* Process data */
7354a488a7aSOded Gabbay struct kfd_process {
73619f6d2a6SOded Gabbay 	/*
73719f6d2a6SOded Gabbay 	 * kfd_process are stored in an mm_struct*->kfd_process*
73819f6d2a6SOded Gabbay 	 * hash table (kfd_processes in kfd_process.c)
73919f6d2a6SOded Gabbay 	 */
74019f6d2a6SOded Gabbay 	struct hlist_node kfd_processes;
74119f6d2a6SOded Gabbay 
7429b56bb11SFelix Kuehling 	/*
7439b56bb11SFelix Kuehling 	 * Opaque pointer to mm_struct. We don't hold a reference to
7449b56bb11SFelix Kuehling 	 * it so it should never be dereferenced from here. This is
7459b56bb11SFelix Kuehling 	 * only used for looking up processes by their mm.
7469b56bb11SFelix Kuehling 	 */
7479b56bb11SFelix Kuehling 	void *mm;
74819f6d2a6SOded Gabbay 
7495ce10687SFelix Kuehling 	struct kref ref;
7505ce10687SFelix Kuehling 	struct work_struct release_work;
7515ce10687SFelix Kuehling 
75219f6d2a6SOded Gabbay 	struct mutex mutex;
75319f6d2a6SOded Gabbay 
75419f6d2a6SOded Gabbay 	/*
75519f6d2a6SOded Gabbay 	 * In any process, the thread that started main() is the lead
75619f6d2a6SOded Gabbay 	 * thread and outlives the rest.
75719f6d2a6SOded Gabbay 	 * It is here because amd_iommu_bind_pasid wants a task_struct.
758894a8293SFelix Kuehling 	 * It can also be used for safely getting a reference to the
759894a8293SFelix Kuehling 	 * mm_struct of the process.
76019f6d2a6SOded Gabbay 	 */
76119f6d2a6SOded Gabbay 	struct task_struct *lead_thread;
76219f6d2a6SOded Gabbay 
76319f6d2a6SOded Gabbay 	/* We want to receive a notification when the mm_struct is destroyed */
76419f6d2a6SOded Gabbay 	struct mmu_notifier mmu_notifier;
76519f6d2a6SOded Gabbay 
766c7b6bac9SFenghua Yu 	u32 pasid;
76719f6d2a6SOded Gabbay 
76819f6d2a6SOded Gabbay 	/*
76919f6d2a6SOded Gabbay 	 * List of kfd_process_device structures,
77019f6d2a6SOded Gabbay 	 * one for each device the process is using.
77119f6d2a6SOded Gabbay 	 */
77219f6d2a6SOded Gabbay 	struct list_head per_device_data;
77319f6d2a6SOded Gabbay 
77445102048SBen Goz 	struct process_queue_manager pqm;
77545102048SBen Goz 
77619f6d2a6SOded Gabbay 	/*Is the user space process 32 bit?*/
77719f6d2a6SOded Gabbay 	bool is_32bit_user_mode;
778f3a39818SAndrew Lewycky 
779f3a39818SAndrew Lewycky 	/* Event-related data */
780f3a39818SAndrew Lewycky 	struct mutex event_mutex;
781482f0777SFelix Kuehling 	/* Event ID allocator and lookup */
782482f0777SFelix Kuehling 	struct idr event_idr;
78350cb7dd9SFelix Kuehling 	/* Event page */
78450cb7dd9SFelix Kuehling 	struct kfd_signal_page *signal_page;
785b9a5d0a5SFelix Kuehling 	size_t signal_mapped_size;
786f3a39818SAndrew Lewycky 	size_t signal_event_count;
787c986169fSFelix Kuehling 	bool signal_event_limit_reached;
788403575c4SFelix Kuehling 
789403575c4SFelix Kuehling 	/* Information used for memory eviction */
790403575c4SFelix Kuehling 	void *kgd_process_info;
791403575c4SFelix Kuehling 	/* Eviction fence that is attached to all the BOs of this process. The
792403575c4SFelix Kuehling 	 * fence will be triggered during eviction and new one will be created
793403575c4SFelix Kuehling 	 * during restore
794403575c4SFelix Kuehling 	 */
795403575c4SFelix Kuehling 	struct dma_fence *ef;
79626103436SFelix Kuehling 
79726103436SFelix Kuehling 	/* Work items for evicting and restoring BOs */
79826103436SFelix Kuehling 	struct delayed_work eviction_work;
79926103436SFelix Kuehling 	struct delayed_work restore_work;
80026103436SFelix Kuehling 	/* seqno of the last scheduled eviction */
80126103436SFelix Kuehling 	unsigned int last_eviction_seqno;
80226103436SFelix Kuehling 	/* Approx. the last timestamp (in jiffies) when the process was
80326103436SFelix Kuehling 	 * restored after an eviction
80426103436SFelix Kuehling 	 */
80526103436SFelix Kuehling 	unsigned long last_restore_timestamp;
806de9f26bbSKent Russell 
807de9f26bbSKent Russell 	/* Kobj for our procfs */
808de9f26bbSKent Russell 	struct kobject *kobj;
8096d220a7eSAmber Lin 	struct kobject *kobj_queues;
810de9f26bbSKent Russell 	struct attribute attr_pasid;
8114a488a7aSOded Gabbay };
8124a488a7aSOded Gabbay 
81364d1c3a4SFelix Kuehling #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
81464d1c3a4SFelix Kuehling extern DECLARE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
81564d1c3a4SFelix Kuehling extern struct srcu_struct kfd_processes_srcu;
81664d1c3a4SFelix Kuehling 
81776baee6cSOded Gabbay /**
818a4497974SRajneesh Bhardwaj  * typedef amdkfd_ioctl_t - typedef for ioctl function pointer.
81976baee6cSOded Gabbay  *
820a4497974SRajneesh Bhardwaj  * @filep: pointer to file structure.
821a4497974SRajneesh Bhardwaj  * @p: amdkfd process pointer.
822a4497974SRajneesh Bhardwaj  * @data: pointer to arg that was copied from user.
823a4497974SRajneesh Bhardwaj  *
824a4497974SRajneesh Bhardwaj  * Return: returns ioctl completion code.
82576baee6cSOded Gabbay  */
82676baee6cSOded Gabbay typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
82776baee6cSOded Gabbay 				void *data);
82876baee6cSOded Gabbay 
82976baee6cSOded Gabbay struct amdkfd_ioctl_desc {
83076baee6cSOded Gabbay 	unsigned int cmd;
83176baee6cSOded Gabbay 	int flags;
83276baee6cSOded Gabbay 	amdkfd_ioctl_t *func;
83376baee6cSOded Gabbay 	unsigned int cmd_drv;
83476baee6cSOded Gabbay 	const char *name;
83576baee6cSOded Gabbay };
83667f7cf9fSshaoyunl bool kfd_dev_is_large_bar(struct kfd_dev *dev);
83776baee6cSOded Gabbay 
8381679ae8fSFelix Kuehling int kfd_process_create_wq(void);
83919f6d2a6SOded Gabbay void kfd_process_destroy_wq(void);
840373d7080SFelix Kuehling struct kfd_process *kfd_create_process(struct file *filep);
84119f6d2a6SOded Gabbay struct kfd_process *kfd_get_process(const struct task_struct *);
842c7b6bac9SFenghua Yu struct kfd_process *kfd_lookup_process_by_pasid(u32 pasid);
84326103436SFelix Kuehling struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
844abb208a8SFelix Kuehling void kfd_unref_process(struct kfd_process *p);
8456b95e797SFelix Kuehling int kfd_process_evict_queues(struct kfd_process *p);
8466b95e797SFelix Kuehling int kfd_process_restore_queues(struct kfd_process *p);
84726103436SFelix Kuehling void kfd_suspend_all_processes(void);
84826103436SFelix Kuehling int kfd_resume_all_processes(void);
84919f6d2a6SOded Gabbay 
850b84394e2SFelix Kuehling int kfd_process_device_init_vm(struct kfd_process_device *pdd,
851b84394e2SFelix Kuehling 			       struct file *drm_file);
85264c7f8cfSBen Goz struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
85364c7f8cfSBen Goz 						struct kfd_process *p);
85419f6d2a6SOded Gabbay struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
855093c7d8cSAlexey Skidanov 							struct kfd_process *p);
856093c7d8cSAlexey Skidanov struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
857093c7d8cSAlexey Skidanov 							struct kfd_process *p);
85819f6d2a6SOded Gabbay 
859df03ef93SHarish Kasiviswanathan int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process,
860373d7080SFelix Kuehling 			  struct vm_area_struct *vma);
861373d7080SFelix Kuehling 
86252b29d73SFelix Kuehling /* KFD process API for creating and translating handles */
86352b29d73SFelix Kuehling int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
86452b29d73SFelix Kuehling 					void *mem);
86552b29d73SFelix Kuehling void *kfd_process_device_translate_handle(struct kfd_process_device *p,
86652b29d73SFelix Kuehling 					int handle);
86752b29d73SFelix Kuehling void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
86852b29d73SFelix Kuehling 					int handle);
86952b29d73SFelix Kuehling 
870775921edSAlexey Skidanov /* Process device data iterator */
8718eabaf54SKent Russell struct kfd_process_device *kfd_get_first_process_device_data(
8728eabaf54SKent Russell 							struct kfd_process *p);
8738eabaf54SKent Russell struct kfd_process_device *kfd_get_next_process_device_data(
8748eabaf54SKent Russell 						struct kfd_process *p,
875775921edSAlexey Skidanov 						struct kfd_process_device *pdd);
876775921edSAlexey Skidanov bool kfd_has_process_device_data(struct kfd_process *p);
877775921edSAlexey Skidanov 
87819f6d2a6SOded Gabbay /* PASIDs */
87919f6d2a6SOded Gabbay int kfd_pasid_init(void);
88019f6d2a6SOded Gabbay void kfd_pasid_exit(void);
88119f6d2a6SOded Gabbay bool kfd_set_pasid_limit(unsigned int new_limit);
88219f6d2a6SOded Gabbay unsigned int kfd_get_pasid_limit(void);
883c7b6bac9SFenghua Yu u32 kfd_pasid_alloc(void);
884c7b6bac9SFenghua Yu void kfd_pasid_free(u32 pasid);
88519f6d2a6SOded Gabbay 
88619f6d2a6SOded Gabbay /* Doorbells */
887ef568db7SFelix Kuehling size_t kfd_doorbell_process_slice(struct kfd_dev *kfd);
888735df2baSFelix Kuehling int kfd_doorbell_init(struct kfd_dev *kfd);
889735df2baSFelix Kuehling void kfd_doorbell_fini(struct kfd_dev *kfd);
890df03ef93SHarish Kasiviswanathan int kfd_doorbell_mmap(struct kfd_dev *dev, struct kfd_process *process,
891df03ef93SHarish Kasiviswanathan 		      struct vm_area_struct *vma);
892ada2b29cSFelix Kuehling void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
89319f6d2a6SOded Gabbay 					unsigned int *doorbell_off);
89419f6d2a6SOded Gabbay void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
89519f6d2a6SOded Gabbay u32 read_kernel_doorbell(u32 __iomem *db);
896ada2b29cSFelix Kuehling void write_kernel_doorbell(void __iomem *db, u32 value);
8979d7d0248SFelix Kuehling void write_kernel_doorbell64(void __iomem *db, u64 value);
898339903faSYong Zhao unsigned int kfd_get_doorbell_dw_offset_in_bar(struct kfd_dev *kfd,
89959d7115dSMukul Joshi 					struct kfd_process_device *pdd,
900ef568db7SFelix Kuehling 					unsigned int doorbell_id);
90159d7115dSMukul Joshi phys_addr_t kfd_get_process_doorbells(struct kfd_process_device *pdd);
90259d7115dSMukul Joshi int kfd_alloc_process_doorbells(struct kfd_dev *kfd,
90359d7115dSMukul Joshi 				unsigned int *doorbell_index);
90459d7115dSMukul Joshi void kfd_free_process_doorbells(struct kfd_dev *kfd,
90559d7115dSMukul Joshi 				unsigned int doorbell_index);
9066e81090bSOded Gabbay /* GTT Sub-Allocator */
9076e81090bSOded Gabbay 
9086e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
9096e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj);
9106e81090bSOded Gabbay 
9116e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj);
9126e81090bSOded Gabbay 
9134a488a7aSOded Gabbay extern struct device *kfd_device;
9144a488a7aSOded Gabbay 
915de9f26bbSKent Russell /* KFD's procfs */
916de9f26bbSKent Russell void kfd_procfs_init(void);
917de9f26bbSKent Russell void kfd_procfs_shutdown(void);
9186d220a7eSAmber Lin int kfd_procfs_add_queue(struct queue *q);
9196d220a7eSAmber Lin void kfd_procfs_del_queue(struct queue *q);
920de9f26bbSKent Russell 
9215b5c4e40SEvgeny Pinchuk /* Topology */
9225b5c4e40SEvgeny Pinchuk int kfd_topology_init(void);
9235b5c4e40SEvgeny Pinchuk void kfd_topology_shutdown(void);
9245b5c4e40SEvgeny Pinchuk int kfd_topology_add_device(struct kfd_dev *gpu);
9255b5c4e40SEvgeny Pinchuk int kfd_topology_remove_device(struct kfd_dev *gpu);
9263a87177eSHarish Kasiviswanathan struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
9273a87177eSHarish Kasiviswanathan 						uint32_t proximity_domain);
92844d8cc6fSYong Zhao struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id);
9295b5c4e40SEvgeny Pinchuk struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
9305b5c4e40SEvgeny Pinchuk struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
9311dde0ea9SFelix Kuehling struct kfd_dev *kfd_device_by_kgd(const struct kgd_dev *kgd);
9326d82eb0eSHarish Kasiviswanathan int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_dev **kdev);
933520b8fb7SFelix Kuehling int kfd_numa_node_to_apic_id(int numa_node_id);
9346127896fSHuang Rui void kfd_double_confirm_iommu_support(struct kfd_dev *gpu);
9355b5c4e40SEvgeny Pinchuk 
9364a488a7aSOded Gabbay /* Interrupts */
9372249d558SAndrew Lewycky int kfd_interrupt_init(struct kfd_dev *dev);
9382249d558SAndrew Lewycky void kfd_interrupt_exit(struct kfd_dev *dev);
9392249d558SAndrew Lewycky bool enqueue_ih_ring_entry(struct kfd_dev *kfd,	const void *ih_ring_entry);
94058e69886SLan Xiao bool interrupt_is_wanted(struct kfd_dev *dev,
94158e69886SLan Xiao 				const uint32_t *ih_ring_entry,
94258e69886SLan Xiao 				uint32_t *patched_ihre, bool *flag);
9434a488a7aSOded Gabbay 
94419f6d2a6SOded Gabbay /* amdkfd Apertures */
94519f6d2a6SOded Gabbay int kfd_init_apertures(struct kfd_process *process);
94619f6d2a6SOded Gabbay 
947*7c9631afSJay Cornwall void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
948*7c9631afSJay Cornwall 				  uint64_t tba_addr,
949*7c9631afSJay Cornwall 				  uint64_t tma_addr);
950*7c9631afSJay Cornwall 
951ed6e6a34SBen Goz /* Queue Context Management */
952e88a614cSEdward O'Callaghan int init_queue(struct queue **q, const struct queue_properties *properties);
953ed6e6a34SBen Goz void uninit_queue(struct queue *q);
95445102048SBen Goz void print_queue_properties(struct queue_properties *q);
955ed6e6a34SBen Goz void print_queue(struct queue *q);
956ed6e6a34SBen Goz 
9574b8f589bSBen Goz struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
9584b8f589bSBen Goz 		struct kfd_dev *dev);
959ee04955aSFelix Kuehling struct mqd_manager *mqd_manager_init_cik_hawaii(enum KFD_MQD_TYPE type,
960ee04955aSFelix Kuehling 		struct kfd_dev *dev);
9614b8f589bSBen Goz struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
9624b8f589bSBen Goz 		struct kfd_dev *dev);
963ee04955aSFelix Kuehling struct mqd_manager *mqd_manager_init_vi_tonga(enum KFD_MQD_TYPE type,
964ee04955aSFelix Kuehling 		struct kfd_dev *dev);
965b91d43ddSFelix Kuehling struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type,
966b91d43ddSFelix Kuehling 		struct kfd_dev *dev);
96714328aa5SPhilip Cox struct mqd_manager *mqd_manager_init_v10(enum KFD_MQD_TYPE type,
96814328aa5SPhilip Cox 		struct kfd_dev *dev);
96964c7f8cfSBen Goz struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev);
97064c7f8cfSBen Goz void device_queue_manager_uninit(struct device_queue_manager *dqm);
971241f24f8SBen Goz struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
972241f24f8SBen Goz 					enum kfd_queue_type type);
973c2a77fdeSFelix Kuehling void kernel_queue_uninit(struct kernel_queue *kq, bool hanging);
974c7b6bac9SFenghua Yu int kfd_process_vm_fault(struct device_queue_manager *dqm, u32 pasid);
975241f24f8SBen Goz 
97645102048SBen Goz /* Process Queue Manager */
97745102048SBen Goz struct process_queue_node {
97845102048SBen Goz 	struct queue *q;
97945102048SBen Goz 	struct kernel_queue *kq;
98045102048SBen Goz 	struct list_head process_queue_list;
98145102048SBen Goz };
98245102048SBen Goz 
9839fd3f1bfSFelix Kuehling void kfd_process_dequeue_from_device(struct kfd_process_device *pdd);
9849fd3f1bfSFelix Kuehling void kfd_process_dequeue_from_all_devices(struct kfd_process *p);
98545102048SBen Goz int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
98645102048SBen Goz void pqm_uninit(struct process_queue_manager *pqm);
98745102048SBen Goz int pqm_create_queue(struct process_queue_manager *pqm,
98845102048SBen Goz 			    struct kfd_dev *dev,
98945102048SBen Goz 			    struct file *f,
99045102048SBen Goz 			    struct queue_properties *properties,
991e47a8b52SYong Zhao 			    unsigned int *qid,
992e47a8b52SYong Zhao 			    uint32_t *p_doorbell_offset_in_process);
99345102048SBen Goz int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
99445102048SBen Goz int pqm_update_queue(struct process_queue_manager *pqm, unsigned int qid,
99545102048SBen Goz 			struct queue_properties *p);
99639e7f331SFelix Kuehling int pqm_set_cu_mask(struct process_queue_manager *pqm, unsigned int qid,
99739e7f331SFelix Kuehling 			struct queue_properties *p);
998eb82da1dSOak Zeng int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
999eb82da1dSOak Zeng 			void *gws);
1000fbeb661bSYair Shachar struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
1001fbeb661bSYair Shachar 						unsigned int qid);
10025bb4b78bSOak Zeng struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
10035bb4b78bSOak Zeng 						unsigned int qid);
10045df099e8SJay Cornwall int pqm_get_wave_state(struct process_queue_manager *pqm,
10055df099e8SJay Cornwall 		       unsigned int qid,
10065df099e8SJay Cornwall 		       void __user *ctl_stack,
10075df099e8SJay Cornwall 		       u32 *ctl_stack_used_size,
10085df099e8SJay Cornwall 		       u32 *save_area_used_size);
100945102048SBen Goz 
1010788bf83dSYair Shachar int amdkfd_fence_wait_timeout(unsigned int *fence_addr,
1011788bf83dSYair Shachar 			      unsigned int fence_value,
10128c72c3d7SYong Zhao 			      unsigned int timeout_ms);
1013788bf83dSYair Shachar 
1014ed6e6a34SBen Goz /* Packet Manager */
1015ed6e6a34SBen Goz 
101664c7f8cfSBen Goz #define KFD_FENCE_COMPLETED (100)
101764c7f8cfSBen Goz #define KFD_FENCE_INIT   (10)
1018241f24f8SBen Goz 
1019ed6e6a34SBen Goz struct packet_manager {
1020ed6e6a34SBen Goz 	struct device_queue_manager *dqm;
1021ed6e6a34SBen Goz 	struct kernel_queue *priv_queue;
1022ed6e6a34SBen Goz 	struct mutex lock;
1023ed6e6a34SBen Goz 	bool allocated;
1024ed6e6a34SBen Goz 	struct kfd_mem_obj *ib_buffer_obj;
1025851a645eSFelix Kuehling 	unsigned int ib_size_bytes;
1026819ec5acSFelix Kuehling 	bool is_over_subscription;
1027f6e27ff1SFelix Kuehling 
1028f6e27ff1SFelix Kuehling 	const struct packet_manager_funcs *pmf;
1029ed6e6a34SBen Goz };
1030ed6e6a34SBen Goz 
1031f6e27ff1SFelix Kuehling struct packet_manager_funcs {
1032f6e27ff1SFelix Kuehling 	/* Support ASIC-specific packet formats for PM4 packets */
1033f6e27ff1SFelix Kuehling 	int (*map_process)(struct packet_manager *pm, uint32_t *buffer,
1034f6e27ff1SFelix Kuehling 			struct qcm_process_device *qpd);
1035f6e27ff1SFelix Kuehling 	int (*runlist)(struct packet_manager *pm, uint32_t *buffer,
1036f6e27ff1SFelix Kuehling 			uint64_t ib, size_t ib_size_in_dwords, bool chain);
1037f6e27ff1SFelix Kuehling 	int (*set_resources)(struct packet_manager *pm, uint32_t *buffer,
1038f6e27ff1SFelix Kuehling 			struct scheduling_resources *res);
1039f6e27ff1SFelix Kuehling 	int (*map_queues)(struct packet_manager *pm, uint32_t *buffer,
1040f6e27ff1SFelix Kuehling 			struct queue *q, bool is_static);
1041f6e27ff1SFelix Kuehling 	int (*unmap_queues)(struct packet_manager *pm, uint32_t *buffer,
1042f6e27ff1SFelix Kuehling 			enum kfd_queue_type type,
1043f6e27ff1SFelix Kuehling 			enum kfd_unmap_queues_filter mode,
1044f6e27ff1SFelix Kuehling 			uint32_t filter_param, bool reset,
1045f6e27ff1SFelix Kuehling 			unsigned int sdma_engine);
1046f6e27ff1SFelix Kuehling 	int (*query_status)(struct packet_manager *pm, uint32_t *buffer,
1047f6e27ff1SFelix Kuehling 			uint64_t fence_address,	uint32_t fence_value);
1048f6e27ff1SFelix Kuehling 	int (*release_mem)(uint64_t gpu_addr, uint32_t *buffer);
1049f6e27ff1SFelix Kuehling 
1050f6e27ff1SFelix Kuehling 	/* Packet sizes */
1051f6e27ff1SFelix Kuehling 	int map_process_size;
1052f6e27ff1SFelix Kuehling 	int runlist_size;
1053f6e27ff1SFelix Kuehling 	int set_resources_size;
1054f6e27ff1SFelix Kuehling 	int map_queues_size;
1055f6e27ff1SFelix Kuehling 	int unmap_queues_size;
1056f6e27ff1SFelix Kuehling 	int query_status_size;
1057f6e27ff1SFelix Kuehling 	int release_mem_size;
1058f6e27ff1SFelix Kuehling };
1059f6e27ff1SFelix Kuehling 
1060f6e27ff1SFelix Kuehling extern const struct packet_manager_funcs kfd_vi_pm_funcs;
1061454150b1SFelix Kuehling extern const struct packet_manager_funcs kfd_v9_pm_funcs;
1062f6e27ff1SFelix Kuehling 
106364c7f8cfSBen Goz int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
1064c2a77fdeSFelix Kuehling void pm_uninit(struct packet_manager *pm, bool hanging);
106564c7f8cfSBen Goz int pm_send_set_resources(struct packet_manager *pm,
106664c7f8cfSBen Goz 				struct scheduling_resources *res);
106764c7f8cfSBen Goz int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
106864c7f8cfSBen Goz int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
106964c7f8cfSBen Goz 				uint32_t fence_value);
107064c7f8cfSBen Goz 
107164c7f8cfSBen Goz int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
10727da2bcf8SYong Zhao 			enum kfd_unmap_queues_filter mode,
107364c7f8cfSBen Goz 			uint32_t filter_param, bool reset,
107464c7f8cfSBen Goz 			unsigned int sdma_engine);
107564c7f8cfSBen Goz 
1076241f24f8SBen Goz void pm_release_ib(struct packet_manager *pm);
1077241f24f8SBen Goz 
1078454150b1SFelix Kuehling /* Following PM funcs can be shared among VI and AI */
1079454150b1SFelix Kuehling unsigned int pm_build_pm4_header(unsigned int opcode, size_t packet_size);
108014328aa5SPhilip Cox 
108119f6d2a6SOded Gabbay uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
108219f6d2a6SOded Gabbay 
1083f3a39818SAndrew Lewycky /* Events */
1084f3a39818SAndrew Lewycky extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
1085ca750681SFelix Kuehling extern const struct kfd_event_interrupt_class event_interrupt_class_v9;
1086ca750681SFelix Kuehling 
1087930c5ff4SAlexey Skidanov extern const struct kfd_device_global_init_class device_global_init_class_cik;
1088f3a39818SAndrew Lewycky 
1089f3a39818SAndrew Lewycky void kfd_event_init_process(struct kfd_process *p);
1090f3a39818SAndrew Lewycky void kfd_event_free_process(struct kfd_process *p);
1091f3a39818SAndrew Lewycky int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
1092f3a39818SAndrew Lewycky int kfd_wait_on_events(struct kfd_process *p,
109359d3e8beSAlexey Skidanov 		       uint32_t num_events, void __user *data,
1094f3a39818SAndrew Lewycky 		       bool all, uint32_t user_timeout_ms,
1095fdf0c833SFelix Kuehling 		       uint32_t *wait_result);
1096c7b6bac9SFenghua Yu void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
1097f3a39818SAndrew Lewycky 				uint32_t valid_id_bits);
109859d3e8beSAlexey Skidanov void kfd_signal_iommu_event(struct kfd_dev *dev,
1099c7b6bac9SFenghua Yu 			    u32 pasid, unsigned long address,
110059d3e8beSAlexey Skidanov 			    bool is_write_requested, bool is_execute_requested);
1101c7b6bac9SFenghua Yu void kfd_signal_hw_exception_event(u32 pasid);
1102f3a39818SAndrew Lewycky int kfd_set_event(struct kfd_process *p, uint32_t event_id);
1103f3a39818SAndrew Lewycky int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
11040fc8011fSFelix Kuehling int kfd_event_page_set(struct kfd_process *p, void *kernel_address,
11050fc8011fSFelix Kuehling 		       uint64_t size);
1106f3a39818SAndrew Lewycky int kfd_event_create(struct file *devkfd, struct kfd_process *p,
1107f3a39818SAndrew Lewycky 		     uint32_t event_type, bool auto_reset, uint32_t node_id,
1108f3a39818SAndrew Lewycky 		     uint32_t *event_id, uint32_t *event_trigger_data,
1109f3a39818SAndrew Lewycky 		     uint64_t *event_page_offset, uint32_t *event_slot_index);
1110f3a39818SAndrew Lewycky int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);
1111f3a39818SAndrew Lewycky 
1112c7b6bac9SFenghua Yu void kfd_signal_vm_fault_event(struct kfd_dev *dev, u32 pasid,
11132640c3faSshaoyunl 				struct kfd_vm_fault_info *info);
11142640c3faSshaoyunl 
1115e42051d2SShaoyun Liu void kfd_signal_reset_event(struct kfd_dev *dev);
1116e42051d2SShaoyun Liu 
1117403575c4SFelix Kuehling void kfd_flush_tlb(struct kfd_process_device *pdd);
1118403575c4SFelix Kuehling 
1119c3447e81SBen Goz int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p);
1120c3447e81SBen Goz 
1121e42051d2SShaoyun Liu bool kfd_is_locked(void);
1122e42051d2SShaoyun Liu 
1123f756e631SHarish Kasiviswanathan /* Compute profile */
1124f756e631SHarish Kasiviswanathan void kfd_inc_compute_active(struct kfd_dev *dev);
1125f756e631SHarish Kasiviswanathan void kfd_dec_compute_active(struct kfd_dev *dev);
1126f756e631SHarish Kasiviswanathan 
11276b855f7bSHarish Kasiviswanathan /* Cgroup Support */
11286b855f7bSHarish Kasiviswanathan /* Check with device cgroup if @kfd device is accessible */
11296b855f7bSHarish Kasiviswanathan static inline int kfd_devcgroup_check_permission(struct kfd_dev *kfd)
11306b855f7bSHarish Kasiviswanathan {
1131eec8fd02SOdin Ugedal #if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)
11326b855f7bSHarish Kasiviswanathan 	struct drm_device *ddev = kfd->ddev;
11336b855f7bSHarish Kasiviswanathan 
113499c7b309SLorenz Brun 	return devcgroup_check_permission(DEVCG_DEV_CHAR, DRM_MAJOR,
11356b855f7bSHarish Kasiviswanathan 					  ddev->render->index,
11366b855f7bSHarish Kasiviswanathan 					  DEVCG_ACC_WRITE | DEVCG_ACC_READ);
11376b855f7bSHarish Kasiviswanathan #else
11386b855f7bSHarish Kasiviswanathan 	return 0;
11396b855f7bSHarish Kasiviswanathan #endif
11406b855f7bSHarish Kasiviswanathan }
11416b855f7bSHarish Kasiviswanathan 
1142851a645eSFelix Kuehling /* Debugfs */
1143851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
1144851a645eSFelix Kuehling 
1145851a645eSFelix Kuehling void kfd_debugfs_init(void);
1146851a645eSFelix Kuehling void kfd_debugfs_fini(void);
1147851a645eSFelix Kuehling int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data);
1148851a645eSFelix Kuehling int pqm_debugfs_mqds(struct seq_file *m, void *data);
1149851a645eSFelix Kuehling int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data);
1150851a645eSFelix Kuehling int dqm_debugfs_hqds(struct seq_file *m, void *data);
1151851a645eSFelix Kuehling int kfd_debugfs_rls_by_device(struct seq_file *m, void *data);
1152851a645eSFelix Kuehling int pm_debugfs_runlist(struct seq_file *m, void *data);
1153851a645eSFelix Kuehling 
1154a29ec470SShaoyun Liu int kfd_debugfs_hang_hws(struct kfd_dev *dev);
1155a29ec470SShaoyun Liu int pm_debugfs_hang_hws(struct packet_manager *pm);
1156a29ec470SShaoyun Liu int dqm_debugfs_execute_queues(struct device_queue_manager *dqm);
1157a29ec470SShaoyun Liu 
1158851a645eSFelix Kuehling #else
1159851a645eSFelix Kuehling 
1160851a645eSFelix Kuehling static inline void kfd_debugfs_init(void) {}
1161851a645eSFelix Kuehling static inline void kfd_debugfs_fini(void) {}
1162851a645eSFelix Kuehling 
1163851a645eSFelix Kuehling #endif
1164851a645eSFelix Kuehling 
11654a488a7aSOded Gabbay #endif
1166