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/mmu_context.h> 30 #include <kgd_kfd_interface.h> 31 #include <drm/ttm/ttm_execbuf_util.h> 32 #include "amdgpu_sync.h" 33 #include "amdgpu_vm.h" 34 35 extern const struct kgd2kfd_calls *kgd2kfd; 36 37 struct amdgpu_device; 38 39 struct kfd_bo_va_list { 40 struct list_head bo_list; 41 struct amdgpu_bo_va *bo_va; 42 void *kgd_dev; 43 bool is_mapped; 44 uint64_t va; 45 uint64_t pte_flags; 46 }; 47 48 struct kgd_mem { 49 struct mutex lock; 50 struct amdgpu_bo *bo; 51 struct list_head bo_va_list; 52 /* protected by amdkfd_process_info.lock */ 53 struct ttm_validate_buffer validate_list; 54 struct ttm_validate_buffer resv_list; 55 uint32_t domain; 56 unsigned int mapped_to_gpu_memory; 57 uint64_t va; 58 59 uint32_t mapping_flags; 60 61 struct amdkfd_process_info *process_info; 62 63 struct amdgpu_sync sync; 64 65 bool aql_queue; 66 }; 67 68 /* KFD Memory Eviction */ 69 struct amdgpu_amdkfd_fence { 70 struct dma_fence base; 71 struct mm_struct *mm; 72 spinlock_t lock; 73 char timeline_name[TASK_COMM_LEN]; 74 }; 75 76 struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context, 77 struct mm_struct *mm); 78 bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm); 79 struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f); 80 81 struct amdkfd_process_info { 82 /* List head of all VMs that belong to a KFD process */ 83 struct list_head vm_list_head; 84 /* List head for all KFD BOs that belong to a KFD process. */ 85 struct list_head kfd_bo_list; 86 /* Lock to protect kfd_bo_list */ 87 struct mutex lock; 88 89 /* Number of VMs */ 90 unsigned int n_vms; 91 /* Eviction Fence */ 92 struct amdgpu_amdkfd_fence *eviction_fence; 93 }; 94 95 /* struct amdkfd_vm - 96 * For Memory Eviction KGD requires a mechanism to keep track of all KFD BOs 97 * belonging to a KFD process. All the VMs belonging to the same process point 98 * to the same amdkfd_process_info. 99 */ 100 struct amdkfd_vm { 101 /* Keep base as the first parameter for pointer compatibility between 102 * amdkfd_vm and amdgpu_vm. 103 */ 104 struct amdgpu_vm base; 105 106 /* List node in amdkfd_process_info.vm_list_head*/ 107 struct list_head vm_list_node; 108 109 struct amdgpu_device *adev; 110 /* Points to the KFD process VM info*/ 111 struct amdkfd_process_info *process_info; 112 113 uint64_t pd_phys_addr; 114 }; 115 116 int amdgpu_amdkfd_init(void); 117 void amdgpu_amdkfd_fini(void); 118 119 void amdgpu_amdkfd_suspend(struct amdgpu_device *adev); 120 int amdgpu_amdkfd_resume(struct amdgpu_device *adev); 121 void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev, 122 const void *ih_ring_entry); 123 void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev); 124 void amdgpu_amdkfd_device_init(struct amdgpu_device *adev); 125 void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev); 126 127 int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine, 128 uint32_t vmid, uint64_t gpu_addr, 129 uint32_t *ib_cmd, uint32_t ib_len); 130 131 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void); 132 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void); 133 134 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid); 135 136 /* Shared API */ 137 int alloc_gtt_mem(struct kgd_dev *kgd, size_t size, 138 void **mem_obj, uint64_t *gpu_addr, 139 void **cpu_ptr); 140 void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj); 141 void get_local_mem_info(struct kgd_dev *kgd, 142 struct kfd_local_mem_info *mem_info); 143 uint64_t get_gpu_clock_counter(struct kgd_dev *kgd); 144 145 uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd); 146 void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info); 147 uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd); 148 149 #define read_user_wptr(mmptr, wptr, dst) \ 150 ({ \ 151 bool valid = false; \ 152 if ((mmptr) && (wptr)) { \ 153 if ((mmptr) == current->mm) { \ 154 valid = !get_user((dst), (wptr)); \ 155 } else if (current->mm == NULL) { \ 156 use_mm(mmptr); \ 157 valid = !get_user((dst), (wptr)); \ 158 unuse_mm(mmptr); \ 159 } \ 160 } \ 161 valid; \ 162 }) 163 164 /* GPUVM API */ 165 int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, void **vm, 166 void **process_info, 167 struct dma_fence **ef); 168 void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm); 169 uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm); 170 int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( 171 struct kgd_dev *kgd, uint64_t va, uint64_t size, 172 void *vm, struct kgd_mem **mem, 173 uint64_t *offset, uint32_t flags); 174 int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( 175 struct kgd_dev *kgd, struct kgd_mem *mem); 176 int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( 177 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); 178 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( 179 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); 180 int amdgpu_amdkfd_gpuvm_sync_memory( 181 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr); 182 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd, 183 struct kgd_mem *mem, void **kptr, uint64_t *size); 184 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info, 185 struct dma_fence **ef); 186 187 void amdgpu_amdkfd_gpuvm_init_mem_limits(void); 188 void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo); 189 190 #endif /* AMDGPU_AMDKFD_H_INCLUDED */ 191