1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /* amdgpu_amdkfd.h defines the private interface between amdgpu and amdkfd. */
24 
25 #ifndef AMDGPU_AMDKFD_H_INCLUDED
26 #define AMDGPU_AMDKFD_H_INCLUDED
27 
28 #include <linux/types.h>
29 #include <linux/mm.h>
30 #include <linux/workqueue.h>
31 #include <kgd_kfd_interface.h>
32 #include <drm/ttm/ttm_execbuf_util.h>
33 #include "amdgpu_sync.h"
34 #include "amdgpu_vm.h"
35 
36 extern const struct kgd2kfd_calls *kgd2kfd;
37 extern uint64_t amdgpu_amdkfd_total_mem_size;
38 
39 struct amdgpu_device;
40 
41 struct kfd_bo_va_list {
42 	struct list_head bo_list;
43 	struct amdgpu_bo_va *bo_va;
44 	void *kgd_dev;
45 	bool is_mapped;
46 	uint64_t va;
47 	uint64_t pte_flags;
48 };
49 
50 struct kgd_mem {
51 	struct mutex lock;
52 	struct amdgpu_bo *bo;
53 	struct list_head bo_va_list;
54 	/* protected by amdkfd_process_info.lock */
55 	struct ttm_validate_buffer validate_list;
56 	struct ttm_validate_buffer resv_list;
57 	uint32_t domain;
58 	unsigned int mapped_to_gpu_memory;
59 	uint64_t va;
60 
61 	uint32_t mapping_flags;
62 
63 	atomic_t invalid;
64 	struct amdkfd_process_info *process_info;
65 	struct page **user_pages;
66 
67 	struct amdgpu_sync sync;
68 
69 	bool aql_queue;
70 };
71 
72 /* KFD Memory Eviction */
73 struct amdgpu_amdkfd_fence {
74 	struct dma_fence base;
75 	struct mm_struct *mm;
76 	spinlock_t lock;
77 	char timeline_name[TASK_COMM_LEN];
78 };
79 
80 struct amdgpu_kfd_dev {
81 	struct kfd_dev *dev;
82 	uint64_t vram_used;
83 };
84 
85 struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
86 						       struct mm_struct *mm);
87 bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm);
88 struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f);
89 
90 struct amdkfd_process_info {
91 	/* List head of all VMs that belong to a KFD process */
92 	struct list_head vm_list_head;
93 	/* List head for all KFD BOs that belong to a KFD process. */
94 	struct list_head kfd_bo_list;
95 	/* List of userptr BOs that are valid or invalid */
96 	struct list_head userptr_valid_list;
97 	struct list_head userptr_inval_list;
98 	/* Lock to protect kfd_bo_list */
99 	struct mutex lock;
100 
101 	/* Number of VMs */
102 	unsigned int n_vms;
103 	/* Eviction Fence */
104 	struct amdgpu_amdkfd_fence *eviction_fence;
105 
106 	/* MMU-notifier related fields */
107 	atomic_t evicted_bos;
108 	struct delayed_work restore_userptr_work;
109 	struct pid *pid;
110 };
111 
112 int amdgpu_amdkfd_init(void);
113 void amdgpu_amdkfd_fini(void);
114 
115 void amdgpu_amdkfd_suspend(struct amdgpu_device *adev);
116 int amdgpu_amdkfd_resume(struct amdgpu_device *adev);
117 void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
118 			const void *ih_ring_entry);
119 void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);
120 void amdgpu_amdkfd_device_init(struct amdgpu_device *adev);
121 void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev);
122 
123 int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm);
124 int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine,
125 				uint32_t vmid, uint64_t gpu_addr,
126 				uint32_t *ib_cmd, uint32_t ib_len);
127 void amdgpu_amdkfd_set_compute_idle(struct kgd_dev *kgd, bool idle);
128 
129 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void);
130 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void);
131 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_9_0_get_functions(void);
132 
133 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid);
134 
135 int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev);
136 
137 int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev);
138 
139 void amdgpu_amdkfd_gpu_reset(struct kgd_dev *kgd);
140 
141 /* Shared API */
142 int amdgpu_amdkfd_alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
143 				void **mem_obj, uint64_t *gpu_addr,
144 				void **cpu_ptr, bool mqd_gfx9);
145 void amdgpu_amdkfd_free_gtt_mem(struct kgd_dev *kgd, void *mem_obj);
146 void amdgpu_amdkfd_get_local_mem_info(struct kgd_dev *kgd,
147 				      struct kfd_local_mem_info *mem_info);
148 uint64_t amdgpu_amdkfd_get_gpu_clock_counter(struct kgd_dev *kgd);
149 
150 uint32_t amdgpu_amdkfd_get_max_engine_clock_in_mhz(struct kgd_dev *kgd);
151 void amdgpu_amdkfd_get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info);
152 int amdgpu_amdkfd_get_dmabuf_info(struct kgd_dev *kgd, int dma_buf_fd,
153 				  struct kgd_dev **dmabuf_kgd,
154 				  uint64_t *bo_size, void *metadata_buffer,
155 				  size_t buffer_size, uint32_t *metadata_size,
156 				  uint32_t *flags);
157 uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd);
158 uint64_t amdgpu_amdkfd_get_hive_id(struct kgd_dev *kgd);
159 
160 #define read_user_wptr(mmptr, wptr, dst)				\
161 	({								\
162 		bool valid = false;					\
163 		if ((mmptr) && (wptr)) {				\
164 			if ((mmptr) == current->mm) {			\
165 				valid = !get_user((dst), (wptr));	\
166 			} else if (current->mm == NULL) {		\
167 				use_mm(mmptr);				\
168 				valid = !get_user((dst), (wptr));	\
169 				unuse_mm(mmptr);			\
170 			}						\
171 		}							\
172 		valid;							\
173 	})
174 
175 /* GPUVM API */
176 int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, unsigned int pasid,
177 					void **vm, void **process_info,
178 					struct dma_fence **ef);
179 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd,
180 					struct file *filp, unsigned int pasid,
181 					void **vm, void **process_info,
182 					struct dma_fence **ef);
183 void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
184 				struct amdgpu_vm *vm);
185 void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm);
186 void amdgpu_amdkfd_gpuvm_release_process_vm(struct kgd_dev *kgd, void *vm);
187 uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm);
188 int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
189 		struct kgd_dev *kgd, uint64_t va, uint64_t size,
190 		void *vm, struct kgd_mem **mem,
191 		uint64_t *offset, uint32_t flags);
192 int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
193 		struct kgd_dev *kgd, struct kgd_mem *mem);
194 int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
195 		struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
196 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
197 		struct kgd_dev *kgd, struct kgd_mem *mem, void *vm);
198 int amdgpu_amdkfd_gpuvm_sync_memory(
199 		struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
200 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
201 		struct kgd_mem *mem, void **kptr, uint64_t *size);
202 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
203 					    struct dma_fence **ef);
204 
205 int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
206 					      struct kfd_vm_fault_info *info);
207 
208 int amdgpu_amdkfd_gpuvm_import_dmabuf(struct kgd_dev *kgd,
209 				      struct dma_buf *dmabuf,
210 				      uint64_t va, void *vm,
211 				      struct kgd_mem **mem, uint64_t *size,
212 				      uint64_t *mmap_offset);
213 
214 void amdgpu_amdkfd_gpuvm_init_mem_limits(void);
215 void amdgpu_amdkfd_unreserve_memory_limit(struct amdgpu_bo *bo);
216 
217 #endif /* AMDGPU_AMDKFD_H_INCLUDED */
218