2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
23 /* amdgpu_amdkfd.h defines the private interface between amdgpu and amdkfd. */
25 #ifndef AMDGPU_AMDKFD_H_INCLUDED
26 #define AMDGPU_AMDKFD_H_INCLUDED
28 #include <linux/types.h>
30 #include <linux/kthread.h>
31 #include <linux/workqueue.h>
32 #include <kgd_kfd_interface.h>
33 #include <drm/ttm/ttm_execbuf_util.h>
34 #include "amdgpu_sync.h"
35 #include "amdgpu_vm.h"
37 extern uint64_t amdgpu_amdkfd_total_mem_size;
41 TLB_FLUSH_LIGHTWEIGHT,
47 enum kfd_mem_attachment_type {
48 KFD_MEM_ATT_SHARED, /* Share kgd_mem->bo or another attachment's */
49 KFD_MEM_ATT_USERPTR, /* SG bo to DMA map pages from a userptr bo */
50 KFD_MEM_ATT_DMABUF, /* DMAbuf to DMA map TTM BOs */
53 struct kfd_mem_attachment {
54 struct list_head list;
55 enum kfd_mem_attachment_type type;
57 struct amdgpu_bo_va *bo_va;
58 struct amdgpu_device *adev;
66 struct dma_buf *dmabuf;
67 struct list_head attachments;
68 /* protected by amdkfd_process_info.lock */
69 struct ttm_validate_buffer validate_list;
70 struct ttm_validate_buffer resv_list;
72 unsigned int mapped_to_gpu_memory;
78 struct amdkfd_process_info *process_info;
80 struct amdgpu_sync sync;
86 /* KFD Memory Eviction */
87 struct amdgpu_amdkfd_fence {
88 struct dma_fence base;
91 char timeline_name[TASK_COMM_LEN];
92 struct svm_range_bo *svm_bo;
95 struct amdgpu_kfd_dev {
101 enum kgd_engine_type {
114 struct amdkfd_process_info {
115 /* List head of all VMs that belong to a KFD process */
116 struct list_head vm_list_head;
117 /* List head for all KFD BOs that belong to a KFD process. */
118 struct list_head kfd_bo_list;
119 /* List of userptr BOs that are valid or invalid */
120 struct list_head userptr_valid_list;
121 struct list_head userptr_inval_list;
122 /* Lock to protect kfd_bo_list */
128 struct amdgpu_amdkfd_fence *eviction_fence;
130 /* MMU-notifier related fields */
131 atomic_t evicted_bos;
132 struct delayed_work restore_userptr_work;
136 int amdgpu_amdkfd_init(void);
137 void amdgpu_amdkfd_fini(void);
139 void amdgpu_amdkfd_suspend(struct amdgpu_device *adev, bool run_pm);
140 int amdgpu_amdkfd_resume_iommu(struct amdgpu_device *adev);
141 int amdgpu_amdkfd_resume(struct amdgpu_device *adev, bool run_pm);
142 void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev,
143 const void *ih_ring_entry);
144 void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev);
145 void amdgpu_amdkfd_device_init(struct amdgpu_device *adev);
146 void amdgpu_amdkfd_device_fini_sw(struct amdgpu_device *adev);
147 int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine,
148 uint32_t vmid, uint64_t gpu_addr,
149 uint32_t *ib_cmd, uint32_t ib_len);
150 void amdgpu_amdkfd_set_compute_idle(struct kgd_dev *kgd, bool idle);
151 bool amdgpu_amdkfd_have_atomics_support(struct kgd_dev *kgd);
152 int amdgpu_amdkfd_flush_gpu_tlb_vmid(struct kgd_dev *kgd, uint16_t vmid);
153 int amdgpu_amdkfd_flush_gpu_tlb_pasid(struct kgd_dev *kgd, uint16_t pasid,
154 enum TLB_FLUSH_TYPE flush_type);
156 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid);
158 int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev);
160 int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev);
162 void amdgpu_amdkfd_gpu_reset(struct kgd_dev *kgd);
164 int amdgpu_queue_mask_bit_to_set_resource_bit(struct amdgpu_device *adev,
167 struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context,
168 struct mm_struct *mm,
169 struct svm_range_bo *svm_bo);
170 #if IS_ENABLED(CONFIG_HSA_AMD)
171 bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm);
172 struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f);
173 int amdgpu_amdkfd_remove_fence_on_pt_pd_bos(struct amdgpu_bo *bo);
174 int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm);
177 bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm)
183 struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f)
189 int amdgpu_amdkfd_remove_fence_on_pt_pd_bos(struct amdgpu_bo *bo)
195 int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm)
201 int amdgpu_amdkfd_alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
202 void **mem_obj, uint64_t *gpu_addr,
203 void **cpu_ptr, bool mqd_gfx9);
204 void amdgpu_amdkfd_free_gtt_mem(struct kgd_dev *kgd, void *mem_obj);
205 int amdgpu_amdkfd_alloc_gws(struct kgd_dev *kgd, size_t size, void **mem_obj);
206 void amdgpu_amdkfd_free_gws(struct kgd_dev *kgd, void *mem_obj);
207 int amdgpu_amdkfd_add_gws_to_process(void *info, void *gws, struct kgd_mem **mem);
208 int amdgpu_amdkfd_remove_gws_from_process(void *info, void *mem);
209 uint32_t amdgpu_amdkfd_get_fw_version(struct kgd_dev *kgd,
210 enum kgd_engine_type type);
211 void amdgpu_amdkfd_get_local_mem_info(struct kgd_dev *kgd,
212 struct kfd_local_mem_info *mem_info);
213 uint64_t amdgpu_amdkfd_get_gpu_clock_counter(struct kgd_dev *kgd);
215 uint32_t amdgpu_amdkfd_get_max_engine_clock_in_mhz(struct kgd_dev *kgd);
216 void amdgpu_amdkfd_get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info);
217 int amdgpu_amdkfd_get_dmabuf_info(struct kgd_dev *kgd, int dma_buf_fd,
218 struct kgd_dev **dmabuf_kgd,
219 uint64_t *bo_size, void *metadata_buffer,
220 size_t buffer_size, uint32_t *metadata_size,
222 uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd);
223 uint64_t amdgpu_amdkfd_get_hive_id(struct kgd_dev *kgd);
224 uint64_t amdgpu_amdkfd_get_unique_id(struct kgd_dev *kgd);
225 uint64_t amdgpu_amdkfd_get_mmio_remap_phys_addr(struct kgd_dev *kgd);
226 uint32_t amdgpu_amdkfd_get_num_gws(struct kgd_dev *kgd);
227 uint32_t amdgpu_amdkfd_get_asic_rev_id(struct kgd_dev *kgd);
228 int amdgpu_amdkfd_get_noretry(struct kgd_dev *kgd);
229 uint8_t amdgpu_amdkfd_get_xgmi_hops_count(struct kgd_dev *dst, struct kgd_dev *src);
230 int amdgpu_amdkfd_get_xgmi_bandwidth_mbytes(struct kgd_dev *dst, struct kgd_dev *src, bool is_min);
231 int amdgpu_amdkfd_get_pcie_bandwidth_mbytes(struct kgd_dev *dev, bool is_min);
233 /* Read user wptr from a specified user address space with page fault
234 * disabled. The memory must be pinned and mapped to the hardware when
235 * this is called in hqd_load functions, so it should never fault in
236 * the first place. This resolves a circular lock dependency involving
237 * four locks, including the DQM lock and mmap_lock.
239 #define read_user_wptr(mmptr, wptr, dst) \
241 bool valid = false; \
242 if ((mmptr) && (wptr)) { \
243 pagefault_disable(); \
244 if ((mmptr) == current->mm) { \
245 valid = !get_user((dst), (wptr)); \
246 } else if (current->flags & PF_KTHREAD) { \
247 kthread_use_mm(mmptr); \
248 valid = !get_user((dst), (wptr)); \
249 kthread_unuse_mm(mmptr); \
251 pagefault_enable(); \
257 #define drm_priv_to_vm(drm_priv) \
258 (&((struct amdgpu_fpriv *) \
259 ((struct drm_file *)(drm_priv))->driver_priv)->vm)
261 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd,
262 struct file *filp, u32 pasid,
264 struct dma_fence **ef);
265 void amdgpu_amdkfd_gpuvm_release_process_vm(struct kgd_dev *kgd, void *drm_priv);
266 uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *drm_priv);
267 int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
268 struct kgd_dev *kgd, uint64_t va, uint64_t size,
269 void *drm_priv, struct kgd_mem **mem,
270 uint64_t *offset, uint32_t flags);
271 int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
272 struct kgd_dev *kgd, struct kgd_mem *mem, void *drm_priv,
274 int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
275 struct kgd_dev *kgd, struct kgd_mem *mem, void *drm_priv, bool *table_freed);
276 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
277 struct kgd_dev *kgd, struct kgd_mem *mem, void *drm_priv);
278 int amdgpu_amdkfd_gpuvm_sync_memory(
279 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr);
280 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
281 struct kgd_mem *mem, void **kptr, uint64_t *size);
282 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info,
283 struct dma_fence **ef);
284 int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
285 struct kfd_vm_fault_info *info);
286 int amdgpu_amdkfd_gpuvm_import_dmabuf(struct kgd_dev *kgd,
287 struct dma_buf *dmabuf,
288 uint64_t va, void *drm_priv,
289 struct kgd_mem **mem, uint64_t *size,
290 uint64_t *mmap_offset);
291 int amdgpu_amdkfd_get_tile_config(struct kgd_dev *kgd,
292 struct tile_config *config);
293 #if IS_ENABLED(CONFIG_HSA_AMD)
294 void amdgpu_amdkfd_gpuvm_init_mem_limits(void);
295 void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
296 struct amdgpu_vm *vm);
297 void amdgpu_amdkfd_unreserve_memory_limit(struct amdgpu_bo *bo);
298 void amdgpu_amdkfd_reserve_system_mem(uint64_t size);
301 void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
306 void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
307 struct amdgpu_vm *vm)
312 void amdgpu_amdkfd_unreserve_memory_limit(struct amdgpu_bo *bo)
316 /* KGD2KFD callbacks */
317 int kgd2kfd_quiesce_mm(struct mm_struct *mm);
318 int kgd2kfd_resume_mm(struct mm_struct *mm);
319 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
320 struct dma_fence *fence);
321 #if IS_ENABLED(CONFIG_HSA_AMD)
322 int kgd2kfd_init(void);
323 void kgd2kfd_exit(void);
324 struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev,
325 unsigned int asic_type, bool vf);
326 bool kgd2kfd_device_init(struct kfd_dev *kfd,
327 struct drm_device *ddev,
328 const struct kgd2kfd_shared_resources *gpu_resources);
329 void kgd2kfd_device_exit(struct kfd_dev *kfd);
330 void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm);
331 int kgd2kfd_resume_iommu(struct kfd_dev *kfd);
332 int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm);
333 int kgd2kfd_pre_reset(struct kfd_dev *kfd);
334 int kgd2kfd_post_reset(struct kfd_dev *kfd);
335 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry);
336 void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd);
337 void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask);
339 static inline int kgd2kfd_init(void)
344 static inline void kgd2kfd_exit(void)
349 struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev,
350 unsigned int asic_type, bool vf)
356 bool kgd2kfd_device_init(struct kfd_dev *kfd, struct drm_device *ddev,
357 const struct kgd2kfd_shared_resources *gpu_resources)
362 static inline void kgd2kfd_device_exit(struct kfd_dev *kfd)
366 static inline void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
370 static int __maybe_unused kgd2kfd_resume_iommu(struct kfd_dev *kfd)
375 static inline int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
380 static inline int kgd2kfd_pre_reset(struct kfd_dev *kfd)
385 static inline int kgd2kfd_post_reset(struct kfd_dev *kfd)
391 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
396 void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
401 void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)
405 #endif /* AMDGPU_AMDKFD_H_INCLUDED */