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