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 uint64_t amdgpu_amdkfd_total_mem_size; 37 38 struct amdgpu_device; 39 40 struct kfd_bo_va_list { 41 struct list_head bo_list; 42 struct amdgpu_bo_va *bo_va; 43 void *kgd_dev; 44 bool is_mapped; 45 uint64_t va; 46 uint64_t pte_flags; 47 }; 48 49 struct kgd_mem { 50 struct mutex lock; 51 struct amdgpu_bo *bo; 52 struct list_head bo_va_list; 53 /* protected by amdkfd_process_info.lock */ 54 struct ttm_validate_buffer validate_list; 55 struct ttm_validate_buffer resv_list; 56 uint32_t domain; 57 unsigned int mapped_to_gpu_memory; 58 uint64_t va; 59 60 uint32_t mapping_flags; 61 62 atomic_t invalid; 63 struct amdkfd_process_info *process_info; 64 65 struct amdgpu_sync sync; 66 67 bool aql_queue; 68 }; 69 70 /* KFD Memory Eviction */ 71 struct amdgpu_amdkfd_fence { 72 struct dma_fence base; 73 struct mm_struct *mm; 74 spinlock_t lock; 75 char timeline_name[TASK_COMM_LEN]; 76 }; 77 78 struct amdgpu_kfd_dev { 79 struct kfd_dev *dev; 80 uint64_t vram_used; 81 }; 82 83 enum kgd_engine_type { 84 KGD_ENGINE_PFP = 1, 85 KGD_ENGINE_ME, 86 KGD_ENGINE_CE, 87 KGD_ENGINE_MEC1, 88 KGD_ENGINE_MEC2, 89 KGD_ENGINE_RLC, 90 KGD_ENGINE_SDMA1, 91 KGD_ENGINE_SDMA2, 92 KGD_ENGINE_MAX 93 }; 94 95 struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context, 96 struct mm_struct *mm); 97 bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm); 98 struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f); 99 100 struct amdkfd_process_info { 101 /* List head of all VMs that belong to a KFD process */ 102 struct list_head vm_list_head; 103 /* List head for all KFD BOs that belong to a KFD process. */ 104 struct list_head kfd_bo_list; 105 /* List of userptr BOs that are valid or invalid */ 106 struct list_head userptr_valid_list; 107 struct list_head userptr_inval_list; 108 /* Lock to protect kfd_bo_list */ 109 struct mutex lock; 110 111 /* Number of VMs */ 112 unsigned int n_vms; 113 /* Eviction Fence */ 114 struct amdgpu_amdkfd_fence *eviction_fence; 115 116 /* MMU-notifier related fields */ 117 atomic_t evicted_bos; 118 struct delayed_work restore_userptr_work; 119 struct pid *pid; 120 }; 121 122 int amdgpu_amdkfd_init(void); 123 void amdgpu_amdkfd_fini(void); 124 125 void amdgpu_amdkfd_suspend(struct amdgpu_device *adev); 126 int amdgpu_amdkfd_resume(struct amdgpu_device *adev); 127 void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev, 128 const void *ih_ring_entry); 129 void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev); 130 void amdgpu_amdkfd_device_init(struct amdgpu_device *adev); 131 void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev); 132 133 int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm); 134 int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine, 135 uint32_t vmid, uint64_t gpu_addr, 136 uint32_t *ib_cmd, uint32_t ib_len); 137 void amdgpu_amdkfd_set_compute_idle(struct kgd_dev *kgd, bool idle); 138 bool amdgpu_amdkfd_have_atomics_support(struct kgd_dev *kgd); 139 140 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void); 141 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void); 142 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_9_0_get_functions(void); 143 struct kfd2kgd_calls *amdgpu_amdkfd_arcturus_get_functions(void); 144 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_10_0_get_functions(void); 145 146 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid); 147 148 int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev); 149 150 int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev); 151 152 void amdgpu_amdkfd_gpu_reset(struct kgd_dev *kgd); 153 154 /* Shared API */ 155 int amdgpu_amdkfd_alloc_gtt_mem(struct kgd_dev *kgd, size_t size, 156 void **mem_obj, uint64_t *gpu_addr, 157 void **cpu_ptr, bool mqd_gfx9); 158 void amdgpu_amdkfd_free_gtt_mem(struct kgd_dev *kgd, void *mem_obj); 159 int amdgpu_amdkfd_alloc_gws(struct kgd_dev *kgd, size_t size, void **mem_obj); 160 void amdgpu_amdkfd_free_gws(struct kgd_dev *kgd, void *mem_obj); 161 int amdgpu_amdkfd_add_gws_to_process(void *info, void *gws, struct kgd_mem **mem); 162 int amdgpu_amdkfd_remove_gws_from_process(void *info, void *mem); 163 uint32_t amdgpu_amdkfd_get_fw_version(struct kgd_dev *kgd, 164 enum kgd_engine_type type); 165 void amdgpu_amdkfd_get_local_mem_info(struct kgd_dev *kgd, 166 struct kfd_local_mem_info *mem_info); 167 uint64_t amdgpu_amdkfd_get_gpu_clock_counter(struct kgd_dev *kgd); 168 169 uint32_t amdgpu_amdkfd_get_max_engine_clock_in_mhz(struct kgd_dev *kgd); 170 void amdgpu_amdkfd_get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info); 171 int amdgpu_amdkfd_get_dmabuf_info(struct kgd_dev *kgd, int dma_buf_fd, 172 struct kgd_dev **dmabuf_kgd, 173 uint64_t *bo_size, void *metadata_buffer, 174 size_t buffer_size, uint32_t *metadata_size, 175 uint32_t *flags); 176 uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd); 177 uint64_t amdgpu_amdkfd_get_hive_id(struct kgd_dev *kgd); 178 uint64_t amdgpu_amdkfd_get_mmio_remap_phys_addr(struct kgd_dev *kgd); 179 uint32_t amdgpu_amdkfd_get_num_gws(struct kgd_dev *kgd); 180 uint8_t amdgpu_amdkfd_get_xgmi_hops_count(struct kgd_dev *dst, struct kgd_dev *src); 181 182 #define read_user_wptr(mmptr, wptr, dst) \ 183 ({ \ 184 bool valid = false; \ 185 if ((mmptr) && (wptr)) { \ 186 if ((mmptr) == current->mm) { \ 187 valid = !get_user((dst), (wptr)); \ 188 } else if (current->mm == NULL) { \ 189 use_mm(mmptr); \ 190 valid = !get_user((dst), (wptr)); \ 191 unuse_mm(mmptr); \ 192 } \ 193 } \ 194 valid; \ 195 }) 196 197 /* GPUVM API */ 198 int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, unsigned int pasid, 199 void **vm, void **process_info, 200 struct dma_fence **ef); 201 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd, 202 struct file *filp, unsigned int pasid, 203 void **vm, void **process_info, 204 struct dma_fence **ef); 205 void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev, 206 struct amdgpu_vm *vm); 207 void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm); 208 void amdgpu_amdkfd_gpuvm_release_process_vm(struct kgd_dev *kgd, void *vm); 209 uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm); 210 int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( 211 struct kgd_dev *kgd, uint64_t va, uint64_t size, 212 void *vm, struct kgd_mem **mem, 213 uint64_t *offset, uint32_t flags); 214 int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( 215 struct kgd_dev *kgd, struct kgd_mem *mem); 216 int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( 217 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); 218 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( 219 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); 220 int amdgpu_amdkfd_gpuvm_sync_memory( 221 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr); 222 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd, 223 struct kgd_mem *mem, void **kptr, uint64_t *size); 224 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info, 225 struct dma_fence **ef); 226 227 int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd, 228 struct kfd_vm_fault_info *info); 229 230 int amdgpu_amdkfd_gpuvm_import_dmabuf(struct kgd_dev *kgd, 231 struct dma_buf *dmabuf, 232 uint64_t va, void *vm, 233 struct kgd_mem **mem, uint64_t *size, 234 uint64_t *mmap_offset); 235 236 void amdgpu_amdkfd_gpuvm_init_mem_limits(void); 237 void amdgpu_amdkfd_unreserve_memory_limit(struct amdgpu_bo *bo); 238 239 /* KGD2KFD callbacks */ 240 int kgd2kfd_init(void); 241 void kgd2kfd_exit(void); 242 struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev, 243 const struct kfd2kgd_calls *f2g); 244 bool kgd2kfd_device_init(struct kfd_dev *kfd, 245 const struct kgd2kfd_shared_resources *gpu_resources); 246 void kgd2kfd_device_exit(struct kfd_dev *kfd); 247 void kgd2kfd_suspend(struct kfd_dev *kfd); 248 int kgd2kfd_resume(struct kfd_dev *kfd); 249 int kgd2kfd_pre_reset(struct kfd_dev *kfd); 250 int kgd2kfd_post_reset(struct kfd_dev *kfd); 251 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry); 252 int kgd2kfd_quiesce_mm(struct mm_struct *mm); 253 int kgd2kfd_resume_mm(struct mm_struct *mm); 254 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm, 255 struct dma_fence *fence); 256 void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd); 257 258 #endif /* AMDGPU_AMDKFD_H_INCLUDED */ 259