xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h (revision bf47afbabf1cf149f9ebc8e1f7dab6913e360dc4)
1130e0371SOded Gabbay /*
2130e0371SOded Gabbay  * Copyright 2014 Advanced Micro Devices, Inc.
3130e0371SOded Gabbay  *
4130e0371SOded Gabbay  * Permission is hereby granted, free of charge, to any person obtaining a
5130e0371SOded Gabbay  * copy of this software and associated documentation files (the "Software"),
6130e0371SOded Gabbay  * to deal in the Software without restriction, including without limitation
7130e0371SOded Gabbay  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8130e0371SOded Gabbay  * and/or sell copies of the Software, and to permit persons to whom the
9130e0371SOded Gabbay  * Software is furnished to do so, subject to the following conditions:
10130e0371SOded Gabbay  *
11130e0371SOded Gabbay  * The above copyright notice and this permission notice shall be included in
12130e0371SOded Gabbay  * all copies or substantial portions of the Software.
13130e0371SOded Gabbay  *
14130e0371SOded Gabbay  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15130e0371SOded Gabbay  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16130e0371SOded Gabbay  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17130e0371SOded Gabbay  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18130e0371SOded Gabbay  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19130e0371SOded Gabbay  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20130e0371SOded Gabbay  * OTHER DEALINGS IN THE SOFTWARE.
21130e0371SOded Gabbay  */
22130e0371SOded Gabbay 
23130e0371SOded Gabbay /* amdgpu_amdkfd.h defines the private interface between amdgpu and amdkfd. */
24130e0371SOded Gabbay 
25130e0371SOded Gabbay #ifndef AMDGPU_AMDKFD_H_INCLUDED
26130e0371SOded Gabbay #define AMDGPU_AMDKFD_H_INCLUDED
27130e0371SOded Gabbay 
28130e0371SOded Gabbay #include <linux/types.h>
297420f482SOded Gabbay #include <linux/mm.h>
3070539bd7SFelix Kuehling #include <linux/mmu_context.h>
315ae0283eSFelix Kuehling #include <linux/workqueue.h>
32130e0371SOded Gabbay #include <kgd_kfd_interface.h>
33a46a2cd1SFelix Kuehling #include <drm/ttm/ttm_execbuf_util.h>
34a46a2cd1SFelix Kuehling #include "amdgpu_sync.h"
35a46a2cd1SFelix Kuehling #include "amdgpu_vm.h"
36130e0371SOded Gabbay 
37d8d019ccSFelix Kuehling extern const struct kgd2kfd_calls *kgd2kfd;
38d8d019ccSFelix Kuehling 
39130e0371SOded Gabbay struct amdgpu_device;
40130e0371SOded Gabbay 
41a46a2cd1SFelix Kuehling struct kfd_bo_va_list {
42a46a2cd1SFelix Kuehling 	struct list_head bo_list;
43a46a2cd1SFelix Kuehling 	struct amdgpu_bo_va *bo_va;
44a46a2cd1SFelix Kuehling 	void *kgd_dev;
45a46a2cd1SFelix Kuehling 	bool is_mapped;
46a46a2cd1SFelix Kuehling 	uint64_t va;
47a46a2cd1SFelix Kuehling 	uint64_t pte_flags;
48a46a2cd1SFelix Kuehling };
49a46a2cd1SFelix Kuehling 
50130e0371SOded Gabbay struct kgd_mem {
51a46a2cd1SFelix Kuehling 	struct mutex lock;
52130e0371SOded Gabbay 	struct amdgpu_bo *bo;
53a46a2cd1SFelix Kuehling 	struct list_head bo_va_list;
54a46a2cd1SFelix Kuehling 	/* protected by amdkfd_process_info.lock */
55a46a2cd1SFelix Kuehling 	struct ttm_validate_buffer validate_list;
56a46a2cd1SFelix Kuehling 	struct ttm_validate_buffer resv_list;
57a46a2cd1SFelix Kuehling 	uint32_t domain;
58a46a2cd1SFelix Kuehling 	unsigned int mapped_to_gpu_memory;
59a46a2cd1SFelix Kuehling 	uint64_t va;
60a46a2cd1SFelix Kuehling 
61a46a2cd1SFelix Kuehling 	uint32_t mapping_flags;
62a46a2cd1SFelix Kuehling 
635ae0283eSFelix Kuehling 	atomic_t invalid;
64a46a2cd1SFelix Kuehling 	struct amdkfd_process_info *process_info;
655ae0283eSFelix Kuehling 	struct page **user_pages;
66a46a2cd1SFelix Kuehling 
67a46a2cd1SFelix Kuehling 	struct amdgpu_sync sync;
68a46a2cd1SFelix Kuehling 
69a46a2cd1SFelix Kuehling 	bool aql_queue;
70130e0371SOded Gabbay };
71130e0371SOded Gabbay 
72d8d019ccSFelix Kuehling /* KFD Memory Eviction */
73d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence {
74d8d019ccSFelix Kuehling 	struct dma_fence base;
75d8d019ccSFelix Kuehling 	struct mm_struct *mm;
76d8d019ccSFelix Kuehling 	spinlock_t lock;
77d8d019ccSFelix Kuehling 	char timeline_name[TASK_COMM_LEN];
78d8d019ccSFelix Kuehling };
79d8d019ccSFelix Kuehling 
80d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
81d8d019ccSFelix Kuehling 						       struct mm_struct *mm);
82d8d019ccSFelix Kuehling bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm);
83d8d019ccSFelix Kuehling struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f);
84d8d019ccSFelix Kuehling 
85a46a2cd1SFelix Kuehling struct amdkfd_process_info {
86a46a2cd1SFelix Kuehling 	/* List head of all VMs that belong to a KFD process */
87a46a2cd1SFelix Kuehling 	struct list_head vm_list_head;
88a46a2cd1SFelix Kuehling 	/* List head for all KFD BOs that belong to a KFD process. */
89a46a2cd1SFelix Kuehling 	struct list_head kfd_bo_list;
905ae0283eSFelix Kuehling 	/* List of userptr BOs that are valid or invalid */
915ae0283eSFelix Kuehling 	struct list_head userptr_valid_list;
925ae0283eSFelix Kuehling 	struct list_head userptr_inval_list;
93a46a2cd1SFelix Kuehling 	/* Lock to protect kfd_bo_list */
94a46a2cd1SFelix Kuehling 	struct mutex lock;
95a46a2cd1SFelix Kuehling 
96a46a2cd1SFelix Kuehling 	/* Number of VMs */
97a46a2cd1SFelix Kuehling 	unsigned int n_vms;
98a46a2cd1SFelix Kuehling 	/* Eviction Fence */
99a46a2cd1SFelix Kuehling 	struct amdgpu_amdkfd_fence *eviction_fence;
1005ae0283eSFelix Kuehling 
1015ae0283eSFelix Kuehling 	/* MMU-notifier related fields */
1025ae0283eSFelix Kuehling 	atomic_t evicted_bos;
1035ae0283eSFelix Kuehling 	struct delayed_work restore_userptr_work;
1045ae0283eSFelix Kuehling 	struct pid *pid;
105a46a2cd1SFelix Kuehling };
106a46a2cd1SFelix Kuehling 
107efb1c658SOded Gabbay int amdgpu_amdkfd_init(void);
108130e0371SOded Gabbay void amdgpu_amdkfd_fini(void);
109130e0371SOded Gabbay 
110dc102c43SAndres Rodriguez void amdgpu_amdkfd_suspend(struct amdgpu_device *adev);
111dc102c43SAndres Rodriguez int amdgpu_amdkfd_resume(struct amdgpu_device *adev);
112dc102c43SAndres Rodriguez void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
113130e0371SOded Gabbay 			const void *ih_ring_entry);
114dc102c43SAndres Rodriguez void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);
115dc102c43SAndres Rodriguez void amdgpu_amdkfd_device_init(struct amdgpu_device *adev);
116dc102c43SAndres Rodriguez void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev);
117130e0371SOded Gabbay 
118e52482deSFelix Kuehling int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm);
1194c660c8fSFelix Kuehling int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine,
1204c660c8fSFelix Kuehling 				uint32_t vmid, uint64_t gpu_addr,
1214c660c8fSFelix Kuehling 				uint32_t *ib_cmd, uint32_t ib_len);
12201c097dbSFelix Kuehling void amdgpu_amdkfd_set_compute_idle(struct kgd_dev *kgd, bool idle);
1234c660c8fSFelix Kuehling 
124130e0371SOded Gabbay struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void);
125ff758a12SBen Goz struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void);
126d5a114a6SFelix Kuehling struct kfd2kgd_calls *amdgpu_amdkfd_gfx_9_0_get_functions(void);
127130e0371SOded Gabbay 
128155494dbSFelix Kuehling bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid);
129155494dbSFelix Kuehling 
1305c6dd71eSShaoyun Liu int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev);
1315c6dd71eSShaoyun Liu 
1325c6dd71eSShaoyun Liu int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev);
1335c6dd71eSShaoyun Liu 
13424da5a9cSShaoyun Liu void amdgpu_amdkfd_gpu_reset(struct kgd_dev *kgd);
13524da5a9cSShaoyun Liu 
136130e0371SOded Gabbay /* Shared API */
137130e0371SOded Gabbay int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
138130e0371SOded Gabbay 			void **mem_obj, uint64_t *gpu_addr,
139130e0371SOded Gabbay 			void **cpu_ptr);
140130e0371SOded Gabbay void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj);
14130f1c042SHarish Kasiviswanathan void get_local_mem_info(struct kgd_dev *kgd,
14230f1c042SHarish Kasiviswanathan 			struct kfd_local_mem_info *mem_info);
143130e0371SOded Gabbay uint64_t get_gpu_clock_counter(struct kgd_dev *kgd);
144130e0371SOded Gabbay 
145130e0371SOded Gabbay uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd);
146ebdebf42SFlora Cui void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info);
1479f0a0b41SKent Russell uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd);
148130e0371SOded Gabbay 
14970539bd7SFelix Kuehling #define read_user_wptr(mmptr, wptr, dst)				\
15070539bd7SFelix Kuehling 	({								\
15170539bd7SFelix Kuehling 		bool valid = false;					\
15270539bd7SFelix Kuehling 		if ((mmptr) && (wptr)) {				\
15370539bd7SFelix Kuehling 			if ((mmptr) == current->mm) {			\
15470539bd7SFelix Kuehling 				valid = !get_user((dst), (wptr));	\
15570539bd7SFelix Kuehling 			} else if (current->mm == NULL) {		\
15670539bd7SFelix Kuehling 				use_mm(mmptr);				\
15770539bd7SFelix Kuehling 				valid = !get_user((dst), (wptr));	\
15870539bd7SFelix Kuehling 				unuse_mm(mmptr);			\
15970539bd7SFelix Kuehling 			}						\
16070539bd7SFelix Kuehling 		}							\
16170539bd7SFelix Kuehling 		valid;							\
16270539bd7SFelix Kuehling 	})
16370539bd7SFelix Kuehling 
164a46a2cd1SFelix Kuehling /* GPUVM API */
1651685b01aSOak Zeng int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, unsigned int pasid,
1661685b01aSOak Zeng 					void **vm, void **process_info,
167a46a2cd1SFelix Kuehling 					struct dma_fence **ef);
168ede0dd86SFelix Kuehling int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd,
1691685b01aSOak Zeng 					struct file *filp, unsigned int pasid,
170ede0dd86SFelix Kuehling 					void **vm, void **process_info,
171ede0dd86SFelix Kuehling 					struct dma_fence **ef);
172ede0dd86SFelix Kuehling void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
173ede0dd86SFelix Kuehling 				struct amdgpu_vm *vm);
174a46a2cd1SFelix Kuehling void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm);
175*bf47afbaSOak Zeng void amdgpu_amdkfd_gpuvm_release_process_vm(struct kgd_dev *kgd, void *vm);
176a46a2cd1SFelix Kuehling uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm);
177a46a2cd1SFelix Kuehling int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
178a46a2cd1SFelix Kuehling 		struct kgd_dev *kgd, uint64_t va, uint64_t size,
179a46a2cd1SFelix Kuehling 		void *vm, struct kgd_mem **mem,
180a46a2cd1SFelix Kuehling 		uint64_t *offset, uint32_t flags);
181a46a2cd1SFelix Kuehling int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
182a46a2cd1SFelix Kuehling 		struct kgd_dev *kgd, struct kgd_mem *mem);
183a46a2cd1SFelix Kuehling int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
184a46a2cd1SFelix Kuehling 		struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
185a46a2cd1SFelix Kuehling int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
186a46a2cd1SFelix Kuehling 		struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
187a46a2cd1SFelix Kuehling int amdgpu_amdkfd_gpuvm_sync_memory(
188a46a2cd1SFelix Kuehling 		struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
189a46a2cd1SFelix Kuehling int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
190a46a2cd1SFelix Kuehling 		struct kgd_mem *mem, void **kptr, uint64_t *size);
191a46a2cd1SFelix Kuehling int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
192a46a2cd1SFelix Kuehling 					    struct dma_fence **ef);
193a46a2cd1SFelix Kuehling 
194b97dfa27Sshaoyunl int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
195b97dfa27Sshaoyunl 					      struct kfd_vm_fault_info *info);
196b97dfa27Sshaoyunl 
197a46a2cd1SFelix Kuehling void amdgpu_amdkfd_gpuvm_init_mem_limits(void);
198a46a2cd1SFelix Kuehling void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo);
199a46a2cd1SFelix Kuehling 
200130e0371SOded Gabbay #endif /* AMDGPU_AMDKFD_H_INCLUDED */
201