xref: /openbmc/linux/drivers/gpu/drm/amd/amdkfd/kfd_priv.h (revision 26843936)
1d87f36a0SRajneesh Bhardwaj /* SPDX-License-Identifier: GPL-2.0 OR MIT */
24a488a7aSOded Gabbay /*
3d87f36a0SRajneesh Bhardwaj  * Copyright 2014-2022 Advanced Micro Devices, Inc.
44a488a7aSOded Gabbay  *
54a488a7aSOded Gabbay  * Permission is hereby granted, free of charge, to any person obtaining a
64a488a7aSOded Gabbay  * copy of this software and associated documentation files (the "Software"),
74a488a7aSOded Gabbay  * to deal in the Software without restriction, including without limitation
84a488a7aSOded Gabbay  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
94a488a7aSOded Gabbay  * and/or sell copies of the Software, and to permit persons to whom the
104a488a7aSOded Gabbay  * Software is furnished to do so, subject to the following conditions:
114a488a7aSOded Gabbay  *
124a488a7aSOded Gabbay  * The above copyright notice and this permission notice shall be included in
134a488a7aSOded Gabbay  * all copies or substantial portions of the Software.
144a488a7aSOded Gabbay  *
154a488a7aSOded Gabbay  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
164a488a7aSOded Gabbay  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174a488a7aSOded Gabbay  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
184a488a7aSOded Gabbay  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
194a488a7aSOded Gabbay  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
204a488a7aSOded Gabbay  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
214a488a7aSOded Gabbay  * OTHER DEALINGS IN THE SOFTWARE.
224a488a7aSOded Gabbay  */
234a488a7aSOded Gabbay 
244a488a7aSOded Gabbay #ifndef KFD_PRIV_H_INCLUDED
254a488a7aSOded Gabbay #define KFD_PRIV_H_INCLUDED
264a488a7aSOded Gabbay 
274a488a7aSOded Gabbay #include <linux/hashtable.h>
284a488a7aSOded Gabbay #include <linux/mmu_notifier.h>
29dc90f084SChristoph Hellwig #include <linux/memremap.h>
304a488a7aSOded Gabbay #include <linux/mutex.h>
314a488a7aSOded Gabbay #include <linux/types.h>
324a488a7aSOded Gabbay #include <linux/atomic.h>
334a488a7aSOded Gabbay #include <linux/workqueue.h>
344a488a7aSOded Gabbay #include <linux/spinlock.h>
3519f6d2a6SOded Gabbay #include <linux/kfd_ioctl.h>
36482f0777SFelix Kuehling #include <linux/idr.h>
3704ad47bdSAndres Rodriguez #include <linux/kfifo.h>
38851a645eSFelix Kuehling #include <linux/seq_file.h>
395ce10687SFelix Kuehling #include <linux/kref.h>
40de9f26bbSKent Russell #include <linux/sysfs.h>
416b855f7bSHarish Kasiviswanathan #include <linux/device_cgroup.h>
421cd4d9eeSStephen Rothwell #include <drm/drm_file.h>
431cd4d9eeSStephen Rothwell #include <drm/drm_drv.h>
441cd4d9eeSStephen Rothwell #include <drm/drm_device.h>
4599c7b309SLorenz Brun #include <drm/drm_ioctl.h>
464a488a7aSOded Gabbay #include <kgd_kfd_interface.h>
476d220a7eSAmber Lin #include <linux/swap.h>
484a488a7aSOded Gabbay 
49e596b903SYong Zhao #include "amd_shared.h"
506ae27841SAlex Sierra #include "amdgpu.h"
51e596b903SYong Zhao 
52af47b390SLaura Abbott #define KFD_MAX_RING_ENTRY_SIZE	8
53af47b390SLaura Abbott 
545b5c4e40SEvgeny Pinchuk #define KFD_SYSFS_FILE_MODE 0444
555b5c4e40SEvgeny Pinchuk 
56df03ef93SHarish Kasiviswanathan /* GPU ID hash width in bits */
57df03ef93SHarish Kasiviswanathan #define KFD_GPU_ID_HASH_WIDTH 16
58df03ef93SHarish Kasiviswanathan 
59df03ef93SHarish Kasiviswanathan /* Use upper bits of mmap offset to store KFD driver specific information.
60df03ef93SHarish Kasiviswanathan  * BITS[63:62] - Encode MMAP type
61df03ef93SHarish Kasiviswanathan  * BITS[61:46] - Encode gpu_id. To identify to which GPU the offset belongs to
62df03ef93SHarish Kasiviswanathan  * BITS[45:0]  - MMAP offset value
63df03ef93SHarish Kasiviswanathan  *
64df03ef93SHarish Kasiviswanathan  * NOTE: struct vm_area_struct.vm_pgoff uses offset in pages. Hence, these
65df03ef93SHarish Kasiviswanathan  *  defines are w.r.t to PAGE_SIZE
66df03ef93SHarish Kasiviswanathan  */
6729453755SYong Zhao #define KFD_MMAP_TYPE_SHIFT	62
68df03ef93SHarish Kasiviswanathan #define KFD_MMAP_TYPE_MASK	(0x3ULL << KFD_MMAP_TYPE_SHIFT)
69df03ef93SHarish Kasiviswanathan #define KFD_MMAP_TYPE_DOORBELL	(0x3ULL << KFD_MMAP_TYPE_SHIFT)
70df03ef93SHarish Kasiviswanathan #define KFD_MMAP_TYPE_EVENTS	(0x2ULL << KFD_MMAP_TYPE_SHIFT)
71df03ef93SHarish Kasiviswanathan #define KFD_MMAP_TYPE_RESERVED_MEM	(0x1ULL << KFD_MMAP_TYPE_SHIFT)
72d33ea570SOak Zeng #define KFD_MMAP_TYPE_MMIO	(0x0ULL << KFD_MMAP_TYPE_SHIFT)
73df03ef93SHarish Kasiviswanathan 
7429453755SYong Zhao #define KFD_MMAP_GPU_ID_SHIFT 46
75df03ef93SHarish Kasiviswanathan #define KFD_MMAP_GPU_ID_MASK (((1ULL << KFD_GPU_ID_HASH_WIDTH) - 1) \
76df03ef93SHarish Kasiviswanathan 				<< KFD_MMAP_GPU_ID_SHIFT)
77df03ef93SHarish Kasiviswanathan #define KFD_MMAP_GPU_ID(gpu_id) ((((uint64_t)gpu_id) << KFD_MMAP_GPU_ID_SHIFT)\
78df03ef93SHarish Kasiviswanathan 				& KFD_MMAP_GPU_ID_MASK)
7929453755SYong Zhao #define KFD_MMAP_GET_GPU_ID(offset)    ((offset & KFD_MMAP_GPU_ID_MASK) \
80df03ef93SHarish Kasiviswanathan 				>> KFD_MMAP_GPU_ID_SHIFT)
81df03ef93SHarish Kasiviswanathan 
82ed6e6a34SBen Goz /*
83ed6e6a34SBen Goz  * When working with cp scheduler we should assign the HIQ manually or via
84e7016d8eSYong Zhao  * the amdgpu driver to a fixed hqd slot, here are the fixed HIQ hqd slot
85ed6e6a34SBen Goz  * definitions for Kaveri. In Kaveri only the first ME queues participates
86ed6e6a34SBen Goz  * in the cp scheduling taking that in mind we set the HIQ slot in the
87ed6e6a34SBen Goz  * second ME.
88ed6e6a34SBen Goz  */
89ed6e6a34SBen Goz #define KFD_CIK_HIQ_PIPE 4
90ed6e6a34SBen Goz #define KFD_CIK_HIQ_QUEUE 0
91ed6e6a34SBen Goz 
925b5c4e40SEvgeny Pinchuk /* Macro for allocating structures */
935b5c4e40SEvgeny Pinchuk #define kfd_alloc_struct(ptr_to_struct)	\
945b5c4e40SEvgeny Pinchuk 	((typeof(ptr_to_struct)) kzalloc(sizeof(*ptr_to_struct), GFP_KERNEL))
955b5c4e40SEvgeny Pinchuk 
9619f6d2a6SOded Gabbay #define KFD_MAX_NUM_OF_PROCESSES 512
97b8cbab04SOded Gabbay #define KFD_MAX_NUM_OF_QUEUES_PER_PROCESS 1024
9819f6d2a6SOded Gabbay 
9919f6d2a6SOded Gabbay /*
100373d7080SFelix Kuehling  * Size of the per-process TBA+TMA buffer: 2 pages
101373d7080SFelix Kuehling  *
102373d7080SFelix Kuehling  * The first page is the TBA used for the CWSR ISA code. The second
103a4497974SRajneesh Bhardwaj  * page is used as TMA for user-mode trap handler setup in daisy-chain mode.
104373d7080SFelix Kuehling  */
105373d7080SFelix Kuehling #define KFD_CWSR_TBA_TMA_SIZE (PAGE_SIZE * 2)
106373d7080SFelix Kuehling #define KFD_CWSR_TMA_OFFSET PAGE_SIZE
107373d7080SFelix Kuehling 
10874523943SYong Zhao #define KFD_MAX_NUM_OF_QUEUES_PER_DEVICE		\
10974523943SYong Zhao 	(KFD_MAX_NUM_OF_PROCESSES *			\
11074523943SYong Zhao 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)
11174523943SYong Zhao 
11274523943SYong Zhao #define KFD_KERNEL_QUEUE_SIZE 2048
11374523943SYong Zhao 
11414328aa5SPhilip Cox #define KFD_UNMAP_LATENCY_MS	(4000)
11514328aa5SPhilip Cox 
116a805889aSMukul Joshi #define KFD_MAX_SDMA_QUEUES	128
117a805889aSMukul Joshi 
118373d7080SFelix Kuehling /*
1191f86805aSYong Zhao  * 512 = 0x200
1201f86805aSYong Zhao  * The doorbell index distance between SDMA RLC (2*i) and (2*i+1) in the
1211f86805aSYong Zhao  * same SDMA engine on SOC15, which has 8-byte doorbells for SDMA.
1221f86805aSYong Zhao  * 512 8-byte doorbell distance (i.e. one page away) ensures that SDMA RLC
1231f86805aSYong Zhao  * (2*i+1) doorbells (in terms of the lower 12 bit address) lie exactly in
1241f86805aSYong Zhao  * the OFFSET and SIZE set in registers like BIF_SDMA0_DOORBELL_RANGE.
1251f86805aSYong Zhao  */
1261f86805aSYong Zhao #define KFD_QUEUE_DOORBELL_MIRROR_OFFSET 512
1271f86805aSYong Zhao 
12836988070SRajneesh Bhardwaj /**
12936988070SRajneesh Bhardwaj  * enum kfd_ioctl_flags - KFD ioctl flags
13036988070SRajneesh Bhardwaj  * Various flags that can be set in &amdkfd_ioctl_desc.flags to control how
13136988070SRajneesh Bhardwaj  * userspace can use a given ioctl.
13236988070SRajneesh Bhardwaj  */
13336988070SRajneesh Bhardwaj enum kfd_ioctl_flags {
13436988070SRajneesh Bhardwaj 	/*
13536988070SRajneesh Bhardwaj 	 * @KFD_IOC_FLAG_CHECKPOINT_RESTORE:
13636988070SRajneesh Bhardwaj 	 * Certain KFD ioctls such as AMDKFD_IOC_CRIU_OP can potentially
13736988070SRajneesh Bhardwaj 	 * perform privileged operations and load arbitrary data into MQDs and
13836988070SRajneesh Bhardwaj 	 * eventually HQD registers when the queue is mapped by HWS. In order to
13936988070SRajneesh Bhardwaj 	 * prevent this we should perform additional security checks.
14036988070SRajneesh Bhardwaj 	 *
14136988070SRajneesh Bhardwaj 	 * This is equivalent to callers with the CHECKPOINT_RESTORE capability.
14236988070SRajneesh Bhardwaj 	 *
14336988070SRajneesh Bhardwaj 	 * Note: Since earlier versions of docker do not support CHECKPOINT_RESTORE,
14436988070SRajneesh Bhardwaj 	 * we also allow ioctls with SYS_ADMIN capability.
14536988070SRajneesh Bhardwaj 	 */
14636988070SRajneesh Bhardwaj 	KFD_IOC_FLAG_CHECKPOINT_RESTORE = BIT(0),
14736988070SRajneesh Bhardwaj };
1481f86805aSYong Zhao /*
149b8cbab04SOded Gabbay  * Kernel module parameter to specify maximum number of supported queues per
150b8cbab04SOded Gabbay  * device
15119f6d2a6SOded Gabbay  */
152b8cbab04SOded Gabbay extern int max_num_of_queues_per_device;
15319f6d2a6SOded Gabbay 
154ed6e6a34SBen Goz 
15531c21fecSBen Goz /* Kernel module parameter to specify the scheduling policy */
15631c21fecSBen Goz extern int sched_policy;
15731c21fecSBen Goz 
158a99c6d4fSFelix Kuehling /*
159a99c6d4fSFelix Kuehling  * Kernel module parameter to specify the maximum process
160a99c6d4fSFelix Kuehling  * number per HW scheduler
161a99c6d4fSFelix Kuehling  */
162a99c6d4fSFelix Kuehling extern int hws_max_conc_proc;
163a99c6d4fSFelix Kuehling 
164373d7080SFelix Kuehling extern int cwsr_enable;
165373d7080SFelix Kuehling 
16681663016SOded Gabbay /*
16781663016SOded Gabbay  * Kernel module parameter to specify whether to send sigterm to HSA process on
16881663016SOded Gabbay  * unhandled exception
16981663016SOded Gabbay  */
17081663016SOded Gabbay extern int send_sigterm;
17181663016SOded Gabbay 
172ebcfd1e2SFelix Kuehling /*
173374200b1SFelix Kuehling  * This kernel module is used to simulate large bar machine on non-large bar
174374200b1SFelix Kuehling  * enabled machines.
175374200b1SFelix Kuehling  */
176374200b1SFelix Kuehling extern int debug_largebar;
177374200b1SFelix Kuehling 
178a4497974SRajneesh Bhardwaj /* Set sh_mem_config.retry_disable on GFX v9 */
17975ee6487SFelix Kuehling extern int amdgpu_noretry;
180bed4f110SFelix Kuehling 
181a4497974SRajneesh Bhardwaj /* Halt if HWS hang is detected */
1820e9a860cSYong Zhao extern int halt_if_hws_hang;
1830e9a860cSYong Zhao 
184a4497974SRajneesh Bhardwaj /* Whether MEC FW support GWS barriers */
18529e76462SOak Zeng extern bool hws_gws_support;
18629e76462SOak Zeng 
187a4497974SRajneesh Bhardwaj /* Queue preemption timeout in ms */
18814328aa5SPhilip Cox extern int queue_preemption_timeout_ms;
18914328aa5SPhilip Cox 
1906d909c5dSOak Zeng /*
1916d909c5dSOak Zeng  * Don't evict process queues on vm fault
1926d909c5dSOak Zeng  */
1936d909c5dSOak Zeng extern int amdgpu_no_queue_eviction_on_vm_fault;
1946d909c5dSOak Zeng 
195a4497974SRajneesh Bhardwaj /* Enable eviction debug messages */
196b2057956SFelix Kuehling extern bool debug_evictions;
197b2057956SFelix Kuehling 
198fe1f05dfSMukul Joshi extern struct mutex kfd_processes_mutex;
199fe1f05dfSMukul Joshi 
200ed6e6a34SBen Goz enum cache_policy {
201ed6e6a34SBen Goz 	cache_policy_coherent,
202ed6e6a34SBen Goz 	cache_policy_noncoherent
203ed6e6a34SBen Goz };
204ed6e6a34SBen Goz 
205dd0ae064SGraham Sider #define KFD_GC_VERSION(dev) ((dev)->adev->ip_versions[GC_HWIP][0])
206dd0ae064SGraham Sider #define KFD_IS_SOC15(dev)   ((KFD_GC_VERSION(dev)) >= (IP_VERSION(9, 0, 1)))
20724294e7bSPhilip Yang #define KFD_SUPPORT_XNACK_PER_PROCESS(dev)\
208cebbfdd5SAmber Lin 	((KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 2)) ||	\
209cebbfdd5SAmber Lin 	 (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3)))
210ef568db7SFelix Kuehling 
2118dc1db31SMukul Joshi struct kfd_node;
2128dc1db31SMukul Joshi 
213f3a39818SAndrew Lewycky struct kfd_event_interrupt_class {
2148dc1db31SMukul Joshi 	bool (*interrupt_isr)(struct kfd_node *dev,
21558e69886SLan Xiao 			const uint32_t *ih_ring_entry, uint32_t *patched_ihre,
21658e69886SLan Xiao 			bool *patched_flag);
2178dc1db31SMukul Joshi 	void (*interrupt_wq)(struct kfd_node *dev,
218f3a39818SAndrew Lewycky 			const uint32_t *ih_ring_entry);
219f3a39818SAndrew Lewycky };
220f3a39818SAndrew Lewycky 
2214a488a7aSOded Gabbay struct kfd_device_info {
2229d6fa9c7SGraham Sider 	uint32_t gfx_target_version;
223f3a39818SAndrew Lewycky 	const struct kfd_event_interrupt_class *event_interrupt_class;
2244a488a7aSOded Gabbay 	unsigned int max_pasid_bits;
225992839adSYair Shachar 	unsigned int max_no_of_hqd;
226ada2b29cSFelix Kuehling 	unsigned int doorbell_size;
2274a488a7aSOded Gabbay 	size_t ih_ring_entry_size;
228f7c826adSAlexey Skidanov 	uint8_t num_of_watch_points;
22919f6d2a6SOded Gabbay 	uint16_t mqd_size_aligned;
230373d7080SFelix Kuehling 	bool supports_cwsr;
2313ee2d00cSFelix Kuehling 	bool needs_pci_atomics;
232fb932dfeSFelix Kuehling 	uint32_t no_atomic_fw_version;
233d5094189SShaoyun Liu 	unsigned int num_sdma_queues_per_engine;
234cc009e61SMukul Joshi 	unsigned int num_reserved_sdma_queues_per_engine;
235597364adSMukul Joshi 	DECLARE_BITMAP(reserved_sdma_queues_bitmap, KFD_MAX_SDMA_QUEUES);
2364a488a7aSOded Gabbay };
2374a488a7aSOded Gabbay 
2388dc1db31SMukul Joshi unsigned int kfd_get_num_sdma_engines(struct kfd_node *kdev);
2398dc1db31SMukul Joshi unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_node *kdev);
240ee2f17f4SAmber Lin 
24136b5c08fSOded Gabbay struct kfd_mem_obj {
24236b5c08fSOded Gabbay 	uint32_t range_start;
24336b5c08fSOded Gabbay 	uint32_t range_end;
24436b5c08fSOded Gabbay 	uint64_t gpu_addr;
24536b5c08fSOded Gabbay 	uint32_t *cpu_ptr;
246b91d43ddSFelix Kuehling 	void *gtt_mem;
24736b5c08fSOded Gabbay };
24836b5c08fSOded Gabbay 
24944008d7aSYong Zhao struct kfd_vmid_info {
25044008d7aSYong Zhao 	uint32_t first_vmid_kfd;
25144008d7aSYong Zhao 	uint32_t last_vmid_kfd;
25244008d7aSYong Zhao 	uint32_t vmid_num_kfd;
25344008d7aSYong Zhao };
25444008d7aSYong Zhao 
25574c5b85dSMukul Joshi #define MAX_KFD_NODES	8
25674c5b85dSMukul Joshi 
2578dc1db31SMukul Joshi struct kfd_dev;
2588dc1db31SMukul Joshi 
2598dc1db31SMukul Joshi struct kfd_node {
260a805889aSMukul Joshi 	unsigned int node_id;
2618dc1db31SMukul Joshi 	struct amdgpu_device *adev;     /* Duplicated here along with keeping
2628dc1db31SMukul Joshi 					 * a copy in kfd_dev to save a hop
2638dc1db31SMukul Joshi 					 */
2648dc1db31SMukul Joshi 	const struct kfd2kgd_calls *kfd2kgd; /* Duplicated here along with
2658dc1db31SMukul Joshi 					      * keeping a copy in kfd_dev to
2668dc1db31SMukul Joshi 					      * save a hop
2678dc1db31SMukul Joshi 					      */
2688dc1db31SMukul Joshi 	struct kfd_vmid_info vm_info;
2698dc1db31SMukul Joshi 	unsigned int id;                /* topology stub index */
270a75f2271SLijo Lazar 	uint32_t xcc_mask; /* Instance mask of XCCs present */
271a75f2271SLijo Lazar 	struct amdgpu_xcp *xcp;
272a75f2271SLijo Lazar 
2738dc1db31SMukul Joshi 	/* Interrupts */
2748dc1db31SMukul Joshi 	struct kfifo ih_fifo;
2758dc1db31SMukul Joshi 	struct workqueue_struct *ih_wq;
2768dc1db31SMukul Joshi 	struct work_struct interrupt_work;
2778dc1db31SMukul Joshi 	spinlock_t interrupt_lock;
2788dc1db31SMukul Joshi 
2798dc1db31SMukul Joshi 	/*
2808dc1db31SMukul Joshi 	 * Interrupts of interest to KFD are copied
2818dc1db31SMukul Joshi 	 * from the HW ring into a SW ring.
2828dc1db31SMukul Joshi 	 */
2838dc1db31SMukul Joshi 	bool interrupts_active;
2845fb34bd9SAlex Sierra 	uint32_t interrupt_bitmap; /* Only used for GFX 9.4.3 */
2858dc1db31SMukul Joshi 
2868dc1db31SMukul Joshi 	/* QCM Device instance */
2878dc1db31SMukul Joshi 	struct device_queue_manager *dqm;
2888dc1db31SMukul Joshi 
2898dc1db31SMukul Joshi 	/* Global GWS resource shared between processes */
2908dc1db31SMukul Joshi 	void *gws;
2918dc1db31SMukul Joshi 	bool gws_debug_workaround;
2928dc1db31SMukul Joshi 
2938dc1db31SMukul Joshi 	/* Clients watching SMI events */
2948dc1db31SMukul Joshi 	struct list_head smi_clients;
2958dc1db31SMukul Joshi 	spinlock_t smi_lock;
2968dc1db31SMukul Joshi 	uint32_t reset_seq_num;
2978dc1db31SMukul Joshi 
2988dc1db31SMukul Joshi 	/* SRAM ECC flag */
2998dc1db31SMukul Joshi 	atomic_t sram_ecc_flag;
3008dc1db31SMukul Joshi 
3018dc1db31SMukul Joshi 	/*spm process id */
3028dc1db31SMukul Joshi 	unsigned int spm_pasid;
3038dc1db31SMukul Joshi 
3048dc1db31SMukul Joshi 	/* Maximum process number mapped to HW scheduler */
3058dc1db31SMukul Joshi 	unsigned int max_proc_per_quantum;
3068dc1db31SMukul Joshi 
30774c5b85dSMukul Joshi 	unsigned int compute_vmid_bitmap;
30874c5b85dSMukul Joshi 
309315e29ecSMukul Joshi 	struct kfd_local_mem_info local_mem_info;
310315e29ecSMukul Joshi 
3118dc1db31SMukul Joshi 	struct kfd_dev *kfd;
3128dc1db31SMukul Joshi };
3138dc1db31SMukul Joshi 
3144a488a7aSOded Gabbay struct kfd_dev {
315c6c57446SGraham Sider 	struct amdgpu_device *adev;
3164a488a7aSOded Gabbay 
317f0dc99a6SGraham Sider 	struct kfd_device_info device_info;
3184a488a7aSOded Gabbay 
31919f6d2a6SOded Gabbay 	u32 __iomem *doorbell_kernel_ptr; /* This is a pointer for a doorbells
32019f6d2a6SOded Gabbay 					   * page used by kernel queue
32119f6d2a6SOded Gabbay 					   */
32219f6d2a6SOded Gabbay 
3234a488a7aSOded Gabbay 	struct kgd2kfd_shared_resources shared_resources;
3244a488a7aSOded Gabbay 
325cea405b1SXihan Zhang 	const struct kfd2kgd_calls *kfd2kgd;
326cea405b1SXihan Zhang 	struct mutex doorbell_mutex;
327cea405b1SXihan Zhang 
32836b5c08fSOded Gabbay 	void *gtt_mem;
32936b5c08fSOded Gabbay 	uint64_t gtt_start_gpu_addr;
33036b5c08fSOded Gabbay 	void *gtt_start_cpu_ptr;
33136b5c08fSOded Gabbay 	void *gtt_sa_bitmap;
33236b5c08fSOded Gabbay 	struct mutex gtt_sa_lock;
33336b5c08fSOded Gabbay 	unsigned int gtt_sa_chunk_size;
33436b5c08fSOded Gabbay 	unsigned int gtt_sa_num_of_chunks;
33536b5c08fSOded Gabbay 
336ed6e6a34SBen Goz 	bool init_complete;
337fbeb661bSYair Shachar 
3385ade6c9cSFelix Kuehling 	/* Firmware versions */
3395ade6c9cSFelix Kuehling 	uint16_t mec_fw_version;
34029633d0eSJoseph Greathouse 	uint16_t mec2_fw_version;
3415ade6c9cSFelix Kuehling 	uint16_t sdma_fw_version;
3425ade6c9cSFelix Kuehling 
343373d7080SFelix Kuehling 	/* CWSR */
344373d7080SFelix Kuehling 	bool cwsr_enabled;
345373d7080SFelix Kuehling 	const void *cwsr_isa;
346373d7080SFelix Kuehling 	unsigned int cwsr_isa_size;
3470c1690e3SShaoyun Liu 
3480c1690e3SShaoyun Liu 	/* xGMI */
3490c1690e3SShaoyun Liu 	uint64_t hive_id;
3500c663695SDivya Shikre 
351d35f00d8SEric Huang 	bool pci_atomic_requested;
3529b54d201SEric Huang 
353f756e631SHarish Kasiviswanathan 	/* Compute Profile ref. count */
354f756e631SHarish Kasiviswanathan 	atomic_t compute_profile;
355e09d4fc8SOak Zeng 
35659d7115dSMukul Joshi 	struct ida doorbell_ida;
35759d7115dSMukul Joshi 	unsigned int max_doorbell_slices;
3589b498efaSAlex Deucher 
3599b498efaSAlex Deucher 	int noretry;
360814ab993SPhilip Yang 
36174c5b85dSMukul Joshi 	struct kfd_node *nodes[MAX_KFD_NODES];
36274c5b85dSMukul Joshi 	unsigned int num_nodes;
363e0f85f46SJonathan Kim 
364e0f85f46SJonathan Kim 	/* Track per device allocated watch points */
365e0f85f46SJonathan Kim 	uint32_t alloc_watch_ids;
366e0f85f46SJonathan Kim 	spinlock_t watch_points_lock;
367c3186665SShashank Sharma 
368c3186665SShashank Sharma 	/* Kernel doorbells for KFD device */
369c3186665SShashank Sharma 	struct amdgpu_bo *doorbells;
370c3186665SShashank Sharma 
371c3186665SShashank Sharma 	/* bitmap for dynamic doorbell allocation from doorbell object */
372c3186665SShashank Sharma 	unsigned long *doorbell_bitmap;
3734a488a7aSOded Gabbay };
3744a488a7aSOded Gabbay 
37519f6d2a6SOded Gabbay enum kfd_mempool {
37619f6d2a6SOded Gabbay 	KFD_MEMPOOL_SYSTEM_CACHEABLE = 1,
37719f6d2a6SOded Gabbay 	KFD_MEMPOOL_SYSTEM_WRITECOMBINE = 2,
37819f6d2a6SOded Gabbay 	KFD_MEMPOOL_FRAMEBUFFER = 3,
37919f6d2a6SOded Gabbay };
38019f6d2a6SOded Gabbay 
3814a488a7aSOded Gabbay /* Character device interface */
3824a488a7aSOded Gabbay int kfd_chardev_init(void);
3834a488a7aSOded Gabbay void kfd_chardev_exit(void);
3844a488a7aSOded Gabbay 
385241f24f8SBen Goz /**
386a4497974SRajneesh Bhardwaj  * enum kfd_unmap_queues_filter - Enum for queue filters.
387241f24f8SBen Goz  *
3887da2bcf8SYong Zhao  * @KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES: Preempts all queues in the
389241f24f8SBen Goz  *						running queues list.
390241f24f8SBen Goz  *
391d2cb0b21SJonathan Kim  * @KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES: Preempts all non-static queues
392d2cb0b21SJonathan Kim  *						in the run list.
393d2cb0b21SJonathan Kim  *
3947da2bcf8SYong Zhao  * @KFD_UNMAP_QUEUES_FILTER_BY_PASID: Preempts queues that belongs to
395241f24f8SBen Goz  *						specific process.
396241f24f8SBen Goz  *
397241f24f8SBen Goz  */
3987da2bcf8SYong Zhao enum kfd_unmap_queues_filter {
399d2cb0b21SJonathan Kim 	KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES = 1,
400d2cb0b21SJonathan Kim 	KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES = 2,
401d2cb0b21SJonathan Kim 	KFD_UNMAP_QUEUES_FILTER_BY_PASID = 3
402241f24f8SBen Goz };
40319f6d2a6SOded Gabbay 
404ed8aab45SBen Goz /**
405a4497974SRajneesh Bhardwaj  * enum kfd_queue_type - Enum for various queue types.
406ed8aab45SBen Goz  *
407ed8aab45SBen Goz  * @KFD_QUEUE_TYPE_COMPUTE: Regular user mode queue type.
408ed8aab45SBen Goz  *
409a4497974SRajneesh Bhardwaj  * @KFD_QUEUE_TYPE_SDMA: SDMA user mode queue type.
410ed8aab45SBen Goz  *
411ed8aab45SBen Goz  * @KFD_QUEUE_TYPE_HIQ: HIQ queue type.
412ed8aab45SBen Goz  *
413ed8aab45SBen Goz  * @KFD_QUEUE_TYPE_DIQ: DIQ queue type.
414a4497974SRajneesh Bhardwaj  *
415a4497974SRajneesh Bhardwaj  * @KFD_QUEUE_TYPE_SDMA_XGMI: Special SDMA queue for XGMI interface.
416ed8aab45SBen Goz  */
417ed8aab45SBen Goz enum kfd_queue_type  {
418ed8aab45SBen Goz 	KFD_QUEUE_TYPE_COMPUTE,
419ed8aab45SBen Goz 	KFD_QUEUE_TYPE_SDMA,
420ed8aab45SBen Goz 	KFD_QUEUE_TYPE_HIQ,
4211b4670f6SOak Zeng 	KFD_QUEUE_TYPE_DIQ,
4221b4670f6SOak Zeng 	KFD_QUEUE_TYPE_SDMA_XGMI
423ed8aab45SBen Goz };
424ed8aab45SBen Goz 
4256e99df57SBen Goz enum kfd_queue_format {
4266e99df57SBen Goz 	KFD_QUEUE_FORMAT_PM4,
4276e99df57SBen Goz 	KFD_QUEUE_FORMAT_AQL
4286e99df57SBen Goz };
4296e99df57SBen Goz 
4300ccbc7cdSOak Zeng enum KFD_QUEUE_PRIORITY {
4310ccbc7cdSOak Zeng 	KFD_QUEUE_PRIORITY_MINIMUM = 0,
4320ccbc7cdSOak Zeng 	KFD_QUEUE_PRIORITY_MAXIMUM = 15
4330ccbc7cdSOak Zeng };
4340ccbc7cdSOak Zeng 
435ed8aab45SBen Goz /**
436ed8aab45SBen Goz  * struct queue_properties
437ed8aab45SBen Goz  *
438ed8aab45SBen Goz  * @type: The queue type.
439ed8aab45SBen Goz  *
440ed8aab45SBen Goz  * @queue_id: Queue identifier.
441ed8aab45SBen Goz  *
442ed8aab45SBen Goz  * @queue_address: Queue ring buffer address.
443ed8aab45SBen Goz  *
444ed8aab45SBen Goz  * @queue_size: Queue ring buffer size.
445ed8aab45SBen Goz  *
446ed8aab45SBen Goz  * @priority: Defines the queue priority relative to other queues in the
447ed8aab45SBen Goz  * process.
448ed8aab45SBen Goz  * This is just an indication and HW scheduling may override the priority as
449ed8aab45SBen Goz  * necessary while keeping the relative prioritization.
450ed8aab45SBen Goz  * the priority granularity is from 0 to f which f is the highest priority.
451ed8aab45SBen Goz  * currently all queues are initialized with the highest priority.
452ed8aab45SBen Goz  *
453ed8aab45SBen Goz  * @queue_percent: This field is partially implemented and currently a zero in
454ed8aab45SBen Goz  * this field defines that the queue is non active.
455ed8aab45SBen Goz  *
456ed8aab45SBen Goz  * @read_ptr: User space address which points to the number of dwords the
457ed8aab45SBen Goz  * cp read from the ring buffer. This field updates automatically by the H/W.
458ed8aab45SBen Goz  *
459ed8aab45SBen Goz  * @write_ptr: Defines the number of dwords written to the ring buffer.
460ed8aab45SBen Goz  *
461a4497974SRajneesh Bhardwaj  * @doorbell_ptr: Notifies the H/W of new packet written to the queue ring
462a4497974SRajneesh Bhardwaj  * buffer. This field should be similar to write_ptr and the user should
463a4497974SRajneesh Bhardwaj  * update this field after updating the write_ptr.
464ed8aab45SBen Goz  *
465ed8aab45SBen Goz  * @doorbell_off: The doorbell offset in the doorbell pci-bar.
466ed8aab45SBen Goz  *
4678eabaf54SKent Russell  * @is_interop: Defines if this is a interop queue. Interop queue means that
4688eabaf54SKent Russell  * the queue can access both graphics and compute resources.
469ed8aab45SBen Goz  *
47026103436SFelix Kuehling  * @is_evicted: Defines if the queue is evicted. Only active queues
47126103436SFelix Kuehling  * are evicted, rendering them inactive.
47226103436SFelix Kuehling  *
47326103436SFelix Kuehling  * @is_active: Defines if the queue is active or not. @is_active and
47426103436SFelix Kuehling  * @is_evicted are protected by the DQM lock.
475ed8aab45SBen Goz  *
476b8020b03SJoseph Greathouse  * @is_gws: Defines if the queue has been updated to be GWS-capable or not.
477b8020b03SJoseph Greathouse  * @is_gws should be protected by the DQM lock, since changing it can yield the
478b8020b03SJoseph Greathouse  * possibility of updating DQM state on number of GWS queues.
479b8020b03SJoseph Greathouse  *
480ed8aab45SBen Goz  * @vmid: If the scheduling mode is no cp scheduling the field defines the vmid
481ed8aab45SBen Goz  * of the queue.
482ed8aab45SBen Goz  *
483ed8aab45SBen Goz  * This structure represents the queue properties for each queue no matter if
484ed8aab45SBen Goz  * it's user mode or kernel mode queue.
485ed8aab45SBen Goz  *
486ed8aab45SBen Goz  */
4878668dfc3SDavid Yat Sin 
488ed8aab45SBen Goz struct queue_properties {
489ed8aab45SBen Goz 	enum kfd_queue_type type;
4906e99df57SBen Goz 	enum kfd_queue_format format;
491ed8aab45SBen Goz 	unsigned int queue_id;
492ed8aab45SBen Goz 	uint64_t queue_address;
493ed8aab45SBen Goz 	uint64_t  queue_size;
494ed8aab45SBen Goz 	uint32_t priority;
495ed8aab45SBen Goz 	uint32_t queue_percent;
496ed8aab45SBen Goz 	uint32_t *read_ptr;
497ed8aab45SBen Goz 	uint32_t *write_ptr;
498ada2b29cSFelix Kuehling 	void __iomem *doorbell_ptr;
499ed8aab45SBen Goz 	uint32_t doorbell_off;
500ed8aab45SBen Goz 	bool is_interop;
50126103436SFelix Kuehling 	bool is_evicted;
502a70a93faSJonathan Kim 	bool is_suspended;
503a70a93faSJonathan Kim 	bool is_being_destroyed;
504ed8aab45SBen Goz 	bool is_active;
505b8020b03SJoseph Greathouse 	bool is_gws;
5063c8bdb51SMukul Joshi 	uint32_t pm4_target_xcc;
50769a8c3aeSJonathan Kim 	bool is_dbg_wa;
50869a8c3aeSJonathan Kim 	bool is_user_cu_masked;
509ed8aab45SBen Goz 	/* Not relevant for user mode queues in cp scheduling */
510ed8aab45SBen Goz 	unsigned int vmid;
51177669eb8SBen Goz 	/* Relevant only for sdma queues*/
51277669eb8SBen Goz 	uint32_t sdma_engine_id;
51377669eb8SBen Goz 	uint32_t sdma_queue_id;
51477669eb8SBen Goz 	uint32_t sdma_vm_addr;
515ff3d04a1SBen Goz 	/* Relevant only for VI */
516ff3d04a1SBen Goz 	uint64_t eop_ring_buffer_address;
517ff3d04a1SBen Goz 	uint32_t eop_ring_buffer_size;
518ff3d04a1SBen Goz 	uint64_t ctx_save_restore_area_address;
519ff3d04a1SBen Goz 	uint32_t ctx_save_restore_area_size;
520373d7080SFelix Kuehling 	uint32_t ctl_stack_size;
521373d7080SFelix Kuehling 	uint64_t tba_addr;
522373d7080SFelix Kuehling 	uint64_t tma_addr;
52344b87bb0SJonathan Kim 	uint64_t exception_status;
524ed8aab45SBen Goz };
525ed8aab45SBen Goz 
526bb2d2128SFelix Kuehling #define QUEUE_IS_ACTIVE(q) ((q).queue_size > 0 &&	\
527bb2d2128SFelix Kuehling 			    (q).queue_address != 0 &&	\
528bb2d2128SFelix Kuehling 			    (q).queue_percent > 0 &&	\
529a70a93faSJonathan Kim 			    !(q).is_evicted &&		\
530a70a93faSJonathan Kim 			    !(q).is_suspended)
531bb2d2128SFelix Kuehling 
5327c695a2cSLang Yu enum mqd_update_flag {
53369a8c3aeSJonathan Kim 	UPDATE_FLAG_DBG_WA_ENABLE = 1,
53469a8c3aeSJonathan Kim 	UPDATE_FLAG_DBG_WA_DISABLE = 2,
5357c695a2cSLang Yu };
5367c695a2cSLang Yu 
5377c695a2cSLang Yu struct mqd_update_info {
5387c695a2cSLang Yu 	union {
5397c695a2cSLang Yu 		struct {
5407c695a2cSLang Yu 			uint32_t count; /* Must be a multiple of 32 */
5417c695a2cSLang Yu 			uint32_t *ptr;
5427c695a2cSLang Yu 		} cu_mask;
5437c695a2cSLang Yu 	};
5447c695a2cSLang Yu 	enum mqd_update_flag update_flag;
5457c695a2cSLang Yu };
546c6e559ebSLang Yu 
547ed8aab45SBen Goz /**
548ed8aab45SBen Goz  * struct queue
549ed8aab45SBen Goz  *
550ed8aab45SBen Goz  * @list: Queue linked list.
551ed8aab45SBen Goz  *
552a4497974SRajneesh Bhardwaj  * @mqd: The queue MQD (memory queue descriptor).
553ed8aab45SBen Goz  *
554ed8aab45SBen Goz  * @mqd_mem_obj: The MQD local gpu memory object.
555ed8aab45SBen Goz  *
556ed8aab45SBen Goz  * @gart_mqd_addr: The MQD gart mc address.
557ed8aab45SBen Goz  *
558ed8aab45SBen Goz  * @properties: The queue properties.
559ed8aab45SBen Goz  *
560ed8aab45SBen Goz  * @mec: Used only in no cp scheduling mode and identifies to micro engine id
561a4497974SRajneesh Bhardwaj  *	 that the queue should be executed on.
562ed8aab45SBen Goz  *
5638eabaf54SKent Russell  * @pipe: Used only in no cp scheduling mode and identifies the queue's pipe
5648eabaf54SKent Russell  *	  id.
565ed8aab45SBen Goz  *
566ed8aab45SBen Goz  * @queue: Used only in no cp scheduliong mode and identifies the queue's slot.
567ed8aab45SBen Goz  *
568ed8aab45SBen Goz  * @process: The kfd process that created this queue.
569ed8aab45SBen Goz  *
570ed8aab45SBen Goz  * @device: The kfd device that created this queue.
571ed8aab45SBen Goz  *
572eb82da1dSOak Zeng  * @gws: Pointing to gws kgd_mem if this is a gws control queue; NULL
573eb82da1dSOak Zeng  * otherwise.
574eb82da1dSOak Zeng  *
575ed8aab45SBen Goz  * This structure represents user mode compute queues.
576ed8aab45SBen Goz  * It contains all the necessary data to handle such queues.
577ed8aab45SBen Goz  *
578ed8aab45SBen Goz  */
579ed8aab45SBen Goz 
580ed8aab45SBen Goz struct queue {
581ed8aab45SBen Goz 	struct list_head list;
582ed8aab45SBen Goz 	void *mqd;
583ed8aab45SBen Goz 	struct kfd_mem_obj *mqd_mem_obj;
584ed8aab45SBen Goz 	uint64_t gart_mqd_addr;
585ed8aab45SBen Goz 	struct queue_properties properties;
586ed8aab45SBen Goz 
587ed8aab45SBen Goz 	uint32_t mec;
588ed8aab45SBen Goz 	uint32_t pipe;
589ed8aab45SBen Goz 	uint32_t queue;
590ed8aab45SBen Goz 
59177669eb8SBen Goz 	unsigned int sdma_id;
592ef568db7SFelix Kuehling 	unsigned int doorbell_id;
59377669eb8SBen Goz 
594ed8aab45SBen Goz 	struct kfd_process	*process;
5958dc1db31SMukul Joshi 	struct kfd_node		*device;
596eb82da1dSOak Zeng 	void *gws;
5976d220a7eSAmber Lin 
5986d220a7eSAmber Lin 	/* procfs */
5996d220a7eSAmber Lin 	struct kobject kobj;
600cc009e61SMukul Joshi 
601cc009e61SMukul Joshi 	void *gang_ctx_bo;
602cc009e61SMukul Joshi 	uint64_t gang_ctx_gpu_addr;
603cc009e61SMukul Joshi 	void *gang_ctx_cpu_ptr;
604e77a541fSGraham Sider 
605e77a541fSGraham Sider 	struct amdgpu_bo *wptr_bo;
606ed8aab45SBen Goz };
607ed8aab45SBen Goz 
6086e99df57SBen Goz enum KFD_MQD_TYPE {
609d7c0b047SYong Zhao 	KFD_MQD_TYPE_HIQ = 0,		/* for hiq */
61085d258f9SBen Goz 	KFD_MQD_TYPE_CP,		/* for cp queues and diq */
61185d258f9SBen Goz 	KFD_MQD_TYPE_SDMA,		/* for sdma queues */
61259f650a0SOak Zeng 	KFD_MQD_TYPE_DIQ,		/* for diq */
6136e99df57SBen Goz 	KFD_MQD_TYPE_MAX
6146e99df57SBen Goz };
6156e99df57SBen Goz 
6160ccbc7cdSOak Zeng enum KFD_PIPE_PRIORITY {
6170ccbc7cdSOak Zeng 	KFD_PIPE_PRIORITY_CS_LOW = 0,
6180ccbc7cdSOak Zeng 	KFD_PIPE_PRIORITY_CS_MEDIUM,
6190ccbc7cdSOak Zeng 	KFD_PIPE_PRIORITY_CS_HIGH
6200ccbc7cdSOak Zeng };
6210ccbc7cdSOak Zeng 
622241f24f8SBen Goz struct scheduling_resources {
623241f24f8SBen Goz 	unsigned int vmid_mask;
624241f24f8SBen Goz 	enum kfd_queue_type type;
625241f24f8SBen Goz 	uint64_t queue_mask;
626241f24f8SBen Goz 	uint64_t gws_mask;
627241f24f8SBen Goz 	uint32_t oac_mask;
628241f24f8SBen Goz 	uint32_t gds_heap_base;
629241f24f8SBen Goz 	uint32_t gds_heap_size;
630241f24f8SBen Goz };
631241f24f8SBen Goz 
632241f24f8SBen Goz struct process_queue_manager {
633241f24f8SBen Goz 	/* data */
634241f24f8SBen Goz 	struct kfd_process	*process;
635241f24f8SBen Goz 	struct list_head	queues;
636241f24f8SBen Goz 	unsigned long		*queue_slot_bitmap;
637241f24f8SBen Goz };
638241f24f8SBen Goz 
639241f24f8SBen Goz struct qcm_process_device {
640241f24f8SBen Goz 	/* The Device Queue Manager that owns this data */
641241f24f8SBen Goz 	struct device_queue_manager *dqm;
642241f24f8SBen Goz 	struct process_queue_manager *pqm;
643241f24f8SBen Goz 	/* Queues list */
644241f24f8SBen Goz 	struct list_head queues_list;
645241f24f8SBen Goz 	struct list_head priv_queue_list;
646241f24f8SBen Goz 
647241f24f8SBen Goz 	unsigned int queue_count;
648241f24f8SBen Goz 	unsigned int vmid;
649241f24f8SBen Goz 	bool is_debug;
65026103436SFelix Kuehling 	unsigned int evicted; /* eviction counter, 0=active */
6519fd3f1bfSFelix Kuehling 
6529fd3f1bfSFelix Kuehling 	/* This flag tells if we should reset all wavefronts on
6539fd3f1bfSFelix Kuehling 	 * process termination
6549fd3f1bfSFelix Kuehling 	 */
6559fd3f1bfSFelix Kuehling 	bool reset_wavefronts;
6569fd3f1bfSFelix Kuehling 
657b8020b03SJoseph Greathouse 	/* This flag tells us if this process has a GWS-capable
658b8020b03SJoseph Greathouse 	 * queue that will be mapped into the runlist. It's
659b8020b03SJoseph Greathouse 	 * possible to request a GWS BO, but not have the queue
660b8020b03SJoseph Greathouse 	 * currently mapped, and this changes how the MAP_PROCESS
661b8020b03SJoseph Greathouse 	 * PM4 packet is configured.
662b8020b03SJoseph Greathouse 	 */
663b8020b03SJoseph Greathouse 	bool mapped_gws_queue;
664b8020b03SJoseph Greathouse 
665a4497974SRajneesh Bhardwaj 	/* All the memory management data should be here too */
666241f24f8SBen Goz 	uint64_t gds_context_area;
667435e2f97SYong Zhao 	/* Contains page table flags such as AMDGPU_PTE_VALID since gfx9 */
668e715c6d0SShaoyun Liu 	uint64_t page_table_base;
669241f24f8SBen Goz 	uint32_t sh_mem_config;
670241f24f8SBen Goz 	uint32_t sh_mem_bases;
671241f24f8SBen Goz 	uint32_t sh_mem_ape1_base;
672241f24f8SBen Goz 	uint32_t sh_mem_ape1_limit;
673241f24f8SBen Goz 	uint32_t gds_size;
674241f24f8SBen Goz 	uint32_t num_gws;
675241f24f8SBen Goz 	uint32_t num_oac;
6766a1c9510SMoses Reuben 	uint32_t sh_hidden_private_base;
677373d7080SFelix Kuehling 
678373d7080SFelix Kuehling 	/* CWSR memory */
67968df0f19SLang Yu 	struct kgd_mem *cwsr_mem;
680373d7080SFelix Kuehling 	void *cwsr_kaddr;
681d01994c2SFelix Kuehling 	uint64_t cwsr_base;
682373d7080SFelix Kuehling 	uint64_t tba_addr;
683373d7080SFelix Kuehling 	uint64_t tma_addr;
684d01994c2SFelix Kuehling 
685d01994c2SFelix Kuehling 	/* IB memory */
68668df0f19SLang Yu 	struct kgd_mem *ib_mem;
687d01994c2SFelix Kuehling 	uint64_t ib_base;
688552764b6SFelix Kuehling 	void *ib_kaddr;
689ef568db7SFelix Kuehling 
6902105a15aSShashank Sharma 	/* doorbells for kfd process */
6912105a15aSShashank Sharma 	struct amdgpu_bo *proc_doorbells;
6922105a15aSShashank Sharma 
6932105a15aSShashank Sharma 	/* bitmap for dynamic doorbell allocation from the bo */
694ef568db7SFelix Kuehling 	unsigned long *doorbell_bitmap;
695241f24f8SBen Goz };
696241f24f8SBen Goz 
69726103436SFelix Kuehling /* KFD Memory Eviction */
69826103436SFelix Kuehling 
69926103436SFelix Kuehling /* Approx. wait time before attempting to restore evicted BOs */
70026103436SFelix Kuehling #define PROCESS_RESTORE_TIME_MS 100
70126103436SFelix Kuehling /* Approx. back off time if restore fails due to lack of memory */
70226103436SFelix Kuehling #define PROCESS_BACK_OFF_TIME_MS 100
70326103436SFelix Kuehling /* Approx. time before evicting the process again */
70426103436SFelix Kuehling #define PROCESS_ACTIVE_TIME_MS 10
70526103436SFelix Kuehling 
7065ec7e028SFelix Kuehling /* 8 byte handle containing GPU ID in the most significant 4 bytes and
7075ec7e028SFelix Kuehling  * idr_handle in the least significant 4 bytes
7085ec7e028SFelix Kuehling  */
7095ec7e028SFelix Kuehling #define MAKE_HANDLE(gpu_id, idr_handle) \
7105ec7e028SFelix Kuehling 	(((uint64_t)(gpu_id) << 32) + idr_handle)
7115ec7e028SFelix Kuehling #define GET_GPU_ID(handle) (handle >> 32)
7125ec7e028SFelix Kuehling #define GET_IDR_HANDLE(handle) (handle & 0xFFFFFFFF)
7135ec7e028SFelix Kuehling 
714733fa1f7SYong Zhao enum kfd_pdd_bound {
715733fa1f7SYong Zhao 	PDD_UNBOUND = 0,
716733fa1f7SYong Zhao 	PDD_BOUND,
717733fa1f7SYong Zhao 	PDD_BOUND_SUSPENDED,
718733fa1f7SYong Zhao };
719733fa1f7SYong Zhao 
7204327bed2SPhilip Cox #define MAX_SYSFS_FILENAME_LEN 15
72132cb59f3SMukul Joshi 
72232cb59f3SMukul Joshi /*
72332cb59f3SMukul Joshi  * SDMA counter runs at 100MHz frequency.
72432cb59f3SMukul Joshi  * We display SDMA activity in microsecond granularity in sysfs.
72532cb59f3SMukul Joshi  * As a result, the divisor is 100.
72632cb59f3SMukul Joshi  */
72732cb59f3SMukul Joshi #define SDMA_ACTIVITY_DIVISOR  100
728d4566deeSMukul Joshi 
72919f6d2a6SOded Gabbay /* Data that is per-process-per device. */
73019f6d2a6SOded Gabbay struct kfd_process_device {
73119f6d2a6SOded Gabbay 	/* The device that owns this data. */
7328dc1db31SMukul Joshi 	struct kfd_node *dev;
73319f6d2a6SOded Gabbay 
7349fd3f1bfSFelix Kuehling 	/* The process that owns this kfd_process_device. */
7359fd3f1bfSFelix Kuehling 	struct kfd_process *process;
73619f6d2a6SOded Gabbay 
73745102048SBen Goz 	/* per-process-per device QCM data structure */
73845102048SBen Goz 	struct qcm_process_device qpd;
73945102048SBen Goz 
74019f6d2a6SOded Gabbay 	/*Apertures*/
74119f6d2a6SOded Gabbay 	uint64_t lds_base;
74219f6d2a6SOded Gabbay 	uint64_t lds_limit;
74319f6d2a6SOded Gabbay 	uint64_t gpuvm_base;
74419f6d2a6SOded Gabbay 	uint64_t gpuvm_limit;
74519f6d2a6SOded Gabbay 	uint64_t scratch_base;
74619f6d2a6SOded Gabbay 	uint64_t scratch_limit;
74719f6d2a6SOded Gabbay 
748403575c4SFelix Kuehling 	/* VM context for GPUVM allocations */
749b84394e2SFelix Kuehling 	struct file *drm_file;
750b40a6ab2SFelix Kuehling 	void *drm_priv;
7518fde0248SPhilip Yang 	atomic64_t tlb_seq;
752403575c4SFelix Kuehling 
75352b29d73SFelix Kuehling 	/* GPUVM allocations storage */
75452b29d73SFelix Kuehling 	struct idr alloc_idr;
75552b29d73SFelix Kuehling 
7569fd3f1bfSFelix Kuehling 	/* Flag used to tell the pdd has dequeued from the dqm.
7579fd3f1bfSFelix Kuehling 	 * This is used to prevent dev->dqm->ops.process_termination() from
7589fd3f1bfSFelix Kuehling 	 * being called twice when it is already called in IOMMU callback
7599fd3f1bfSFelix Kuehling 	 * function.
760a82918f1SBen Goz 	 */
7619fd3f1bfSFelix Kuehling 	bool already_dequeued;
7629593f4d6SRajneesh Bhardwaj 	bool runtime_inuse;
76364d1c3a4SFelix Kuehling 
76464d1c3a4SFelix Kuehling 	/* Is this process/pasid bound to this device? (amd_iommu_bind_pasid) */
76564d1c3a4SFelix Kuehling 	enum kfd_pdd_bound bound;
766d4566deeSMukul Joshi 
767d4566deeSMukul Joshi 	/* VRAM usage */
768d4566deeSMukul Joshi 	uint64_t vram_usage;
769d4566deeSMukul Joshi 	struct attribute attr_vram;
77032cb59f3SMukul Joshi 	char vram_filename[MAX_SYSFS_FILENAME_LEN];
77132cb59f3SMukul Joshi 
77232cb59f3SMukul Joshi 	/* SDMA activity tracking */
77332cb59f3SMukul Joshi 	uint64_t sdma_past_activity_counter;
77432cb59f3SMukul Joshi 	struct attribute attr_sdma;
77532cb59f3SMukul Joshi 	char sdma_filename[MAX_SYSFS_FILENAME_LEN];
7764327bed2SPhilip Cox 
7774327bed2SPhilip Cox 	/* Eviction activity tracking */
7784327bed2SPhilip Cox 	uint64_t last_evict_timestamp;
7794327bed2SPhilip Cox 	atomic64_t evict_duration_counter;
7804327bed2SPhilip Cox 	struct attribute attr_evict;
7814327bed2SPhilip Cox 
7824327bed2SPhilip Cox 	struct kobject *kobj_stats;
783f2fa07b3SRamesh Errabolu 
784f2fa07b3SRamesh Errabolu 	/*
785f2fa07b3SRamesh Errabolu 	 * @cu_occupancy: Reports occupancy of Compute Units (CU) of a process
786f2fa07b3SRamesh Errabolu 	 * that is associated with device encoded by "this" struct instance. The
787f2fa07b3SRamesh Errabolu 	 * value reflects CU usage by all of the waves launched by this process
788f2fa07b3SRamesh Errabolu 	 * on this device. A very important property of occupancy parameter is
789f2fa07b3SRamesh Errabolu 	 * that its value is a snapshot of current use.
790f2fa07b3SRamesh Errabolu 	 *
791f2fa07b3SRamesh Errabolu 	 * Following is to be noted regarding how this parameter is reported:
792f2fa07b3SRamesh Errabolu 	 *
793f2fa07b3SRamesh Errabolu 	 *  The number of waves that a CU can launch is limited by couple of
794f2fa07b3SRamesh Errabolu 	 *  parameters. These are encoded by struct amdgpu_cu_info instance
795f2fa07b3SRamesh Errabolu 	 *  that is part of every device definition. For GFX9 devices this
796f2fa07b3SRamesh Errabolu 	 *  translates to 40 waves (simd_per_cu * max_waves_per_simd) when waves
797f2fa07b3SRamesh Errabolu 	 *  do not use scratch memory and 32 waves (max_scratch_slots_per_cu)
798f2fa07b3SRamesh Errabolu 	 *  when they do use scratch memory. This could change for future
799f2fa07b3SRamesh Errabolu 	 *  devices and therefore this example should be considered as a guide.
800f2fa07b3SRamesh Errabolu 	 *
801f2fa07b3SRamesh Errabolu 	 *  All CU's of a device are available for the process. This may not be true
802f2fa07b3SRamesh Errabolu 	 *  under certain conditions - e.g. CU masking.
803f2fa07b3SRamesh Errabolu 	 *
804f2fa07b3SRamesh Errabolu 	 *  Finally number of CU's that are occupied by a process is affected by both
805f2fa07b3SRamesh Errabolu 	 *  number of CU's a device has along with number of other competing processes
806f2fa07b3SRamesh Errabolu 	 */
807f2fa07b3SRamesh Errabolu 	struct attribute attr_cu_occupancy;
808751580b3SPhilip Yang 
809751580b3SPhilip Yang 	/* sysfs counters for GPU retry fault and page migration tracking */
810751580b3SPhilip Yang 	struct kobject *kobj_counters;
811751580b3SPhilip Yang 	struct attribute attr_faults;
812751580b3SPhilip Yang 	struct attribute attr_page_in;
813751580b3SPhilip Yang 	struct attribute attr_page_out;
814751580b3SPhilip Yang 	uint64_t faults;
815751580b3SPhilip Yang 	uint64_t page_in;
816751580b3SPhilip Yang 	uint64_t page_out;
8170de4ec9aSJonathan Kim 
81844b87bb0SJonathan Kim 	/* Exception code status*/
81944b87bb0SJonathan Kim 	uint64_t exception_status;
82044b87bb0SJonathan Kim 	void *vm_fault_exc_data;
82144b87bb0SJonathan Kim 	size_t vm_fault_exc_data_size;
82244b87bb0SJonathan Kim 
8230de4ec9aSJonathan Kim 	/* Tracks debug per-vmid request settings */
8240de4ec9aSJonathan Kim 	uint32_t spi_dbg_override;
8250de4ec9aSJonathan Kim 	uint32_t spi_dbg_launch_mode;
8260de4ec9aSJonathan Kim 	uint32_t watch_points[4];
827e0f85f46SJonathan Kim 	uint32_t alloc_watch_ids;
8280de4ec9aSJonathan Kim 
829bef153b7SDavid Yat Sin 	/*
830bef153b7SDavid Yat Sin 	 * If this process has been checkpointed before, then the user
831bef153b7SDavid Yat Sin 	 * application will use the original gpu_id on the
832bef153b7SDavid Yat Sin 	 * checkpointed node to refer to this device.
833bef153b7SDavid Yat Sin 	 */
834bef153b7SDavid Yat Sin 	uint32_t user_gpu_id;
835cc009e61SMukul Joshi 
836cc009e61SMukul Joshi 	void *proc_ctx_bo;
837cc009e61SMukul Joshi 	uint64_t proc_ctx_gpu_addr;
838cc009e61SMukul Joshi 	void *proc_ctx_cpu_ptr;
83919f6d2a6SOded Gabbay };
84019f6d2a6SOded Gabbay 
84152a5fdceSAlexey Skidanov #define qpd_to_pdd(x) container_of(x, struct kfd_process_device, qpd)
84252a5fdceSAlexey Skidanov 
84342de677fSPhilip Yang struct svm_range_list {
84442de677fSPhilip Yang 	struct mutex			lock;
84542de677fSPhilip Yang 	struct rb_root_cached		objects;
84642de677fSPhilip Yang 	struct list_head		list;
8474683cfecSPhilip Yang 	struct work_struct		deferred_list_work;
8484683cfecSPhilip Yang 	struct list_head		deferred_range_list;
849c2db32ceSRajneesh Bhardwaj 	struct list_head                criu_svm_metadata_list;
8504683cfecSPhilip Yang 	spinlock_t			deferred_list_lock;
8518a7c184aSFelix Kuehling 	atomic_t			evicted_ranges;
8522e447728SPhilip Yang 	atomic_t			drain_pagefaults;
8538a7c184aSFelix Kuehling 	struct delayed_work		restore_work;
8545a75ea56SFelix Kuehling 	DECLARE_BITMAP(bitmap_supported, MAX_GPU_INSTANCE);
855a6283010SAlex Sierra 	struct task_struct		*faulting_task;
85642de677fSPhilip Yang };
85742de677fSPhilip Yang 
8584a488a7aSOded Gabbay /* Process data */
8594a488a7aSOded Gabbay struct kfd_process {
86019f6d2a6SOded Gabbay 	/*
86119f6d2a6SOded Gabbay 	 * kfd_process are stored in an mm_struct*->kfd_process*
86219f6d2a6SOded Gabbay 	 * hash table (kfd_processes in kfd_process.c)
86319f6d2a6SOded Gabbay 	 */
86419f6d2a6SOded Gabbay 	struct hlist_node kfd_processes;
86519f6d2a6SOded Gabbay 
8669b56bb11SFelix Kuehling 	/*
8679b56bb11SFelix Kuehling 	 * Opaque pointer to mm_struct. We don't hold a reference to
8689b56bb11SFelix Kuehling 	 * it so it should never be dereferenced from here. This is
8699b56bb11SFelix Kuehling 	 * only used for looking up processes by their mm.
8709b56bb11SFelix Kuehling 	 */
8719b56bb11SFelix Kuehling 	void *mm;
87219f6d2a6SOded Gabbay 
8735ce10687SFelix Kuehling 	struct kref ref;
8745ce10687SFelix Kuehling 	struct work_struct release_work;
8755ce10687SFelix Kuehling 
87619f6d2a6SOded Gabbay 	struct mutex mutex;
87719f6d2a6SOded Gabbay 
87819f6d2a6SOded Gabbay 	/*
87919f6d2a6SOded Gabbay 	 * In any process, the thread that started main() is the lead
88019f6d2a6SOded Gabbay 	 * thread and outlives the rest.
88119f6d2a6SOded Gabbay 	 * It is here because amd_iommu_bind_pasid wants a task_struct.
882894a8293SFelix Kuehling 	 * It can also be used for safely getting a reference to the
883894a8293SFelix Kuehling 	 * mm_struct of the process.
88419f6d2a6SOded Gabbay 	 */
88519f6d2a6SOded Gabbay 	struct task_struct *lead_thread;
88619f6d2a6SOded Gabbay 
88719f6d2a6SOded Gabbay 	/* We want to receive a notification when the mm_struct is destroyed */
88819f6d2a6SOded Gabbay 	struct mmu_notifier mmu_notifier;
88919f6d2a6SOded Gabbay 
890c7b6bac9SFenghua Yu 	u32 pasid;
89119f6d2a6SOded Gabbay 
89219f6d2a6SOded Gabbay 	/*
8936ae27841SAlex Sierra 	 * Array of kfd_process_device pointers,
89419f6d2a6SOded Gabbay 	 * one for each device the process is using.
89519f6d2a6SOded Gabbay 	 */
8966ae27841SAlex Sierra 	struct kfd_process_device *pdds[MAX_GPU_INSTANCE];
8976ae27841SAlex Sierra 	uint32_t n_pdds;
89819f6d2a6SOded Gabbay 
89945102048SBen Goz 	struct process_queue_manager pqm;
90045102048SBen Goz 
90119f6d2a6SOded Gabbay 	/*Is the user space process 32 bit?*/
90219f6d2a6SOded Gabbay 	bool is_32bit_user_mode;
903f3a39818SAndrew Lewycky 
904f3a39818SAndrew Lewycky 	/* Event-related data */
905f3a39818SAndrew Lewycky 	struct mutex event_mutex;
906482f0777SFelix Kuehling 	/* Event ID allocator and lookup */
907482f0777SFelix Kuehling 	struct idr event_idr;
90850cb7dd9SFelix Kuehling 	/* Event page */
90968df0f19SLang Yu 	u64 signal_handle;
91050cb7dd9SFelix Kuehling 	struct kfd_signal_page *signal_page;
911b9a5d0a5SFelix Kuehling 	size_t signal_mapped_size;
912f3a39818SAndrew Lewycky 	size_t signal_event_count;
913c986169fSFelix Kuehling 	bool signal_event_limit_reached;
914403575c4SFelix Kuehling 
915403575c4SFelix Kuehling 	/* Information used for memory eviction */
916403575c4SFelix Kuehling 	void *kgd_process_info;
917403575c4SFelix Kuehling 	/* Eviction fence that is attached to all the BOs of this process. The
918403575c4SFelix Kuehling 	 * fence will be triggered during eviction and new one will be created
919403575c4SFelix Kuehling 	 * during restore
920403575c4SFelix Kuehling 	 */
921403575c4SFelix Kuehling 	struct dma_fence *ef;
92226103436SFelix Kuehling 
92326103436SFelix Kuehling 	/* Work items for evicting and restoring BOs */
92426103436SFelix Kuehling 	struct delayed_work eviction_work;
92526103436SFelix Kuehling 	struct delayed_work restore_work;
92626103436SFelix Kuehling 	/* seqno of the last scheduled eviction */
92726103436SFelix Kuehling 	unsigned int last_eviction_seqno;
92826103436SFelix Kuehling 	/* Approx. the last timestamp (in jiffies) when the process was
92926103436SFelix Kuehling 	 * restored after an eviction
93026103436SFelix Kuehling 	 */
93126103436SFelix Kuehling 	unsigned long last_restore_timestamp;
932de9f26bbSKent Russell 
9330ab2d753SJonathan Kim 	/* Indicates device process is debug attached with reserved vmid. */
9340ab2d753SJonathan Kim 	bool debug_trap_enabled;
9350ab2d753SJonathan Kim 
9360ab2d753SJonathan Kim 	/* per-process-per device debug event fd file */
9370ab2d753SJonathan Kim 	struct file *dbg_ev_file;
9380ab2d753SJonathan Kim 
9390ab2d753SJonathan Kim 	/* If the process is a kfd debugger, we need to know so we can clean
9400ab2d753SJonathan Kim 	 * up at exit time.  If a process enables debugging on itself, it does
9410ab2d753SJonathan Kim 	 * its own clean-up, so we don't set the flag here.  We track this by
9420ab2d753SJonathan Kim 	 * counting the number of processes this process is debugging.
9430ab2d753SJonathan Kim 	 */
9440ab2d753SJonathan Kim 	atomic_t debugged_process_count;
9450ab2d753SJonathan Kim 
9460ab2d753SJonathan Kim 	/* If the process is a debugged, this is the debugger process */
9470ab2d753SJonathan Kim 	struct kfd_process *debugger_process;
9480ab2d753SJonathan Kim 
949de9f26bbSKent Russell 	/* Kobj for our procfs */
950de9f26bbSKent Russell 	struct kobject *kobj;
9516d220a7eSAmber Lin 	struct kobject *kobj_queues;
952de9f26bbSKent Russell 	struct attribute attr_pasid;
95340ce74d1SPhilip Yang 
9540ab2d753SJonathan Kim 	/* Keep track cwsr init */
9550ab2d753SJonathan Kim 	bool has_cwsr;
9560ab2d753SJonathan Kim 
9570ab2d753SJonathan Kim 	/* Exception code enable mask and status */
9580ab2d753SJonathan Kim 	uint64_t exception_enable_mask;
95944b87bb0SJonathan Kim 	uint64_t exception_status;
9600ab2d753SJonathan Kim 
96112fb1ad7SJonathan Kim 	/* Used to drain stale interrupts */
96212fb1ad7SJonathan Kim 	wait_queue_head_t wait_irq_drain;
96312fb1ad7SJonathan Kim 	bool irq_drain_is_open;
96412fb1ad7SJonathan Kim 
96542de677fSPhilip Yang 	/* shared virtual memory registered by this process */
96642de677fSPhilip Yang 	struct svm_range_list svms;
967063e33c5SAlex Sierra 
968063e33c5SAlex Sierra 	bool xnack_enabled;
969b6485bedSTao Zhou 
97044b87bb0SJonathan Kim 	/* Work area for debugger event writer worker. */
97144b87bb0SJonathan Kim 	struct work_struct debug_event_workarea;
97244b87bb0SJonathan Kim 
9730de4ec9aSJonathan Kim 	/* Tracks debug per-vmid request for debug flags */
9749b0cc30dSSrinivasan Shanmugam 	u32 dbg_flags;
9750de4ec9aSJonathan Kim 
976b6485bedSTao Zhou 	atomic_t poison;
977cd9f7910SDavid Yat Sin 	/* Queues are in paused stated because we are in the process of doing a CRIU checkpoint */
978cd9f7910SDavid Yat Sin 	bool queues_paused;
9790ab2d753SJonathan Kim 
9800ab2d753SJonathan Kim 	/* Tracks runtime enable status */
981c2d2588cSJonathan Kim 	struct semaphore runtime_enable_sema;
982455227c4SJonathan Kim 	bool is_runtime_retry;
9830ab2d753SJonathan Kim 	struct kfd_runtime_info runtime_info;
9844a488a7aSOded Gabbay };
9854a488a7aSOded Gabbay 
98664d1c3a4SFelix Kuehling #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
98764d1c3a4SFelix Kuehling extern DECLARE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
98864d1c3a4SFelix Kuehling extern struct srcu_struct kfd_processes_srcu;
98964d1c3a4SFelix Kuehling 
99076baee6cSOded Gabbay /**
991a4497974SRajneesh Bhardwaj  * typedef amdkfd_ioctl_t - typedef for ioctl function pointer.
99276baee6cSOded Gabbay  *
993a4497974SRajneesh Bhardwaj  * @filep: pointer to file structure.
994a4497974SRajneesh Bhardwaj  * @p: amdkfd process pointer.
995a4497974SRajneesh Bhardwaj  * @data: pointer to arg that was copied from user.
996a4497974SRajneesh Bhardwaj  *
997a4497974SRajneesh Bhardwaj  * Return: returns ioctl completion code.
99876baee6cSOded Gabbay  */
99976baee6cSOded Gabbay typedef int amdkfd_ioctl_t(struct file *filep, struct kfd_process *p,
100076baee6cSOded Gabbay 				void *data);
100176baee6cSOded Gabbay 
100276baee6cSOded Gabbay struct amdkfd_ioctl_desc {
100376baee6cSOded Gabbay 	unsigned int cmd;
100476baee6cSOded Gabbay 	int flags;
100576baee6cSOded Gabbay 	amdkfd_ioctl_t *func;
100676baee6cSOded Gabbay 	unsigned int cmd_drv;
100776baee6cSOded Gabbay 	const char *name;
100876baee6cSOded Gabbay };
10098dc1db31SMukul Joshi bool kfd_dev_is_large_bar(struct kfd_node *dev);
101076baee6cSOded Gabbay 
10111679ae8fSFelix Kuehling int kfd_process_create_wq(void);
101219f6d2a6SOded Gabbay void kfd_process_destroy_wq(void);
101322e3d934SDavid Belanger void kfd_cleanup_processes(void);
10140ab2d753SJonathan Kim struct kfd_process *kfd_create_process(struct task_struct *thread);
10152243f493SRajneesh Bhardwaj struct kfd_process *kfd_get_process(const struct task_struct *task);
1016c7b6bac9SFenghua Yu struct kfd_process *kfd_lookup_process_by_pasid(u32 pasid);
101726103436SFelix Kuehling struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm);
10182aeb742bSAlex Sierra 
10192aeb742bSAlex Sierra int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id);
10205fb34bd9SAlex Sierra int kfd_process_gpuid_from_node(struct kfd_process *p, struct kfd_node *node,
10215fb34bd9SAlex Sierra 				uint32_t *gpuid, uint32_t *gpuidx);
kfd_process_gpuid_from_gpuidx(struct kfd_process * p,uint32_t gpuidx,uint32_t * gpuid)10222aeb742bSAlex Sierra static inline int kfd_process_gpuid_from_gpuidx(struct kfd_process *p,
10232aeb742bSAlex Sierra 				uint32_t gpuidx, uint32_t *gpuid) {
10242aeb742bSAlex Sierra 	return gpuidx < p->n_pdds ? p->pdds[gpuidx]->dev->id : -EINVAL;
10252aeb742bSAlex Sierra }
kfd_process_device_from_gpuidx(struct kfd_process * p,uint32_t gpuidx)10262aeb742bSAlex Sierra static inline struct kfd_process_device *kfd_process_device_from_gpuidx(
10272aeb742bSAlex Sierra 				struct kfd_process *p, uint32_t gpuidx) {
10282aeb742bSAlex Sierra 	return gpuidx < p->n_pdds ? p->pdds[gpuidx] : NULL;
10292aeb742bSAlex Sierra }
10302aeb742bSAlex Sierra 
1031abb208a8SFelix Kuehling void kfd_unref_process(struct kfd_process *p);
1032c7f21978SPhilip Yang int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger);
10336b95e797SFelix Kuehling int kfd_process_restore_queues(struct kfd_process *p);
103426103436SFelix Kuehling void kfd_suspend_all_processes(void);
103526103436SFelix Kuehling int kfd_resume_all_processes(void);
103619f6d2a6SOded Gabbay 
1037bef153b7SDavid Yat Sin struct kfd_process_device *kfd_process_device_data_by_id(struct kfd_process *process,
1038bef153b7SDavid Yat Sin 							 uint32_t gpu_id);
1039bef153b7SDavid Yat Sin 
1040bef153b7SDavid Yat Sin int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id);
1041bef153b7SDavid Yat Sin 
1042b84394e2SFelix Kuehling int kfd_process_device_init_vm(struct kfd_process_device *pdd,
1043b84394e2SFelix Kuehling 			       struct file *drm_file);
10448dc1db31SMukul Joshi struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev,
104564c7f8cfSBen Goz 						struct kfd_process *p);
10468dc1db31SMukul Joshi struct kfd_process_device *kfd_get_process_device_data(struct kfd_node *dev,
1047093c7d8cSAlexey Skidanov 							struct kfd_process *p);
10488dc1db31SMukul Joshi struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
1049093c7d8cSAlexey Skidanov 							struct kfd_process *p);
105019f6d2a6SOded Gabbay 
1051063e33c5SAlex Sierra bool kfd_process_xnack_mode(struct kfd_process *p, bool supported);
1052063e33c5SAlex Sierra 
10538dc1db31SMukul Joshi int kfd_reserved_mem_mmap(struct kfd_node *dev, struct kfd_process *process,
1054373d7080SFelix Kuehling 			  struct vm_area_struct *vma);
1055373d7080SFelix Kuehling 
105652b29d73SFelix Kuehling /* KFD process API for creating and translating handles */
105752b29d73SFelix Kuehling int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
105852b29d73SFelix Kuehling 					void *mem);
105952b29d73SFelix Kuehling void *kfd_process_device_translate_handle(struct kfd_process_device *p,
106052b29d73SFelix Kuehling 					int handle);
106152b29d73SFelix Kuehling void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
106252b29d73SFelix Kuehling 					int handle);
1063011bbb03SRajneesh Bhardwaj struct kfd_process *kfd_lookup_process_by_pid(struct pid *pid);
106452b29d73SFelix Kuehling 
106519f6d2a6SOded Gabbay /* PASIDs */
106619f6d2a6SOded Gabbay int kfd_pasid_init(void);
106719f6d2a6SOded Gabbay void kfd_pasid_exit(void);
106819f6d2a6SOded Gabbay bool kfd_set_pasid_limit(unsigned int new_limit);
106919f6d2a6SOded Gabbay unsigned int kfd_get_pasid_limit(void);
1070c7b6bac9SFenghua Yu u32 kfd_pasid_alloc(void);
1071c7b6bac9SFenghua Yu void kfd_pasid_free(u32 pasid);
107219f6d2a6SOded Gabbay 
107319f6d2a6SOded Gabbay /* Doorbells */
1074ef568db7SFelix Kuehling size_t kfd_doorbell_process_slice(struct kfd_dev *kfd);
1075735df2baSFelix Kuehling int kfd_doorbell_init(struct kfd_dev *kfd);
1076735df2baSFelix Kuehling void kfd_doorbell_fini(struct kfd_dev *kfd);
10778dc1db31SMukul Joshi int kfd_doorbell_mmap(struct kfd_node *dev, struct kfd_process *process,
1078df03ef93SHarish Kasiviswanathan 		      struct vm_area_struct *vma);
1079ada2b29cSFelix Kuehling void __iomem *kfd_get_kernel_doorbell(struct kfd_dev *kfd,
108019f6d2a6SOded Gabbay 					unsigned int *doorbell_off);
108119f6d2a6SOded Gabbay void kfd_release_kernel_doorbell(struct kfd_dev *kfd, u32 __iomem *db_addr);
108219f6d2a6SOded Gabbay u32 read_kernel_doorbell(u32 __iomem *db);
1083ada2b29cSFelix Kuehling void write_kernel_doorbell(void __iomem *db, u32 value);
10849d7d0248SFelix Kuehling void write_kernel_doorbell64(void __iomem *db, u64 value);
1085339903faSYong Zhao unsigned int kfd_get_doorbell_dw_offset_in_bar(struct kfd_dev *kfd,
108659d7115dSMukul Joshi 					struct kfd_process_device *pdd,
1087ef568db7SFelix Kuehling 					unsigned int doorbell_id);
108859d7115dSMukul Joshi phys_addr_t kfd_get_process_doorbells(struct kfd_process_device *pdd);
108959d7115dSMukul Joshi int kfd_alloc_process_doorbells(struct kfd_dev *kfd,
10902105a15aSShashank Sharma 				struct kfd_process_device *pdd);
109159d7115dSMukul Joshi void kfd_free_process_doorbells(struct kfd_dev *kfd,
10922105a15aSShashank Sharma 				struct kfd_process_device *pdd);
10936e81090bSOded Gabbay /* GTT Sub-Allocator */
10946e81090bSOded Gabbay 
10958dc1db31SMukul Joshi int kfd_gtt_sa_allocate(struct kfd_node *node, unsigned int size,
10966e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj);
10976e81090bSOded Gabbay 
10988dc1db31SMukul Joshi int kfd_gtt_sa_free(struct kfd_node *node, struct kfd_mem_obj *mem_obj);
10996e81090bSOded Gabbay 
11004a488a7aSOded Gabbay extern struct device *kfd_device;
11014a488a7aSOded Gabbay 
1102de9f26bbSKent Russell /* KFD's procfs */
1103de9f26bbSKent Russell void kfd_procfs_init(void);
1104de9f26bbSKent Russell void kfd_procfs_shutdown(void);
11056d220a7eSAmber Lin int kfd_procfs_add_queue(struct queue *q);
11066d220a7eSAmber Lin void kfd_procfs_del_queue(struct queue *q);
1107de9f26bbSKent Russell 
11085b5c4e40SEvgeny Pinchuk /* Topology */
11095b5c4e40SEvgeny Pinchuk int kfd_topology_init(void);
11105b5c4e40SEvgeny Pinchuk void kfd_topology_shutdown(void);
11118dc1db31SMukul Joshi int kfd_topology_add_device(struct kfd_node *gpu);
11128dc1db31SMukul Joshi int kfd_topology_remove_device(struct kfd_node *gpu);
11133a87177eSHarish Kasiviswanathan struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
11143a87177eSHarish Kasiviswanathan 						uint32_t proximity_domain);
111546d18d51SMukul Joshi struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
111646d18d51SMukul Joshi 						uint32_t proximity_domain);
111744d8cc6fSYong Zhao struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id);
11188dc1db31SMukul Joshi struct kfd_node *kfd_device_by_id(uint32_t gpu_id);
11198dc1db31SMukul Joshi struct kfd_node *kfd_device_by_pci_dev(const struct pci_dev *pdev);
kfd_irq_is_from_node(struct kfd_node * node,uint32_t node_id,uint32_t vmid)1120f5fe7edfSMukul Joshi static inline bool kfd_irq_is_from_node(struct kfd_node *node, uint32_t node_id,
1121f5fe7edfSMukul Joshi 					uint32_t vmid)
11225fb34bd9SAlex Sierra {
1123f5fe7edfSMukul Joshi 	return (node->interrupt_bitmap & (1 << node_id)) != 0 &&
1124f5fe7edfSMukul Joshi 	       (node->compute_vmid_bitmap & (1 << vmid)) != 0;
11255fb34bd9SAlex Sierra }
kfd_node_by_irq_ids(struct amdgpu_device * adev,uint32_t node_id,uint32_t vmid)11265fb34bd9SAlex Sierra static inline struct kfd_node *kfd_node_by_irq_ids(struct amdgpu_device *adev,
1127f5fe7edfSMukul Joshi 					uint32_t node_id, uint32_t vmid) {
11285fb34bd9SAlex Sierra 	struct kfd_dev *dev = adev->kfd.dev;
11295fb34bd9SAlex Sierra 	uint32_t i;
11305fb34bd9SAlex Sierra 
1131867ecd89SMukul Joshi 	if (KFD_GC_VERSION(dev) != IP_VERSION(9, 4, 3))
11325fb34bd9SAlex Sierra 		return dev->nodes[0];
11335fb34bd9SAlex Sierra 
11345fb34bd9SAlex Sierra 	for (i = 0; i < dev->num_nodes; i++)
1135f5fe7edfSMukul Joshi 		if (kfd_irq_is_from_node(dev->nodes[i], node_id, vmid))
11365fb34bd9SAlex Sierra 			return dev->nodes[i];
11375fb34bd9SAlex Sierra 
11385fb34bd9SAlex Sierra 	return NULL;
11395fb34bd9SAlex Sierra }
11408dc1db31SMukul Joshi int kfd_topology_enum_kfd_devices(uint8_t idx, struct kfd_node **kdev);
1141520b8fb7SFelix Kuehling int kfd_numa_node_to_apic_id(int numa_node_id);
11425b5c4e40SEvgeny Pinchuk 
11434a488a7aSOded Gabbay /* Interrupts */
114412fb1ad7SJonathan Kim #define	KFD_IRQ_FENCE_CLIENTID	0xff
114512fb1ad7SJonathan Kim #define	KFD_IRQ_FENCE_SOURCEID	0xff
114612fb1ad7SJonathan Kim #define	KFD_IRQ_IS_FENCE(client, source)				\
114712fb1ad7SJonathan Kim 				((client) == KFD_IRQ_FENCE_CLIENTID &&	\
114812fb1ad7SJonathan Kim 				(source) == KFD_IRQ_FENCE_SOURCEID)
11498dc1db31SMukul Joshi int kfd_interrupt_init(struct kfd_node *dev);
11508dc1db31SMukul Joshi void kfd_interrupt_exit(struct kfd_node *dev);
11518dc1db31SMukul Joshi bool enqueue_ih_ring_entry(struct kfd_node *kfd, const void *ih_ring_entry);
11528dc1db31SMukul Joshi bool interrupt_is_wanted(struct kfd_node *dev,
115358e69886SLan Xiao 				const uint32_t *ih_ring_entry,
115458e69886SLan Xiao 				uint32_t *patched_ihre, bool *flag);
115512fb1ad7SJonathan Kim int kfd_process_drain_interrupts(struct kfd_process_device *pdd);
115612fb1ad7SJonathan Kim void kfd_process_close_interrupt_drain(unsigned int pasid);
11574a488a7aSOded Gabbay 
115819f6d2a6SOded Gabbay /* amdkfd Apertures */
115919f6d2a6SOded Gabbay int kfd_init_apertures(struct kfd_process *process);
116019f6d2a6SOded Gabbay 
11617c9631afSJay Cornwall void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
11627c9631afSJay Cornwall 				  uint64_t tba_addr,
11637c9631afSJay Cornwall 				  uint64_t tma_addr);
116450cff45eSJay Cornwall void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd,
116550cff45eSJay Cornwall 				     bool enabled);
11667c9631afSJay Cornwall 
11670ab2d753SJonathan Kim /* CWSR initialization */
11680ab2d753SJonathan Kim int kfd_process_init_cwsr_apu(struct kfd_process *process, struct file *filep);
11690ab2d753SJonathan Kim 
117036988070SRajneesh Bhardwaj /* CRIU */
117136988070SRajneesh Bhardwaj /*
117236988070SRajneesh Bhardwaj  * Need to increment KFD_CRIU_PRIV_VERSION each time a change is made to any of the CRIU private
117336988070SRajneesh Bhardwaj  * structures:
117436988070SRajneesh Bhardwaj  * kfd_criu_process_priv_data
117536988070SRajneesh Bhardwaj  * kfd_criu_device_priv_data
117636988070SRajneesh Bhardwaj  * kfd_criu_bo_priv_data
117736988070SRajneesh Bhardwaj  * kfd_criu_queue_priv_data
117836988070SRajneesh Bhardwaj  * kfd_criu_event_priv_data
117936988070SRajneesh Bhardwaj  * kfd_criu_svm_range_priv_data
118036988070SRajneesh Bhardwaj  */
118136988070SRajneesh Bhardwaj 
118236988070SRajneesh Bhardwaj #define KFD_CRIU_PRIV_VERSION 1
118336988070SRajneesh Bhardwaj 
118436988070SRajneesh Bhardwaj struct kfd_criu_process_priv_data {
118536988070SRajneesh Bhardwaj 	uint32_t version;
11864717fe3dSRajneesh Bhardwaj 	uint32_t xnack_mode;
118736988070SRajneesh Bhardwaj };
118836988070SRajneesh Bhardwaj 
118936988070SRajneesh Bhardwaj struct kfd_criu_device_priv_data {
119036988070SRajneesh Bhardwaj 	/* For future use */
119136988070SRajneesh Bhardwaj 	uint64_t reserved;
119236988070SRajneesh Bhardwaj };
119336988070SRajneesh Bhardwaj 
119436988070SRajneesh Bhardwaj struct kfd_criu_bo_priv_data {
11955ccbb057SRajneesh Bhardwaj 	uint64_t user_addr;
11965ccbb057SRajneesh Bhardwaj 	uint32_t idr_handle;
11975ccbb057SRajneesh Bhardwaj 	uint32_t mapped_gpuids[MAX_GPU_INSTANCE];
119836988070SRajneesh Bhardwaj };
119936988070SRajneesh Bhardwaj 
1200626f7b31SDavid Yat Sin /*
1201626f7b31SDavid Yat Sin  * The first 4 bytes of kfd_criu_queue_priv_data, kfd_criu_event_priv_data,
1202626f7b31SDavid Yat Sin  * kfd_criu_svm_range_priv_data is the object type
1203626f7b31SDavid Yat Sin  */
1204626f7b31SDavid Yat Sin enum kfd_criu_object_type {
1205626f7b31SDavid Yat Sin 	KFD_CRIU_OBJECT_TYPE_QUEUE,
1206626f7b31SDavid Yat Sin 	KFD_CRIU_OBJECT_TYPE_EVENT,
1207626f7b31SDavid Yat Sin 	KFD_CRIU_OBJECT_TYPE_SVM_RANGE,
1208626f7b31SDavid Yat Sin };
1209626f7b31SDavid Yat Sin 
121036988070SRajneesh Bhardwaj struct kfd_criu_svm_range_priv_data {
121136988070SRajneesh Bhardwaj 	uint32_t object_type;
121208a987a8SRajneesh Bhardwaj 	uint64_t start_addr;
121308a987a8SRajneesh Bhardwaj 	uint64_t size;
121408a987a8SRajneesh Bhardwaj 	/* Variable length array of attributes */
1215d5c83156SChangcheng Deng 	struct kfd_ioctl_svm_attribute attrs[];
121636988070SRajneesh Bhardwaj };
121736988070SRajneesh Bhardwaj 
121836988070SRajneesh Bhardwaj struct kfd_criu_queue_priv_data {
121936988070SRajneesh Bhardwaj 	uint32_t object_type;
1220626f7b31SDavid Yat Sin 	uint64_t q_address;
1221626f7b31SDavid Yat Sin 	uint64_t q_size;
1222626f7b31SDavid Yat Sin 	uint64_t read_ptr_addr;
1223626f7b31SDavid Yat Sin 	uint64_t write_ptr_addr;
1224626f7b31SDavid Yat Sin 	uint64_t doorbell_off;
1225626f7b31SDavid Yat Sin 	uint64_t eop_ring_buffer_address;
1226626f7b31SDavid Yat Sin 	uint64_t ctx_save_restore_area_address;
1227626f7b31SDavid Yat Sin 	uint32_t gpu_id;
1228626f7b31SDavid Yat Sin 	uint32_t type;
1229626f7b31SDavid Yat Sin 	uint32_t format;
1230626f7b31SDavid Yat Sin 	uint32_t q_id;
1231626f7b31SDavid Yat Sin 	uint32_t priority;
1232626f7b31SDavid Yat Sin 	uint32_t q_percent;
1233626f7b31SDavid Yat Sin 	uint32_t doorbell_id;
1234747eea07SDavid Yat Sin 	uint32_t gws;
1235626f7b31SDavid Yat Sin 	uint32_t sdma_id;
1236626f7b31SDavid Yat Sin 	uint32_t eop_ring_buffer_size;
1237626f7b31SDavid Yat Sin 	uint32_t ctx_save_restore_area_size;
1238626f7b31SDavid Yat Sin 	uint32_t ctl_stack_size;
1239626f7b31SDavid Yat Sin 	uint32_t mqd_size;
124036988070SRajneesh Bhardwaj };
124136988070SRajneesh Bhardwaj 
124236988070SRajneesh Bhardwaj struct kfd_criu_event_priv_data {
124336988070SRajneesh Bhardwaj 	uint32_t object_type;
124440e8a766SDavid Yat Sin 	uint64_t user_handle;
124540e8a766SDavid Yat Sin 	uint32_t event_id;
124640e8a766SDavid Yat Sin 	uint32_t auto_reset;
124740e8a766SDavid Yat Sin 	uint32_t type;
124840e8a766SDavid Yat Sin 	uint32_t signaled;
124940e8a766SDavid Yat Sin 
125040e8a766SDavid Yat Sin 	union {
125140e8a766SDavid Yat Sin 		struct kfd_hsa_memory_exception_data memory_exception_data;
125240e8a766SDavid Yat Sin 		struct kfd_hsa_hw_exception_data hw_exception_data;
125340e8a766SDavid Yat Sin 	};
125436988070SRajneesh Bhardwaj };
125536988070SRajneesh Bhardwaj 
1256626f7b31SDavid Yat Sin int kfd_process_get_queue_info(struct kfd_process *p,
1257626f7b31SDavid Yat Sin 			       uint32_t *num_queues,
1258626f7b31SDavid Yat Sin 			       uint64_t *priv_data_sizes);
1259626f7b31SDavid Yat Sin 
1260626f7b31SDavid Yat Sin int kfd_criu_checkpoint_queues(struct kfd_process *p,
1261626f7b31SDavid Yat Sin 			 uint8_t __user *user_priv_data,
1262626f7b31SDavid Yat Sin 			 uint64_t *priv_data_offset);
1263626f7b31SDavid Yat Sin 
1264626f7b31SDavid Yat Sin int kfd_criu_restore_queue(struct kfd_process *p,
1265626f7b31SDavid Yat Sin 			   uint8_t __user *user_priv_data,
1266626f7b31SDavid Yat Sin 			   uint64_t *priv_data_offset,
1267626f7b31SDavid Yat Sin 			   uint64_t max_priv_data_size);
126840e8a766SDavid Yat Sin 
126940e8a766SDavid Yat Sin int kfd_criu_checkpoint_events(struct kfd_process *p,
127040e8a766SDavid Yat Sin 			 uint8_t __user *user_priv_data,
127140e8a766SDavid Yat Sin 			 uint64_t *priv_data_offset);
127240e8a766SDavid Yat Sin 
127340e8a766SDavid Yat Sin int kfd_criu_restore_event(struct file *devkfd,
127440e8a766SDavid Yat Sin 			   struct kfd_process *p,
127540e8a766SDavid Yat Sin 			   uint8_t __user *user_priv_data,
127640e8a766SDavid Yat Sin 			   uint64_t *priv_data_offset,
127740e8a766SDavid Yat Sin 			   uint64_t max_priv_data_size);
127836988070SRajneesh Bhardwaj /* CRIU - End */
127936988070SRajneesh Bhardwaj 
1280ed6e6a34SBen Goz /* Queue Context Management */
1281e88a614cSEdward O'Callaghan int init_queue(struct queue **q, const struct queue_properties *properties);
1282ed6e6a34SBen Goz void uninit_queue(struct queue *q);
128345102048SBen Goz void print_queue_properties(struct queue_properties *q);
1284ed6e6a34SBen Goz void print_queue(struct queue *q);
1285ed6e6a34SBen Goz 
12864b8f589bSBen Goz struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
12878dc1db31SMukul Joshi 		struct kfd_node *dev);
12884b8f589bSBen Goz struct mqd_manager *mqd_manager_init_vi(enum KFD_MQD_TYPE type,
12898dc1db31SMukul Joshi 		struct kfd_node *dev);
1290b91d43ddSFelix Kuehling struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type,
12918dc1db31SMukul Joshi 		struct kfd_node *dev);
129214328aa5SPhilip Cox struct mqd_manager *mqd_manager_init_v10(enum KFD_MQD_TYPE type,
12938dc1db31SMukul Joshi 		struct kfd_node *dev);
1294cc009e61SMukul Joshi struct mqd_manager *mqd_manager_init_v11(enum KFD_MQD_TYPE type,
12958dc1db31SMukul Joshi 		struct kfd_node *dev);
12968dc1db31SMukul Joshi struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev);
129764c7f8cfSBen Goz void device_queue_manager_uninit(struct device_queue_manager *dqm);
12988dc1db31SMukul Joshi struct kernel_queue *kernel_queue_init(struct kfd_node *dev,
1299241f24f8SBen Goz 					enum kfd_queue_type type);
1300c2a77fdeSFelix Kuehling void kernel_queue_uninit(struct kernel_queue *kq, bool hanging);
130103e5b167STao Zhou int kfd_dqm_evict_pasid(struct device_queue_manager *dqm, u32 pasid);
1302241f24f8SBen Goz 
130345102048SBen Goz /* Process Queue Manager */
130445102048SBen Goz struct process_queue_node {
130545102048SBen Goz 	struct queue *q;
130645102048SBen Goz 	struct kernel_queue *kq;
130745102048SBen Goz 	struct list_head process_queue_list;
130845102048SBen Goz };
130945102048SBen Goz 
13109fd3f1bfSFelix Kuehling void kfd_process_dequeue_from_device(struct kfd_process_device *pdd);
13119fd3f1bfSFelix Kuehling void kfd_process_dequeue_from_all_devices(struct kfd_process *p);
131245102048SBen Goz int pqm_init(struct process_queue_manager *pqm, struct kfd_process *p);
131345102048SBen Goz void pqm_uninit(struct process_queue_manager *pqm);
131445102048SBen Goz int pqm_create_queue(struct process_queue_manager *pqm,
13158dc1db31SMukul Joshi 			    struct kfd_node *dev,
131645102048SBen Goz 			    struct file *f,
131745102048SBen Goz 			    struct queue_properties *properties,
1318e47a8b52SYong Zhao 			    unsigned int *qid,
1319e77a541fSGraham Sider 			    struct amdgpu_bo *wptr_bo,
13208668dfc3SDavid Yat Sin 			    const struct kfd_criu_queue_priv_data *q_data,
132142c6c482SDavid Yat Sin 			    const void *restore_mqd,
13223a9822d7SDavid Yat Sin 			    const void *restore_ctl_stack,
1323e47a8b52SYong Zhao 			    uint32_t *p_doorbell_offset_in_process);
132445102048SBen Goz int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid);
13257c695a2cSLang Yu int pqm_update_queue_properties(struct process_queue_manager *pqm, unsigned int qid,
132645102048SBen Goz 			struct queue_properties *p);
13277c695a2cSLang Yu int pqm_update_mqd(struct process_queue_manager *pqm, unsigned int qid,
13287c695a2cSLang Yu 			struct mqd_update_info *minfo);
1329eb82da1dSOak Zeng int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
1330eb82da1dSOak Zeng 			void *gws);
1331fbeb661bSYair Shachar struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
1332fbeb661bSYair Shachar 						unsigned int qid);
13335bb4b78bSOak Zeng struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
13345bb4b78bSOak Zeng 						unsigned int qid);
13355df099e8SJay Cornwall int pqm_get_wave_state(struct process_queue_manager *pqm,
13365df099e8SJay Cornwall 		       unsigned int qid,
13375df099e8SJay Cornwall 		       void __user *ctl_stack,
13385df099e8SJay Cornwall 		       u32 *ctl_stack_used_size,
13395df099e8SJay Cornwall 		       u32 *save_area_used_size);
1340b17bd5dbSJonathan Kim int pqm_get_queue_snapshot(struct process_queue_manager *pqm,
1341b17bd5dbSJonathan Kim 			   uint64_t exception_clear_mask,
1342b17bd5dbSJonathan Kim 			   void __user *buf,
1343b17bd5dbSJonathan Kim 			   int *num_qss_entries,
1344b17bd5dbSJonathan Kim 			   uint32_t *entry_size);
134545102048SBen Goz 
1346b010affeSQu Huang int amdkfd_fence_wait_timeout(uint64_t *fence_addr,
1347b010affeSQu Huang 			      uint64_t fence_value,
13488c72c3d7SYong Zhao 			      unsigned int timeout_ms);
1349788bf83dSYair Shachar 
135042c6c482SDavid Yat Sin int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm,
135142c6c482SDavid Yat Sin 				  unsigned int qid,
13523a9822d7SDavid Yat Sin 				  u32 *mqd_size,
13533a9822d7SDavid Yat Sin 				  u32 *ctl_stack_size);
1354ed6e6a34SBen Goz /* Packet Manager */
1355ed6e6a34SBen Goz 
135664c7f8cfSBen Goz #define KFD_FENCE_COMPLETED (100)
135764c7f8cfSBen Goz #define KFD_FENCE_INIT   (10)
1358241f24f8SBen Goz 
1359ed6e6a34SBen Goz struct packet_manager {
1360ed6e6a34SBen Goz 	struct device_queue_manager *dqm;
1361ed6e6a34SBen Goz 	struct kernel_queue *priv_queue;
1362ed6e6a34SBen Goz 	struct mutex lock;
1363ed6e6a34SBen Goz 	bool allocated;
1364ed6e6a34SBen Goz 	struct kfd_mem_obj *ib_buffer_obj;
1365851a645eSFelix Kuehling 	unsigned int ib_size_bytes;
1366819ec5acSFelix Kuehling 	bool is_over_subscription;
1367f6e27ff1SFelix Kuehling 
1368f6e27ff1SFelix Kuehling 	const struct packet_manager_funcs *pmf;
1369ed6e6a34SBen Goz };
1370ed6e6a34SBen Goz 
1371f6e27ff1SFelix Kuehling struct packet_manager_funcs {
1372f6e27ff1SFelix Kuehling 	/* Support ASIC-specific packet formats for PM4 packets */
1373f6e27ff1SFelix Kuehling 	int (*map_process)(struct packet_manager *pm, uint32_t *buffer,
1374f6e27ff1SFelix Kuehling 			struct qcm_process_device *qpd);
1375f6e27ff1SFelix Kuehling 	int (*runlist)(struct packet_manager *pm, uint32_t *buffer,
1376f6e27ff1SFelix Kuehling 			uint64_t ib, size_t ib_size_in_dwords, bool chain);
1377f6e27ff1SFelix Kuehling 	int (*set_resources)(struct packet_manager *pm, uint32_t *buffer,
1378f6e27ff1SFelix Kuehling 			struct scheduling_resources *res);
1379f6e27ff1SFelix Kuehling 	int (*map_queues)(struct packet_manager *pm, uint32_t *buffer,
1380f6e27ff1SFelix Kuehling 			struct queue *q, bool is_static);
1381f6e27ff1SFelix Kuehling 	int (*unmap_queues)(struct packet_manager *pm, uint32_t *buffer,
1382f6e27ff1SFelix Kuehling 			enum kfd_unmap_queues_filter mode,
1383d2cb0b21SJonathan Kim 			uint32_t filter_param, bool reset);
13847cee6a68SJonathan Kim 	int (*set_grace_period)(struct packet_manager *pm, uint32_t *buffer,
13857cee6a68SJonathan Kim 			uint32_t grace_period);
1386f6e27ff1SFelix Kuehling 	int (*query_status)(struct packet_manager *pm, uint32_t *buffer,
1387b010affeSQu Huang 			uint64_t fence_address,	uint64_t fence_value);
1388f6e27ff1SFelix Kuehling 	int (*release_mem)(uint64_t gpu_addr, uint32_t *buffer);
1389f6e27ff1SFelix Kuehling 
1390f6e27ff1SFelix Kuehling 	/* Packet sizes */
1391f6e27ff1SFelix Kuehling 	int map_process_size;
1392f6e27ff1SFelix Kuehling 	int runlist_size;
1393f6e27ff1SFelix Kuehling 	int set_resources_size;
1394f6e27ff1SFelix Kuehling 	int map_queues_size;
1395f6e27ff1SFelix Kuehling 	int unmap_queues_size;
13967cee6a68SJonathan Kim 	int set_grace_period_size;
1397f6e27ff1SFelix Kuehling 	int query_status_size;
1398f6e27ff1SFelix Kuehling 	int release_mem_size;
1399f6e27ff1SFelix Kuehling };
1400f6e27ff1SFelix Kuehling 
1401f6e27ff1SFelix Kuehling extern const struct packet_manager_funcs kfd_vi_pm_funcs;
1402454150b1SFelix Kuehling extern const struct packet_manager_funcs kfd_v9_pm_funcs;
1403fd6a440eSJonathan Kim extern const struct packet_manager_funcs kfd_aldebaran_pm_funcs;
1404f6e27ff1SFelix Kuehling 
140564c7f8cfSBen Goz int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm);
1406c2a77fdeSFelix Kuehling void pm_uninit(struct packet_manager *pm, bool hanging);
140764c7f8cfSBen Goz int pm_send_set_resources(struct packet_manager *pm,
140864c7f8cfSBen Goz 				struct scheduling_resources *res);
140964c7f8cfSBen Goz int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues);
141064c7f8cfSBen Goz int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
1411b010affeSQu Huang 				uint64_t fence_value);
141264c7f8cfSBen Goz 
1413d2cb0b21SJonathan Kim int pm_send_unmap_queue(struct packet_manager *pm,
14147da2bcf8SYong Zhao 			enum kfd_unmap_queues_filter mode,
1415d2cb0b21SJonathan Kim 			uint32_t filter_param, bool reset);
141664c7f8cfSBen Goz 
1417241f24f8SBen Goz void pm_release_ib(struct packet_manager *pm);
1418241f24f8SBen Goz 
14197cee6a68SJonathan Kim int pm_update_grace_period(struct packet_manager *pm, uint32_t grace_period);
14207cee6a68SJonathan Kim 
1421454150b1SFelix Kuehling /* Following PM funcs can be shared among VI and AI */
1422454150b1SFelix Kuehling unsigned int pm_build_pm4_header(unsigned int opcode, size_t packet_size);
142314328aa5SPhilip Cox 
142419f6d2a6SOded Gabbay uint64_t kfd_get_number_elems(struct kfd_dev *kfd);
142519f6d2a6SOded Gabbay 
1426f3a39818SAndrew Lewycky /* Events */
1427f3a39818SAndrew Lewycky extern const struct kfd_event_interrupt_class event_interrupt_class_cik;
1428ca750681SFelix Kuehling extern const struct kfd_event_interrupt_class event_interrupt_class_v9;
1429d4300362SMukul Joshi extern const struct kfd_event_interrupt_class event_interrupt_class_v9_4_3;
143012fb1ad7SJonathan Kim extern const struct kfd_event_interrupt_class event_interrupt_class_v10;
1431cc009e61SMukul Joshi extern const struct kfd_event_interrupt_class event_interrupt_class_v11;
1432ca750681SFelix Kuehling 
1433930c5ff4SAlexey Skidanov extern const struct kfd_device_global_init_class device_global_init_class_cik;
1434f3a39818SAndrew Lewycky 
1435c3eb12dfSFelix Kuehling int kfd_event_init_process(struct kfd_process *p);
1436f3a39818SAndrew Lewycky void kfd_event_free_process(struct kfd_process *p);
1437f3a39818SAndrew Lewycky int kfd_event_mmap(struct kfd_process *process, struct vm_area_struct *vma);
1438f3a39818SAndrew Lewycky int kfd_wait_on_events(struct kfd_process *p,
143959d3e8beSAlexey Skidanov 		       uint32_t num_events, void __user *data,
1440bea9a56aSFelix Kuehling 		       bool all, uint32_t *user_timeout_ms,
1441fdf0c833SFelix Kuehling 		       uint32_t *wait_result);
1442c7b6bac9SFenghua Yu void kfd_signal_event_interrupt(u32 pasid, uint32_t partial_id,
1443f3a39818SAndrew Lewycky 				uint32_t valid_id_bits);
1444c7b6bac9SFenghua Yu void kfd_signal_hw_exception_event(u32 pasid);
1445f3a39818SAndrew Lewycky int kfd_set_event(struct kfd_process *p, uint32_t event_id);
1446f3a39818SAndrew Lewycky int kfd_reset_event(struct kfd_process *p, uint32_t event_id);
144740e8a766SDavid Yat Sin int kfd_kmap_event_page(struct kfd_process *p, uint64_t event_page_offset);
144840e8a766SDavid Yat Sin 
1449f3a39818SAndrew Lewycky int kfd_event_create(struct file *devkfd, struct kfd_process *p,
1450f3a39818SAndrew Lewycky 		     uint32_t event_type, bool auto_reset, uint32_t node_id,
1451f3a39818SAndrew Lewycky 		     uint32_t *event_id, uint32_t *event_trigger_data,
1452f3a39818SAndrew Lewycky 		     uint64_t *event_page_offset, uint32_t *event_slot_index);
145340e8a766SDavid Yat Sin 
145440e8a766SDavid Yat Sin int kfd_get_num_events(struct kfd_process *p);
1455f3a39818SAndrew Lewycky int kfd_event_destroy(struct kfd_process *p, uint32_t event_id);
1456f3a39818SAndrew Lewycky 
14578dc1db31SMukul Joshi void kfd_signal_vm_fault_event(struct kfd_node *dev, u32 pasid,
1458c2d2588cSJonathan Kim 				struct kfd_vm_fault_info *info,
1459c2d2588cSJonathan Kim 				struct kfd_hsa_memory_exception_data *data);
14602640c3faSshaoyunl 
14618dc1db31SMukul Joshi void kfd_signal_reset_event(struct kfd_node *dev);
1462e42051d2SShaoyun Liu 
14638dc1db31SMukul Joshi void kfd_signal_poison_consumed_event(struct kfd_node *dev, u32 pasid);
1464e2b1f9f5SDennis Li 
14653543b055SEric Huang void kfd_flush_tlb(struct kfd_process_device *pdd, enum TLB_FLUSH_TYPE type);
1466403575c4SFelix Kuehling 
kfd_flush_tlb_after_unmap(struct kfd_dev * dev)1467459ccca5SLang Yu static inline bool kfd_flush_tlb_after_unmap(struct kfd_dev *dev)
1468459ccca5SLang Yu {
146926843936SEric Huang 	return KFD_GC_VERSION(dev) >= IP_VERSION(9, 4, 2) ||
147075dda67cSPhilip Yang 	       (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 1) && dev->sdma_fw_version >= 18) ||
1471459ccca5SLang Yu 	       KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 0);
1472459ccca5SLang Yu }
1473459ccca5SLang Yu 
1474c2d2588cSJonathan Kim int kfd_send_exception_to_runtime(struct kfd_process *p,
1475c2d2588cSJonathan Kim 				unsigned int queue_id,
1476c2d2588cSJonathan Kim 				uint64_t error_reason);
1477e42051d2SShaoyun Liu bool kfd_is_locked(void);
1478e42051d2SShaoyun Liu 
1479f756e631SHarish Kasiviswanathan /* Compute profile */
14808dc1db31SMukul Joshi void kfd_inc_compute_active(struct kfd_node *dev);
14818dc1db31SMukul Joshi void kfd_dec_compute_active(struct kfd_node *dev);
1482f756e631SHarish Kasiviswanathan 
14836b855f7bSHarish Kasiviswanathan /* Cgroup Support */
14846b855f7bSHarish Kasiviswanathan /* Check with device cgroup if @kfd device is accessible */
kfd_devcgroup_check_permission(struct kfd_node * node)14855a1bd214SMukul Joshi static inline int kfd_devcgroup_check_permission(struct kfd_node *node)
14866b855f7bSHarish Kasiviswanathan {
1487eec8fd02SOdin Ugedal #if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)
14885a1bd214SMukul Joshi 	struct drm_device *ddev;
14895a1bd214SMukul Joshi 
14905a1bd214SMukul Joshi 	if (node->xcp)
14915a1bd214SMukul Joshi 		ddev = node->xcp->ddev;
14925a1bd214SMukul Joshi 	else
14935a1bd214SMukul Joshi 		ddev = adev_to_drm(node->adev);
14946b855f7bSHarish Kasiviswanathan 
149599c7b309SLorenz Brun 	return devcgroup_check_permission(DEVCG_DEV_CHAR, DRM_MAJOR,
14966b855f7bSHarish Kasiviswanathan 					  ddev->render->index,
14976b855f7bSHarish Kasiviswanathan 					  DEVCG_ACC_WRITE | DEVCG_ACC_READ);
14986b855f7bSHarish Kasiviswanathan #else
14996b855f7bSHarish Kasiviswanathan 	return 0;
15006b855f7bSHarish Kasiviswanathan #endif
15016b855f7bSHarish Kasiviswanathan }
15026b855f7bSHarish Kasiviswanathan 
kfd_is_first_node(struct kfd_node * node)150374c5b85dSMukul Joshi static inline bool kfd_is_first_node(struct kfd_node *node)
150474c5b85dSMukul Joshi {
150574c5b85dSMukul Joshi 	return (node == node->kfd->nodes[0]);
150674c5b85dSMukul Joshi }
150774c5b85dSMukul Joshi 
1508851a645eSFelix Kuehling /* Debugfs */
1509851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
1510851a645eSFelix Kuehling 
1511851a645eSFelix Kuehling void kfd_debugfs_init(void);
1512851a645eSFelix Kuehling void kfd_debugfs_fini(void);
1513851a645eSFelix Kuehling int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data);
1514851a645eSFelix Kuehling int pqm_debugfs_mqds(struct seq_file *m, void *data);
1515851a645eSFelix Kuehling int kfd_debugfs_hqds_by_device(struct seq_file *m, void *data);
1516851a645eSFelix Kuehling int dqm_debugfs_hqds(struct seq_file *m, void *data);
1517851a645eSFelix Kuehling int kfd_debugfs_rls_by_device(struct seq_file *m, void *data);
1518851a645eSFelix Kuehling int pm_debugfs_runlist(struct seq_file *m, void *data);
1519851a645eSFelix Kuehling 
15208dc1db31SMukul Joshi int kfd_debugfs_hang_hws(struct kfd_node *dev);
1521a29ec470SShaoyun Liu int pm_debugfs_hang_hws(struct packet_manager *pm);
15224f942aaeSOak Zeng int dqm_debugfs_hang_hws(struct device_queue_manager *dqm);
1523a29ec470SShaoyun Liu 
1524851a645eSFelix Kuehling #else
1525851a645eSFelix Kuehling 
kfd_debugfs_init(void)1526851a645eSFelix Kuehling static inline void kfd_debugfs_init(void) {}
kfd_debugfs_fini(void)1527851a645eSFelix Kuehling static inline void kfd_debugfs_fini(void) {}
1528851a645eSFelix Kuehling 
1529851a645eSFelix Kuehling #endif
1530851a645eSFelix Kuehling 
15314a488a7aSOded Gabbay #endif
1532