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/mmu_context.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 const struct kgd2kfd_calls *kgd2kfd; 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 struct amdkfd_process_info *process_info; 63 64 struct amdgpu_sync sync; 65 66 bool aql_queue; 67 }; 68 69 /* KFD Memory Eviction */ 70 struct amdgpu_amdkfd_fence { 71 struct dma_fence base; 72 struct mm_struct *mm; 73 spinlock_t lock; 74 char timeline_name[TASK_COMM_LEN]; 75 }; 76 77 struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context, 78 struct mm_struct *mm); 79 bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm); 80 struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f); 81 82 struct amdkfd_process_info { 83 /* List head of all VMs that belong to a KFD process */ 84 struct list_head vm_list_head; 85 /* List head for all KFD BOs that belong to a KFD process. */ 86 struct list_head kfd_bo_list; 87 /* Lock to protect kfd_bo_list */ 88 struct mutex lock; 89 90 /* Number of VMs */ 91 unsigned int n_vms; 92 /* Eviction Fence */ 93 struct amdgpu_amdkfd_fence *eviction_fence; 94 }; 95 96 int amdgpu_amdkfd_init(void); 97 void amdgpu_amdkfd_fini(void); 98 99 void amdgpu_amdkfd_suspend(struct amdgpu_device *adev); 100 int amdgpu_amdkfd_resume(struct amdgpu_device *adev); 101 void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev, 102 const void *ih_ring_entry); 103 void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev); 104 void amdgpu_amdkfd_device_init(struct amdgpu_device *adev); 105 void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev); 106 107 int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine, 108 uint32_t vmid, uint64_t gpu_addr, 109 uint32_t *ib_cmd, uint32_t ib_len); 110 111 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void); 112 struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void); 113 114 bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid); 115 116 /* Shared API */ 117 int alloc_gtt_mem(struct kgd_dev *kgd, size_t size, 118 void **mem_obj, uint64_t *gpu_addr, 119 void **cpu_ptr); 120 void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj); 121 void get_local_mem_info(struct kgd_dev *kgd, 122 struct kfd_local_mem_info *mem_info); 123 uint64_t get_gpu_clock_counter(struct kgd_dev *kgd); 124 125 uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd); 126 void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info); 127 uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd); 128 129 #define read_user_wptr(mmptr, wptr, dst) \ 130 ({ \ 131 bool valid = false; \ 132 if ((mmptr) && (wptr)) { \ 133 if ((mmptr) == current->mm) { \ 134 valid = !get_user((dst), (wptr)); \ 135 } else if (current->mm == NULL) { \ 136 use_mm(mmptr); \ 137 valid = !get_user((dst), (wptr)); \ 138 unuse_mm(mmptr); \ 139 } \ 140 } \ 141 valid; \ 142 }) 143 144 /* GPUVM API */ 145 int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, void **vm, 146 void **process_info, 147 struct dma_fence **ef); 148 int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd, 149 struct file *filp, 150 void **vm, void **process_info, 151 struct dma_fence **ef); 152 void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev, 153 struct amdgpu_vm *vm); 154 void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm); 155 uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm); 156 int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( 157 struct kgd_dev *kgd, uint64_t va, uint64_t size, 158 void *vm, struct kgd_mem **mem, 159 uint64_t *offset, uint32_t flags); 160 int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( 161 struct kgd_dev *kgd, struct kgd_mem *mem); 162 int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( 163 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); 164 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( 165 struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); 166 int amdgpu_amdkfd_gpuvm_sync_memory( 167 struct kgd_dev *kgd, struct kgd_mem *mem, bool intr); 168 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd, 169 struct kgd_mem *mem, void **kptr, uint64_t *size); 170 int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info, 171 struct dma_fence **ef); 172 173 void amdgpu_amdkfd_gpuvm_init_mem_limits(void); 174 void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo); 175 176 #endif /* AMDGPU_AMDKFD_H_INCLUDED */ 177