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_gfx_10_0_get_functions(void); 144 145 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid); 146 147 int amdgpu_amdkfd_pre_reset(struct amdgpu_device *adev); 148 149 int amdgpu_amdkfd_post_reset(struct amdgpu_device *adev); 150 151 void amdgpu_amdkfd_gpu_reset(struct kgd_dev *kgd); 152 153 /* Shared API */ 154 int amdgpu_amdkfd_alloc_gtt_mem(struct kgd_dev *kgd, size_t size, 155 void **mem_obj, uint64_t *gpu_addr, 156 void **cpu_ptr, bool mqd_gfx9); 157 void amdgpu_amdkfd_free_gtt_mem(struct kgd_dev *kgd, void *mem_obj); 158 int amdgpu_amdkfd_alloc_gws(struct kgd_dev *kgd, size_t size, void **mem_obj); 159 void amdgpu_amdkfd_free_gws(struct kgd_dev *kgd, void *mem_obj); 160 int amdgpu_amdkfd_add_gws_to_process(void *info, void *gws, struct kgd_mem **mem); 161 int amdgpu_amdkfd_remove_gws_from_process(void *info, void *mem); 162 uint32_t amdgpu_amdkfd_get_fw_version(struct kgd_dev *kgd, 163 enum kgd_engine_type type); 164 void amdgpu_amdkfd_get_local_mem_info(struct kgd_dev *kgd, 165 struct kfd_local_mem_info *mem_info); 166 uint64_t amdgpu_amdkfd_get_gpu_clock_counter(struct kgd_dev *kgd); 167 168 uint32_t amdgpu_amdkfd_get_max_engine_clock_in_mhz(struct kgd_dev *kgd); 169 void amdgpu_amdkfd_get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info); 170 int amdgpu_amdkfd_get_dmabuf_info(struct kgd_dev *kgd, int dma_buf_fd, 171 struct kgd_dev **dmabuf_kgd, 172 uint64_t *bo_size, void *metadata_buffer, 173 size_t buffer_size, uint32_t *metadata_size, 174 uint32_t *flags); 175 uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd); 176 uint64_t amdgpu_amdkfd_get_hive_id(struct kgd_dev *kgd); 177 uint64_t amdgpu_amdkfd_get_mmio_remap_phys_addr(struct kgd_dev *kgd); 178 uint32_t amdgpu_amdkfd_get_num_gws(struct kgd_dev *kgd); 179 uint8_t amdgpu_amdkfd_get_xgmi_hops_count(struct kgd_dev *dst, struct kgd_dev *src); 180 181 #define read_user_wptr(mmptr, wptr, dst) \ 182 ({ \ 183 bool valid = false; \ 184 if ((mmptr) && (wptr)) { \ 185 if ((mmptr) == current->mm) { \ 186 valid = !get_user((dst), (wptr)); \ 187 } else if (current->mm == NULL) { \ 188 use_mm(mmptr); \ 189 valid = !get_user((dst), (wptr)); \ 190 unuse_mm(mmptr); \ 191 } \ 192 } \ 193 valid; \ 194 }) 195 196 /* GPUVM API */ 197 int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, unsigned int pasid, 198 void **vm, void **process_info, 199 struct dma_fence **ef); 200 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd, 201 struct file *filp, unsigned int pasid, 202 void **vm, void **process_info, 203 struct dma_fence **ef); 204 void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev, 205 struct amdgpu_vm *vm); 206 void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm); 207 void amdgpu_amdkfd_gpuvm_release_process_vm(struct kgd_dev *kgd, void *vm); 208 uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm); 209 int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( 210 struct kgd_dev *kgd, uint64_t va, uint64_t size, 211 void *vm, struct kgd_mem **mem, 212 uint64_t *offset, uint32_t flags); 213 int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( 214 struct kgd_dev *kgd, struct kgd_mem *mem); 215 int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( 216 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); 217 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( 218 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); 219 int amdgpu_amdkfd_gpuvm_sync_memory( 220 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr); 221 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd, 222 struct kgd_mem *mem, void **kptr, uint64_t *size); 223 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info, 224 struct dma_fence **ef); 225 226 int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd, 227 struct kfd_vm_fault_info *info); 228 229 int amdgpu_amdkfd_gpuvm_import_dmabuf(struct kgd_dev *kgd, 230 struct dma_buf *dmabuf, 231 uint64_t va, void *vm, 232 struct kgd_mem **mem, uint64_t *size, 233 uint64_t *mmap_offset); 234 235 void amdgpu_amdkfd_gpuvm_init_mem_limits(void); 236 void amdgpu_amdkfd_unreserve_memory_limit(struct amdgpu_bo *bo); 237 238 /* KGD2KFD callbacks */ 239 int kgd2kfd_init(void); 240 void kgd2kfd_exit(void); 241 struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev, 242 const struct kfd2kgd_calls *f2g); 243 bool kgd2kfd_device_init(struct kfd_dev *kfd, 244 const struct kgd2kfd_shared_resources *gpu_resources); 245 void kgd2kfd_device_exit(struct kfd_dev *kfd); 246 void kgd2kfd_suspend(struct kfd_dev *kfd); 247 int kgd2kfd_resume(struct kfd_dev *kfd); 248 int kgd2kfd_pre_reset(struct kfd_dev *kfd); 249 int kgd2kfd_post_reset(struct kfd_dev *kfd); 250 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry); 251 int kgd2kfd_quiesce_mm(struct mm_struct *mm); 252 int kgd2kfd_resume_mm(struct mm_struct *mm); 253 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm, 254 struct dma_fence *fence); 255 void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd); 256 257 #endif /* AMDGPU_AMDKFD_H_INCLUDED */ 258