Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 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> |
Oded Gabbay | 7420f48 | 2018-03-15 10:08:35 +0200 | [diff] [blame] | 29 | #include <linux/mm.h> |
Felix Kuehling | 70539bd | 2017-08-15 23:00:17 -0400 | [diff] [blame] | 30 | #include <linux/mmu_context.h> |
Felix Kuehling | 5ae0283 | 2018-03-23 15:32:33 -0400 | [diff] [blame] | 31 | #include <linux/workqueue.h> |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 32 | #include <kgd_kfd_interface.h> |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 33 | #include <drm/ttm/ttm_execbuf_util.h> |
| 34 | #include "amdgpu_sync.h" |
| 35 | #include "amdgpu_vm.h" |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 36 | |
Felix Kuehling | d8d019c | 2018-02-06 20:32:35 -0500 | [diff] [blame] | 37 | extern const struct kgd2kfd_calls *kgd2kfd; |
| 38 | |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 39 | struct amdgpu_device; |
| 40 | |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 41 | struct kfd_bo_va_list { |
| 42 | struct list_head bo_list; |
| 43 | struct amdgpu_bo_va *bo_va; |
| 44 | void *kgd_dev; |
| 45 | bool is_mapped; |
| 46 | uint64_t va; |
| 47 | uint64_t pte_flags; |
| 48 | }; |
| 49 | |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 50 | struct kgd_mem { |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 51 | struct mutex lock; |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 52 | struct amdgpu_bo *bo; |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 53 | struct list_head bo_va_list; |
| 54 | /* protected by amdkfd_process_info.lock */ |
| 55 | struct ttm_validate_buffer validate_list; |
| 56 | struct ttm_validate_buffer resv_list; |
| 57 | uint32_t domain; |
| 58 | unsigned int mapped_to_gpu_memory; |
| 59 | uint64_t va; |
| 60 | |
| 61 | uint32_t mapping_flags; |
| 62 | |
Felix Kuehling | 5ae0283 | 2018-03-23 15:32:33 -0400 | [diff] [blame] | 63 | atomic_t invalid; |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 64 | struct amdkfd_process_info *process_info; |
Felix Kuehling | 5ae0283 | 2018-03-23 15:32:33 -0400 | [diff] [blame] | 65 | struct page **user_pages; |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 66 | |
| 67 | struct amdgpu_sync sync; |
| 68 | |
| 69 | bool aql_queue; |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 70 | }; |
| 71 | |
Felix Kuehling | d8d019c | 2018-02-06 20:32:35 -0500 | [diff] [blame] | 72 | /* KFD Memory Eviction */ |
| 73 | struct amdgpu_amdkfd_fence { |
| 74 | struct dma_fence base; |
| 75 | struct mm_struct *mm; |
| 76 | spinlock_t lock; |
| 77 | char timeline_name[TASK_COMM_LEN]; |
| 78 | }; |
| 79 | |
| 80 | struct amdgpu_amdkfd_fence *amdgpu_amdkfd_fence_create(u64 context, |
| 81 | struct mm_struct *mm); |
| 82 | bool amdkfd_fence_check_mm(struct dma_fence *f, struct mm_struct *mm); |
| 83 | struct amdgpu_amdkfd_fence *to_amdgpu_amdkfd_fence(struct dma_fence *f); |
| 84 | |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 85 | struct amdkfd_process_info { |
| 86 | /* List head of all VMs that belong to a KFD process */ |
| 87 | struct list_head vm_list_head; |
| 88 | /* List head for all KFD BOs that belong to a KFD process. */ |
| 89 | struct list_head kfd_bo_list; |
Felix Kuehling | 5ae0283 | 2018-03-23 15:32:33 -0400 | [diff] [blame] | 90 | /* List of userptr BOs that are valid or invalid */ |
| 91 | struct list_head userptr_valid_list; |
| 92 | struct list_head userptr_inval_list; |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 93 | /* Lock to protect kfd_bo_list */ |
| 94 | struct mutex lock; |
| 95 | |
| 96 | /* Number of VMs */ |
| 97 | unsigned int n_vms; |
| 98 | /* Eviction Fence */ |
| 99 | struct amdgpu_amdkfd_fence *eviction_fence; |
Felix Kuehling | 5ae0283 | 2018-03-23 15:32:33 -0400 | [diff] [blame] | 100 | |
| 101 | /* MMU-notifier related fields */ |
| 102 | atomic_t evicted_bos; |
| 103 | struct delayed_work restore_userptr_work; |
| 104 | struct pid *pid; |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 105 | }; |
| 106 | |
Oded Gabbay | efb1c65 | 2016-02-09 13:30:12 +0200 | [diff] [blame] | 107 | int amdgpu_amdkfd_init(void); |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 108 | void amdgpu_amdkfd_fini(void); |
| 109 | |
Andres Rodriguez | dc102c4 | 2017-02-01 17:02:13 -0500 | [diff] [blame] | 110 | void amdgpu_amdkfd_suspend(struct amdgpu_device *adev); |
| 111 | int amdgpu_amdkfd_resume(struct amdgpu_device *adev); |
| 112 | void amdgpu_amdkfd_interrupt(struct amdgpu_device *adev, |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 113 | const void *ih_ring_entry); |
Andres Rodriguez | dc102c4 | 2017-02-01 17:02:13 -0500 | [diff] [blame] | 114 | void amdgpu_amdkfd_device_probe(struct amdgpu_device *adev); |
| 115 | void amdgpu_amdkfd_device_init(struct amdgpu_device *adev); |
| 116 | void amdgpu_amdkfd_device_fini(struct amdgpu_device *adev); |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 117 | |
Felix Kuehling | e52482d | 2018-03-23 15:32:28 -0400 | [diff] [blame] | 118 | int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, struct mm_struct *mm); |
Felix Kuehling | 4c660c8 | 2018-02-06 20:32:39 -0500 | [diff] [blame] | 119 | int amdgpu_amdkfd_submit_ib(struct kgd_dev *kgd, enum kgd_engine_type engine, |
| 120 | uint32_t vmid, uint64_t gpu_addr, |
| 121 | uint32_t *ib_cmd, uint32_t ib_len); |
| 122 | |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 123 | struct kfd2kgd_calls *amdgpu_amdkfd_gfx_7_get_functions(void); |
Ben Goz | ff758a1 | 2014-10-07 14:43:07 +0300 | [diff] [blame] | 124 | struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void); |
Felix Kuehling | d5a114a | 2018-04-10 17:33:01 -0400 | [diff] [blame] | 125 | struct kfd2kgd_calls *amdgpu_amdkfd_gfx_9_0_get_functions(void); |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 126 | |
Felix Kuehling | 155494d | 2018-02-06 20:32:36 -0500 | [diff] [blame] | 127 | bool amdgpu_amdkfd_is_kfd_vmid(struct amdgpu_device *adev, u32 vmid); |
| 128 | |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 129 | /* Shared API */ |
| 130 | int alloc_gtt_mem(struct kgd_dev *kgd, size_t size, |
| 131 | void **mem_obj, uint64_t *gpu_addr, |
| 132 | void **cpu_ptr); |
| 133 | void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj); |
Harish Kasiviswanathan | 30f1c04 | 2017-12-08 23:08:42 -0500 | [diff] [blame] | 134 | void get_local_mem_info(struct kgd_dev *kgd, |
| 135 | struct kfd_local_mem_info *mem_info); |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 136 | uint64_t get_gpu_clock_counter(struct kgd_dev *kgd); |
| 137 | |
| 138 | uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd); |
Flora Cui | ebdebf4 | 2017-12-08 23:08:40 -0500 | [diff] [blame] | 139 | void get_cu_info(struct kgd_dev *kgd, struct kfd_cu_info *cu_info); |
Kent Russell | 9f0a0b4 | 2017-12-08 23:09:05 -0500 | [diff] [blame] | 140 | uint64_t amdgpu_amdkfd_get_vram_usage(struct kgd_dev *kgd); |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 141 | |
Felix Kuehling | 70539bd | 2017-08-15 23:00:17 -0400 | [diff] [blame] | 142 | #define read_user_wptr(mmptr, wptr, dst) \ |
| 143 | ({ \ |
| 144 | bool valid = false; \ |
| 145 | if ((mmptr) && (wptr)) { \ |
| 146 | if ((mmptr) == current->mm) { \ |
| 147 | valid = !get_user((dst), (wptr)); \ |
| 148 | } else if (current->mm == NULL) { \ |
| 149 | use_mm(mmptr); \ |
| 150 | valid = !get_user((dst), (wptr)); \ |
| 151 | unuse_mm(mmptr); \ |
| 152 | } \ |
| 153 | } \ |
| 154 | valid; \ |
| 155 | }) |
| 156 | |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 157 | /* GPUVM API */ |
| 158 | int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, void **vm, |
Oded Gabbay | fcdfa43 | 2018-05-18 22:18:16 +0300 | [diff] [blame] | 159 | void **process_info, |
| 160 | struct dma_fence **ef); |
Felix Kuehling | ede0dd8 | 2018-03-15 17:27:43 -0400 | [diff] [blame] | 161 | int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd, |
Oded Gabbay | fcdfa43 | 2018-05-18 22:18:16 +0300 | [diff] [blame] | 162 | struct file *filp, |
| 163 | void **vm, void **process_info, |
| 164 | struct dma_fence **ef); |
Felix Kuehling | ede0dd8 | 2018-03-15 17:27:43 -0400 | [diff] [blame] | 165 | void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev, |
Oded Gabbay | fcdfa43 | 2018-05-18 22:18:16 +0300 | [diff] [blame] | 166 | struct amdgpu_vm *vm); |
Felix Kuehling | a46a2cd | 2018-02-06 20:32:38 -0500 | [diff] [blame] | 167 | void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm); |
| 168 | uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm); |
| 169 | int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( |
| 170 | struct kgd_dev *kgd, uint64_t va, uint64_t size, |
| 171 | void *vm, struct kgd_mem **mem, |
| 172 | uint64_t *offset, uint32_t flags); |
| 173 | int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( |
| 174 | struct kgd_dev *kgd, struct kgd_mem *mem); |
| 175 | int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( |
| 176 | struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); |
| 177 | int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( |
| 178 | struct kgd_dev *kgd, struct kgd_mem *mem, void *vm); |
| 179 | int amdgpu_amdkfd_gpuvm_sync_memory( |
| 180 | struct kgd_dev *kgd, struct kgd_mem *mem, bool intr); |
| 181 | int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd, |
| 182 | struct kgd_mem *mem, void **kptr, uint64_t *size); |
| 183 | int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info, |
| 184 | struct dma_fence **ef); |
| 185 | |
| 186 | void amdgpu_amdkfd_gpuvm_init_mem_limits(void); |
| 187 | void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo); |
| 188 | |
Oded Gabbay | 130e037 | 2015-06-12 21:35:14 +0300 | [diff] [blame] | 189 | #endif /* AMDGPU_AMDKFD_H_INCLUDED */ |