Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | * Authors: Christian König |
| 23 | */ |
| 24 | #ifndef __AMDGPU_VM_H__ |
| 25 | #define __AMDGPU_VM_H__ |
| 26 | |
| 27 | #include <linux/rbtree.h> |
| 28 | |
| 29 | #include "gpu_scheduler.h" |
| 30 | #include "amdgpu_sync.h" |
| 31 | #include "amdgpu_ring.h" |
| 32 | |
| 33 | struct amdgpu_bo_va; |
| 34 | struct amdgpu_job; |
| 35 | struct amdgpu_bo_list_entry; |
| 36 | |
| 37 | /* |
| 38 | * GPUVM handling |
| 39 | */ |
| 40 | |
| 41 | /* maximum number of VMIDs */ |
| 42 | #define AMDGPU_NUM_VM 16 |
| 43 | |
| 44 | /* Maximum number of PTEs the hardware can write with one command */ |
| 45 | #define AMDGPU_VM_MAX_UPDATE_SIZE 0x3FFFF |
| 46 | |
| 47 | /* number of entries in page table */ |
Zhang, Jerry | 36b32a6 | 2017-03-29 16:08:32 +0800 | [diff] [blame] | 48 | #define AMDGPU_VM_PTE_COUNT(adev) (1 << (adev)->vm_manager.block_size) |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 49 | |
| 50 | /* PTBs (Page Table Blocks) need to be aligned to 32K */ |
| 51 | #define AMDGPU_VM_PTB_ALIGN_SIZE 32768 |
| 52 | |
| 53 | /* LOG2 number of continuous pages for the fragment field */ |
Christian König | 6be7adb | 2017-05-23 18:35:22 +0200 | [diff] [blame] | 54 | #define AMDGPU_LOG2_PAGES_PER_FRAG(adev) \ |
| 55 | ((adev)->asic_type < CHIP_VEGA10 ? 4 : \ |
| 56 | (adev)->vm_manager.block_size) |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 57 | |
Christian König | 35ba15f | 2017-02-13 14:22:58 +0100 | [diff] [blame] | 58 | #define AMDGPU_PTE_VALID (1ULL << 0) |
| 59 | #define AMDGPU_PTE_SYSTEM (1ULL << 1) |
| 60 | #define AMDGPU_PTE_SNOOPED (1ULL << 2) |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 61 | |
| 62 | /* VI only */ |
Christian König | 35ba15f | 2017-02-13 14:22:58 +0100 | [diff] [blame] | 63 | #define AMDGPU_PTE_EXECUTABLE (1ULL << 4) |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 64 | |
Christian König | 35ba15f | 2017-02-13 14:22:58 +0100 | [diff] [blame] | 65 | #define AMDGPU_PTE_READABLE (1ULL << 5) |
| 66 | #define AMDGPU_PTE_WRITEABLE (1ULL << 6) |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 67 | |
Alex Xie | 982a134 | 2017-02-15 14:10:19 -0500 | [diff] [blame] | 68 | #define AMDGPU_PTE_FRAG(x) ((x & 0x1fULL) << 7) |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 69 | |
Zhang, Jerry | d0766e9 | 2017-04-19 09:53:29 +0800 | [diff] [blame] | 70 | /* TILED for VEGA10, reserved for older ASICs */ |
| 71 | #define AMDGPU_PTE_PRT (1ULL << 51) |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 72 | |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 73 | /* PDE is handled as PTE for VEGA10 */ |
| 74 | #define AMDGPU_PDE_PTE (1ULL << 54) |
| 75 | |
Alex Deucher | ca02061 | 2017-03-03 15:23:14 -0500 | [diff] [blame] | 76 | /* VEGA10 only */ |
| 77 | #define AMDGPU_PTE_MTYPE(a) ((uint64_t)a << 57) |
| 78 | #define AMDGPU_PTE_MTYPE_MASK AMDGPU_PTE_MTYPE(3ULL) |
| 79 | |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 80 | /* How to programm VM fault handling */ |
| 81 | #define AMDGPU_VM_FAULT_STOP_NEVER 0 |
| 82 | #define AMDGPU_VM_FAULT_STOP_FIRST 1 |
| 83 | #define AMDGPU_VM_FAULT_STOP_ALWAYS 2 |
| 84 | |
Christian König | eb60ef2 | 2017-03-30 14:41:19 +0200 | [diff] [blame] | 85 | /* max number of VMHUB */ |
| 86 | #define AMDGPU_MAX_VMHUBS 2 |
| 87 | #define AMDGPU_GFXHUB 0 |
| 88 | #define AMDGPU_MMHUB 1 |
| 89 | |
| 90 | /* hardcode that limit for now */ |
| 91 | #define AMDGPU_VA_RESERVED_SIZE (8 << 20) |
Chunming Zhou | c350577 | 2017-04-21 15:51:04 +0800 | [diff] [blame] | 92 | /* max vmids dedicated for process */ |
| 93 | #define AMDGPU_VM_MAX_RESERVED_VMID 1 |
Christian König | eb60ef2 | 2017-03-30 14:41:19 +0200 | [diff] [blame] | 94 | |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 95 | #define AMDGPU_VM_CONTEXT_GFX 0 |
| 96 | #define AMDGPU_VM_CONTEXT_COMPUTE 1 |
| 97 | |
| 98 | /* See vm_update_mode */ |
| 99 | #define AMDGPU_VM_USE_CPU_FOR_GFX (1 << 0) |
| 100 | #define AMDGPU_VM_USE_CPU_FOR_COMPUTE (1 << 1) |
| 101 | |
| 102 | |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 103 | struct amdgpu_vm_pt { |
| 104 | struct amdgpu_bo *bo; |
| 105 | uint64_t addr; |
Alex Deucher | cf2f0a3 | 2017-07-25 16:35:38 -0400 | [diff] [blame] | 106 | bool huge_page; |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 107 | |
| 108 | /* array of page tables, one for each directory entry */ |
| 109 | struct amdgpu_vm_pt *entries; |
| 110 | unsigned last_entry_used; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | struct amdgpu_vm { |
| 114 | /* tree of virtual addresses mapped */ |
| 115 | struct rb_root va; |
| 116 | |
| 117 | /* protecting invalidated */ |
| 118 | spinlock_t status_lock; |
| 119 | |
| 120 | /* BOs moved, but not yet updated in the PT */ |
| 121 | struct list_head invalidated; |
| 122 | |
| 123 | /* BOs cleared in the PT because of a move */ |
| 124 | struct list_head cleared; |
| 125 | |
| 126 | /* BO mappings freed, but not yet updated in the PT */ |
| 127 | struct list_head freed; |
| 128 | |
| 129 | /* contains the page directory */ |
Christian König | 67003a1 | 2016-10-12 14:46:26 +0200 | [diff] [blame] | 130 | struct amdgpu_vm_pt root; |
Christian König | a24960f | 2016-10-12 13:20:52 +0200 | [diff] [blame] | 131 | struct dma_fence *last_dir_update; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 132 | uint64_t last_eviction_counter; |
| 133 | |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 134 | /* protecting freed */ |
| 135 | spinlock_t freed_lock; |
| 136 | |
| 137 | /* Scheduler entity for page table updates */ |
| 138 | struct amd_sched_entity entity; |
| 139 | |
| 140 | /* client id */ |
| 141 | u64 client_id; |
Chunming Zhou | 36bbf3b | 2017-04-20 16:17:34 +0800 | [diff] [blame] | 142 | /* dedicated to vm */ |
| 143 | struct amdgpu_vm_id *reserved_vmid[AMDGPU_MAX_VMHUBS]; |
Monk Liu | bd7de27 | 2017-01-09 15:23:17 +0800 | [diff] [blame] | 144 | /* each VM will map on CSA */ |
| 145 | struct amdgpu_bo_va *csa_bo_va; |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 146 | |
| 147 | /* Flag to indicate if VM tables are updated by CPU or GPU (SDMA) */ |
| 148 | bool use_cpu_for_update; |
Yong Zhao | 51ac7ee | 2017-07-27 12:48:22 -0400 | [diff] [blame] | 149 | |
| 150 | /* Flag to indicate ATS support from PTE for GFX9 */ |
| 151 | bool pte_support_ats; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | struct amdgpu_vm_id { |
| 155 | struct list_head list; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 156 | struct amdgpu_sync active; |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 157 | struct dma_fence *last_flush; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 158 | atomic64_t owner; |
| 159 | |
| 160 | uint64_t pd_gpu_addr; |
| 161 | /* last flushed PD/PT update */ |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 162 | struct dma_fence *flushed_updates; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 163 | |
| 164 | uint32_t current_gpu_reset_count; |
| 165 | |
| 166 | uint32_t gds_base; |
| 167 | uint32_t gds_size; |
| 168 | uint32_t gws_base; |
| 169 | uint32_t gws_size; |
| 170 | uint32_t oa_base; |
| 171 | uint32_t oa_size; |
| 172 | }; |
| 173 | |
Christian König | 7645670 | 2017-04-06 17:52:39 +0200 | [diff] [blame] | 174 | struct amdgpu_vm_id_manager { |
| 175 | struct mutex lock; |
| 176 | unsigned num_ids; |
| 177 | struct list_head ids_lru; |
| 178 | struct amdgpu_vm_id ids[AMDGPU_NUM_VM]; |
Chunming Zhou | c350577 | 2017-04-21 15:51:04 +0800 | [diff] [blame] | 179 | atomic_t reserved_vmid_num; |
Christian König | 7645670 | 2017-04-06 17:52:39 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 182 | struct amdgpu_vm_manager { |
| 183 | /* Handling of VMIDs */ |
Christian König | 7645670 | 2017-04-06 17:52:39 +0200 | [diff] [blame] | 184 | struct amdgpu_vm_id_manager id_mgr[AMDGPU_MAX_VMHUBS]; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 185 | |
| 186 | /* Handling of VM fences */ |
| 187 | u64 fence_context; |
| 188 | unsigned seqno[AMDGPU_MAX_RINGS]; |
| 189 | |
Felix Kuehling | 22770e5 | 2017-03-28 20:24:53 -0400 | [diff] [blame] | 190 | uint64_t max_pfn; |
Christian König | 8437a09 | 2016-10-17 15:08:10 +0200 | [diff] [blame] | 191 | uint32_t num_level; |
Zhang, Jerry | 36b32a6 | 2017-03-29 16:08:32 +0800 | [diff] [blame] | 192 | uint64_t vm_size; |
| 193 | uint32_t block_size; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 194 | /* vram base address for page table entry */ |
| 195 | u64 vram_base_offset; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 196 | /* vm pte handling */ |
| 197 | const struct amdgpu_vm_pte_funcs *vm_pte_funcs; |
| 198 | struct amdgpu_ring *vm_pte_rings[AMDGPU_MAX_RINGS]; |
| 199 | unsigned vm_pte_num_rings; |
| 200 | atomic_t vm_pte_next_ring; |
| 201 | /* client id counter */ |
| 202 | atomic64_t client_counter; |
Christian König | 284710f | 2017-01-30 11:09:31 +0100 | [diff] [blame] | 203 | |
| 204 | /* partial resident texture handling */ |
| 205 | spinlock_t prt_lock; |
Christian König | 451bc8e | 2017-02-14 16:02:52 +0100 | [diff] [blame] | 206 | atomic_t num_prt_users; |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 207 | |
| 208 | /* controls how VM page tables are updated for Graphics and Compute. |
| 209 | * BIT0[= 0] Graphics updated by SDMA [= 1] by CPU |
| 210 | * BIT1[= 0] Compute updated by SDMA [= 1] by CPU |
| 211 | */ |
| 212 | int vm_update_mode; |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | void amdgpu_vm_manager_init(struct amdgpu_device *adev); |
| 216 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev); |
Harish Kasiviswanathan | 9a4b7d4 | 2017-06-09 11:26:57 -0400 | [diff] [blame] | 217 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
| 218 | int vm_context); |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 219 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm); |
| 220 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
| 221 | struct list_head *validated, |
| 222 | struct amdgpu_bo_list_entry *entry); |
| 223 | int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
| 224 | int (*callback)(void *p, struct amdgpu_bo *bo), |
| 225 | void *param); |
| 226 | void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev, |
| 227 | struct amdgpu_vm *vm); |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 228 | int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, |
| 229 | struct amdgpu_vm *vm, |
| 230 | uint64_t saddr, uint64_t size); |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 231 | int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring, |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 232 | struct amdgpu_sync *sync, struct dma_fence *fence, |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 233 | struct amdgpu_job *job); |
Monk Liu | 8fdf074 | 2017-06-06 17:25:13 +0800 | [diff] [blame] | 234 | int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync); |
Christian König | 7645670 | 2017-04-06 17:52:39 +0200 | [diff] [blame] | 235 | void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub, |
| 236 | unsigned vmid); |
Christian König | 32601d4 | 2017-05-10 20:06:58 +0200 | [diff] [blame] | 237 | void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev); |
Christian König | 194d216 | 2016-10-12 15:13:52 +0200 | [diff] [blame] | 238 | int amdgpu_vm_update_directories(struct amdgpu_device *adev, |
| 239 | struct amdgpu_vm *vm); |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 240 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 241 | struct amdgpu_vm *vm, |
| 242 | struct dma_fence **fence); |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 243 | int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
| 244 | struct amdgpu_sync *sync); |
| 245 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, |
| 246 | struct amdgpu_bo_va *bo_va, |
| 247 | bool clear); |
| 248 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, |
| 249 | struct amdgpu_bo *bo); |
| 250 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, |
| 251 | struct amdgpu_bo *bo); |
| 252 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, |
| 253 | struct amdgpu_vm *vm, |
| 254 | struct amdgpu_bo *bo); |
| 255 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, |
| 256 | struct amdgpu_bo_va *bo_va, |
| 257 | uint64_t addr, uint64_t offset, |
Christian König | 268c300 | 2017-01-18 14:49:43 +0100 | [diff] [blame] | 258 | uint64_t size, uint64_t flags); |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 259 | int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, |
| 260 | struct amdgpu_bo_va *bo_va, |
| 261 | uint64_t addr, uint64_t offset, |
| 262 | uint64_t size, uint64_t flags); |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 263 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, |
| 264 | struct amdgpu_bo_va *bo_va, |
| 265 | uint64_t addr); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 266 | int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, |
| 267 | struct amdgpu_vm *vm, |
| 268 | uint64_t saddr, uint64_t size); |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 269 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, |
| 270 | struct amdgpu_bo_va *bo_va); |
Junwei Zhang | bab4fee | 2017-04-05 13:54:56 +0800 | [diff] [blame] | 271 | void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size); |
Chunming Zhou | cfbcacf | 2017-04-24 11:09:04 +0800 | [diff] [blame] | 272 | int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp); |
Chunming Zhou | b9bf33d | 2017-05-11 14:52:48 -0400 | [diff] [blame] | 273 | bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring, |
| 274 | struct amdgpu_job *job); |
Alex Xie | e59c020 | 2017-06-01 09:42:59 -0400 | [diff] [blame] | 275 | void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev); |
Christian König | 073440d | 2016-09-28 15:41:50 +0200 | [diff] [blame] | 276 | |
| 277 | #endif |