]>
Commit | Line | Data |
---|---|---|
d38ceaf9 AD |
1 | /* |
2 | * Copyright 2008 Advanced Micro Devices, Inc. | |
3 | * Copyright 2008 Red Hat Inc. | |
4 | * Copyright 2009 Jerome Glisse. | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a | |
7 | * copy of this software and associated documentation files (the "Software"), | |
8 | * to deal in the Software without restriction, including without limitation | |
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
10 | * and/or sell copies of the Software, and to permit persons to whom the | |
11 | * Software is furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
22 | * OTHER DEALINGS IN THE SOFTWARE. | |
23 | * | |
24 | * Authors: Dave Airlie | |
25 | * Alex Deucher | |
26 | * Jerome Glisse | |
27 | */ | |
f54d1867 | 28 | #include <linux/dma-fence-array.h> |
a9f87f64 | 29 | #include <linux/interval_tree_generic.h> |
02208441 | 30 | #include <linux/idr.h> |
d38ceaf9 AD |
31 | #include <drm/drmP.h> |
32 | #include <drm/amdgpu_drm.h> | |
33 | #include "amdgpu.h" | |
34 | #include "amdgpu_trace.h" | |
ede0dd86 | 35 | #include "amdgpu_amdkfd.h" |
c8c5e569 | 36 | #include "amdgpu_gmc.h" |
df399b06 | 37 | #include "amdgpu_xgmi.h" |
d38ceaf9 | 38 | |
7fc48e59 AG |
39 | /** |
40 | * DOC: GPUVM | |
41 | * | |
d38ceaf9 AD |
42 | * GPUVM is similar to the legacy gart on older asics, however |
43 | * rather than there being a single global gart table | |
44 | * for the entire GPU, there are multiple VM page tables active | |
45 | * at any given time. The VM page tables can contain a mix | |
46 | * vram pages and system memory pages and system memory pages | |
47 | * can be mapped as snooped (cached system pages) or unsnooped | |
48 | * (uncached system pages). | |
49 | * Each VM has an ID associated with it and there is a page table | |
50 | * associated with each VMID. When execting a command buffer, | |
51 | * the kernel tells the the ring what VMID to use for that command | |
52 | * buffer. VMIDs are allocated dynamically as commands are submitted. | |
53 | * The userspace drivers maintain their own address space and the kernel | |
54 | * sets up their pages tables accordingly when they submit their | |
55 | * command buffers and a VMID is assigned. | |
56 | * Cayman/Trinity support up to 8 active VMs at any given time; | |
57 | * SI supports 16. | |
58 | */ | |
59 | ||
a9f87f64 CK |
60 | #define START(node) ((node)->start) |
61 | #define LAST(node) ((node)->last) | |
62 | ||
63 | INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last, | |
64 | START, LAST, static, amdgpu_vm_it) | |
65 | ||
66 | #undef START | |
67 | #undef LAST | |
68 | ||
7fc48e59 AG |
69 | /** |
70 | * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback | |
71 | */ | |
284710fa | 72 | struct amdgpu_prt_cb { |
7fc48e59 AG |
73 | |
74 | /** | |
75 | * @adev: amdgpu device | |
76 | */ | |
284710fa | 77 | struct amdgpu_device *adev; |
7fc48e59 AG |
78 | |
79 | /** | |
80 | * @cb: callback | |
81 | */ | |
284710fa CK |
82 | struct dma_fence_cb cb; |
83 | }; | |
84 | ||
50783147 CK |
85 | /** |
86 | * amdgpu_vm_level_shift - return the addr shift for each level | |
87 | * | |
88 | * @adev: amdgpu_device pointer | |
7fc48e59 | 89 | * @level: VMPT level |
50783147 | 90 | * |
7fc48e59 AG |
91 | * Returns: |
92 | * The number of bits the pfn needs to be right shifted for a level. | |
50783147 CK |
93 | */ |
94 | static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev, | |
95 | unsigned level) | |
96 | { | |
196f7489 CZ |
97 | unsigned shift = 0xff; |
98 | ||
99 | switch (level) { | |
100 | case AMDGPU_VM_PDB2: | |
101 | case AMDGPU_VM_PDB1: | |
102 | case AMDGPU_VM_PDB0: | |
103 | shift = 9 * (AMDGPU_VM_PDB0 - level) + | |
50783147 | 104 | adev->vm_manager.block_size; |
196f7489 CZ |
105 | break; |
106 | case AMDGPU_VM_PTB: | |
107 | shift = 0; | |
108 | break; | |
109 | default: | |
110 | dev_err(adev->dev, "the level%d isn't supported.\n", level); | |
111 | } | |
112 | ||
113 | return shift; | |
50783147 CK |
114 | } |
115 | ||
d38ceaf9 | 116 | /** |
72a7ec5c | 117 | * amdgpu_vm_num_entries - return the number of entries in a PD/PT |
d38ceaf9 AD |
118 | * |
119 | * @adev: amdgpu_device pointer | |
7fc48e59 | 120 | * @level: VMPT level |
d38ceaf9 | 121 | * |
7fc48e59 AG |
122 | * Returns: |
123 | * The number of entries in a page directory or page table. | |
d38ceaf9 | 124 | */ |
72a7ec5c CK |
125 | static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev, |
126 | unsigned level) | |
d38ceaf9 | 127 | { |
196f7489 CZ |
128 | unsigned shift = amdgpu_vm_level_shift(adev, |
129 | adev->vm_manager.root_level); | |
0410c5e5 | 130 | |
196f7489 | 131 | if (level == adev->vm_manager.root_level) |
72a7ec5c | 132 | /* For the root directory */ |
9ce2b991 | 133 | return round_up(adev->vm_manager.max_pfn, 1ULL << shift) >> shift; |
196f7489 | 134 | else if (level != AMDGPU_VM_PTB) |
0410c5e5 CK |
135 | /* Everything in between */ |
136 | return 512; | |
137 | else | |
72a7ec5c | 138 | /* For the page tables on the leaves */ |
36b32a68 | 139 | return AMDGPU_VM_PTE_COUNT(adev); |
d38ceaf9 AD |
140 | } |
141 | ||
780637cb CK |
142 | /** |
143 | * amdgpu_vm_num_ats_entries - return the number of ATS entries in the root PD | |
144 | * | |
145 | * @adev: amdgpu_device pointer | |
146 | * | |
147 | * Returns: | |
148 | * The number of entries in the root page directory which needs the ATS setting. | |
149 | */ | |
150 | static unsigned amdgpu_vm_num_ats_entries(struct amdgpu_device *adev) | |
151 | { | |
152 | unsigned shift; | |
153 | ||
154 | shift = amdgpu_vm_level_shift(adev, adev->vm_manager.root_level); | |
155 | return AMDGPU_GMC_HOLE_START >> (shift + AMDGPU_GPU_PAGE_SHIFT); | |
156 | } | |
157 | ||
cb90b97b CK |
158 | /** |
159 | * amdgpu_vm_entries_mask - the mask to get the entry number of a PD/PT | |
160 | * | |
161 | * @adev: amdgpu_device pointer | |
162 | * @level: VMPT level | |
163 | * | |
164 | * Returns: | |
165 | * The mask to extract the entry number of a PD/PT from an address. | |
166 | */ | |
167 | static uint32_t amdgpu_vm_entries_mask(struct amdgpu_device *adev, | |
168 | unsigned int level) | |
169 | { | |
170 | if (level <= adev->vm_manager.root_level) | |
171 | return 0xffffffff; | |
172 | else if (level != AMDGPU_VM_PTB) | |
173 | return 0x1ff; | |
174 | else | |
175 | return AMDGPU_VM_PTE_COUNT(adev) - 1; | |
176 | } | |
177 | ||
d38ceaf9 | 178 | /** |
72a7ec5c | 179 | * amdgpu_vm_bo_size - returns the size of the BOs in bytes |
d38ceaf9 AD |
180 | * |
181 | * @adev: amdgpu_device pointer | |
7fc48e59 | 182 | * @level: VMPT level |
d38ceaf9 | 183 | * |
7fc48e59 AG |
184 | * Returns: |
185 | * The size of the BO for a page directory or page table in bytes. | |
d38ceaf9 | 186 | */ |
72a7ec5c | 187 | static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level) |
d38ceaf9 | 188 | { |
72a7ec5c | 189 | return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8); |
d38ceaf9 AD |
190 | } |
191 | ||
bcdc9fd6 CK |
192 | /** |
193 | * amdgpu_vm_bo_evicted - vm_bo is evicted | |
194 | * | |
195 | * @vm_bo: vm_bo which is evicted | |
196 | * | |
197 | * State for PDs/PTs and per VM BOs which are not at the location they should | |
198 | * be. | |
199 | */ | |
200 | static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo) | |
201 | { | |
202 | struct amdgpu_vm *vm = vm_bo->vm; | |
203 | struct amdgpu_bo *bo = vm_bo->bo; | |
204 | ||
205 | vm_bo->moved = true; | |
206 | if (bo->tbo.type == ttm_bo_type_kernel) | |
207 | list_move(&vm_bo->vm_status, &vm->evicted); | |
208 | else | |
209 | list_move_tail(&vm_bo->vm_status, &vm->evicted); | |
210 | } | |
211 | ||
212 | /** | |
213 | * amdgpu_vm_bo_relocated - vm_bo is reloacted | |
214 | * | |
215 | * @vm_bo: vm_bo which is relocated | |
216 | * | |
217 | * State for PDs/PTs which needs to update their parent PD. | |
218 | */ | |
219 | static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo) | |
220 | { | |
221 | list_move(&vm_bo->vm_status, &vm_bo->vm->relocated); | |
222 | } | |
223 | ||
224 | /** | |
225 | * amdgpu_vm_bo_moved - vm_bo is moved | |
226 | * | |
227 | * @vm_bo: vm_bo which is moved | |
228 | * | |
229 | * State for per VM BOs which are moved, but that change is not yet reflected | |
230 | * in the page tables. | |
231 | */ | |
232 | static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo) | |
233 | { | |
234 | list_move(&vm_bo->vm_status, &vm_bo->vm->moved); | |
235 | } | |
236 | ||
237 | /** | |
238 | * amdgpu_vm_bo_idle - vm_bo is idle | |
239 | * | |
240 | * @vm_bo: vm_bo which is now idle | |
241 | * | |
242 | * State for PDs/PTs and per VM BOs which have gone through the state machine | |
243 | * and are now idle. | |
244 | */ | |
245 | static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo) | |
246 | { | |
247 | list_move(&vm_bo->vm_status, &vm_bo->vm->idle); | |
248 | vm_bo->moved = false; | |
249 | } | |
250 | ||
251 | /** | |
252 | * amdgpu_vm_bo_invalidated - vm_bo is invalidated | |
253 | * | |
254 | * @vm_bo: vm_bo which is now invalidated | |
255 | * | |
256 | * State for normal BOs which are invalidated and that change not yet reflected | |
257 | * in the PTs. | |
258 | */ | |
259 | static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo) | |
260 | { | |
261 | spin_lock(&vm_bo->vm->invalidated_lock); | |
262 | list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated); | |
263 | spin_unlock(&vm_bo->vm->invalidated_lock); | |
264 | } | |
265 | ||
266 | /** | |
267 | * amdgpu_vm_bo_done - vm_bo is done | |
268 | * | |
269 | * @vm_bo: vm_bo which is now done | |
270 | * | |
271 | * State for normal BOs which are invalidated and that change has been updated | |
272 | * in the PTs. | |
273 | */ | |
274 | static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo) | |
275 | { | |
276 | spin_lock(&vm_bo->vm->invalidated_lock); | |
277 | list_del_init(&vm_bo->vm_status); | |
278 | spin_unlock(&vm_bo->vm->invalidated_lock); | |
279 | } | |
280 | ||
c460f8a6 CK |
281 | /** |
282 | * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm | |
283 | * | |
284 | * @base: base structure for tracking BO usage in a VM | |
285 | * @vm: vm to which bo is to be added | |
286 | * @bo: amdgpu buffer object | |
287 | * | |
288 | * Initialize a bo_va_base structure and add it to the appropriate lists | |
289 | * | |
290 | */ | |
291 | static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base, | |
292 | struct amdgpu_vm *vm, | |
293 | struct amdgpu_bo *bo) | |
294 | { | |
295 | base->vm = vm; | |
296 | base->bo = bo; | |
646b9025 | 297 | base->next = NULL; |
c460f8a6 CK |
298 | INIT_LIST_HEAD(&base->vm_status); |
299 | ||
300 | if (!bo) | |
301 | return; | |
646b9025 CK |
302 | base->next = bo->vm_bo; |
303 | bo->vm_bo = base; | |
c460f8a6 CK |
304 | |
305 | if (bo->tbo.resv != vm->root.base.bo->tbo.resv) | |
306 | return; | |
307 | ||
308 | vm->bulk_moveable = false; | |
fda43ab6 | 309 | if (bo->tbo.type == ttm_bo_type_kernel && bo->parent) |
bcdc9fd6 | 310 | amdgpu_vm_bo_relocated(base); |
c460f8a6 | 311 | else |
bcdc9fd6 | 312 | amdgpu_vm_bo_idle(base); |
c460f8a6 CK |
313 | |
314 | if (bo->preferred_domains & | |
315 | amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)) | |
316 | return; | |
317 | ||
318 | /* | |
319 | * we checked all the prerequisites, but it looks like this per vm bo | |
320 | * is currently evicted. add the bo to the evicted list to make sure it | |
321 | * is validated on next vm use to avoid fault. | |
322 | * */ | |
bcdc9fd6 | 323 | amdgpu_vm_bo_evicted(base); |
c460f8a6 CK |
324 | } |
325 | ||
ba79fde4 CK |
326 | /** |
327 | * amdgpu_vm_pt_parent - get the parent page directory | |
328 | * | |
329 | * @pt: child page table | |
330 | * | |
331 | * Helper to get the parent entry for the child page table. NULL if we are at | |
332 | * the root page directory. | |
333 | */ | |
334 | static struct amdgpu_vm_pt *amdgpu_vm_pt_parent(struct amdgpu_vm_pt *pt) | |
335 | { | |
336 | struct amdgpu_bo *parent = pt->base.bo->parent; | |
337 | ||
338 | if (!parent) | |
339 | return NULL; | |
340 | ||
646b9025 | 341 | return container_of(parent->vm_bo, struct amdgpu_vm_pt, base); |
ba79fde4 CK |
342 | } |
343 | ||
73633e32 CK |
344 | /** |
345 | * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt | |
346 | */ | |
347 | struct amdgpu_vm_pt_cursor { | |
348 | uint64_t pfn; | |
349 | struct amdgpu_vm_pt *parent; | |
350 | struct amdgpu_vm_pt *entry; | |
351 | unsigned level; | |
352 | }; | |
353 | ||
354 | /** | |
355 | * amdgpu_vm_pt_start - start PD/PT walk | |
356 | * | |
357 | * @adev: amdgpu_device pointer | |
358 | * @vm: amdgpu_vm structure | |
359 | * @start: start address of the walk | |
360 | * @cursor: state to initialize | |
361 | * | |
362 | * Initialize a amdgpu_vm_pt_cursor to start a walk. | |
363 | */ | |
364 | static void amdgpu_vm_pt_start(struct amdgpu_device *adev, | |
365 | struct amdgpu_vm *vm, uint64_t start, | |
366 | struct amdgpu_vm_pt_cursor *cursor) | |
367 | { | |
368 | cursor->pfn = start; | |
369 | cursor->parent = NULL; | |
370 | cursor->entry = &vm->root; | |
371 | cursor->level = adev->vm_manager.root_level; | |
372 | } | |
373 | ||
374 | /** | |
375 | * amdgpu_vm_pt_descendant - go to child node | |
376 | * | |
377 | * @adev: amdgpu_device pointer | |
378 | * @cursor: current state | |
379 | * | |
380 | * Walk to the child node of the current node. | |
381 | * Returns: | |
382 | * True if the walk was possible, false otherwise. | |
383 | */ | |
384 | static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev, | |
385 | struct amdgpu_vm_pt_cursor *cursor) | |
386 | { | |
cb90b97b | 387 | unsigned mask, shift, idx; |
73633e32 CK |
388 | |
389 | if (!cursor->entry->entries) | |
390 | return false; | |
391 | ||
392 | BUG_ON(!cursor->entry->base.bo); | |
cb90b97b | 393 | mask = amdgpu_vm_entries_mask(adev, cursor->level); |
73633e32 CK |
394 | shift = amdgpu_vm_level_shift(adev, cursor->level); |
395 | ||
396 | ++cursor->level; | |
cb90b97b | 397 | idx = (cursor->pfn >> shift) & mask; |
73633e32 CK |
398 | cursor->parent = cursor->entry; |
399 | cursor->entry = &cursor->entry->entries[idx]; | |
400 | return true; | |
401 | } | |
402 | ||
403 | /** | |
404 | * amdgpu_vm_pt_sibling - go to sibling node | |
405 | * | |
406 | * @adev: amdgpu_device pointer | |
407 | * @cursor: current state | |
408 | * | |
409 | * Walk to the sibling node of the current node. | |
410 | * Returns: | |
411 | * True if the walk was possible, false otherwise. | |
412 | */ | |
413 | static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev, | |
414 | struct amdgpu_vm_pt_cursor *cursor) | |
415 | { | |
416 | unsigned shift, num_entries; | |
417 | ||
418 | /* Root doesn't have a sibling */ | |
419 | if (!cursor->parent) | |
420 | return false; | |
421 | ||
422 | /* Go to our parents and see if we got a sibling */ | |
423 | shift = amdgpu_vm_level_shift(adev, cursor->level - 1); | |
424 | num_entries = amdgpu_vm_num_entries(adev, cursor->level - 1); | |
425 | ||
426 | if (cursor->entry == &cursor->parent->entries[num_entries - 1]) | |
427 | return false; | |
428 | ||
429 | cursor->pfn += 1ULL << shift; | |
430 | cursor->pfn &= ~((1ULL << shift) - 1); | |
431 | ++cursor->entry; | |
432 | return true; | |
433 | } | |
434 | ||
435 | /** | |
436 | * amdgpu_vm_pt_ancestor - go to parent node | |
437 | * | |
438 | * @cursor: current state | |
439 | * | |
440 | * Walk to the parent node of the current node. | |
441 | * Returns: | |
442 | * True if the walk was possible, false otherwise. | |
443 | */ | |
444 | static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor) | |
445 | { | |
446 | if (!cursor->parent) | |
447 | return false; | |
448 | ||
449 | --cursor->level; | |
450 | cursor->entry = cursor->parent; | |
451 | cursor->parent = amdgpu_vm_pt_parent(cursor->parent); | |
452 | return true; | |
453 | } | |
454 | ||
455 | /** | |
456 | * amdgpu_vm_pt_next - get next PD/PT in hieratchy | |
457 | * | |
458 | * @adev: amdgpu_device pointer | |
459 | * @cursor: current state | |
460 | * | |
461 | * Walk the PD/PT tree to the next node. | |
462 | */ | |
463 | static void amdgpu_vm_pt_next(struct amdgpu_device *adev, | |
464 | struct amdgpu_vm_pt_cursor *cursor) | |
465 | { | |
466 | /* First try a newborn child */ | |
467 | if (amdgpu_vm_pt_descendant(adev, cursor)) | |
468 | return; | |
469 | ||
470 | /* If that didn't worked try to find a sibling */ | |
471 | while (!amdgpu_vm_pt_sibling(adev, cursor)) { | |
472 | /* No sibling, go to our parents and grandparents */ | |
473 | if (!amdgpu_vm_pt_ancestor(cursor)) { | |
474 | cursor->pfn = ~0ll; | |
475 | return; | |
476 | } | |
477 | } | |
478 | } | |
479 | ||
480 | /** | |
73633e32 | 481 | * amdgpu_vm_pt_first_dfs - start a deep first search |
73633e32 | 482 | * |
73633e32 | 483 | * @adev: amdgpu_device structure |
73633e32 | 484 | * @vm: amdgpu_vm structure |
73633e32 CK |
485 | * @cursor: state to initialize |
486 | * | |
73633e32 | 487 | * Starts a deep first traversal of the PD/PT tree. |
73633e32 | 488 | */ |
73633e32 CK |
489 | static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev, |
490 | struct amdgpu_vm *vm, | |
e35fb064 | 491 | struct amdgpu_vm_pt_cursor *start, |
73633e32 CK |
492 | struct amdgpu_vm_pt_cursor *cursor) |
493 | { | |
e35fb064 CK |
494 | if (start) |
495 | *cursor = *start; | |
496 | else | |
497 | amdgpu_vm_pt_start(adev, vm, 0, cursor); | |
73633e32 | 498 | while (amdgpu_vm_pt_descendant(adev, cursor)); |
73633e32 CK |
499 | } |
500 | ||
501 | /** | |
e35fb064 | 502 | * amdgpu_vm_pt_continue_dfs - check if the deep first search should continue |
73633e32 | 503 | * |
e35fb064 CK |
504 | * @start: starting point for the search |
505 | * @entry: current entry | |
73633e32 | 506 | * |
e35fb064 CK |
507 | * Returns: |
508 | * True when the search should continue, false otherwise. | |
73633e32 | 509 | */ |
e35fb064 CK |
510 | static bool amdgpu_vm_pt_continue_dfs(struct amdgpu_vm_pt_cursor *start, |
511 | struct amdgpu_vm_pt *entry) | |
73633e32 | 512 | { |
e35fb064 | 513 | return entry && (!start || entry != start->entry); |
73633e32 CK |
514 | } |
515 | ||
516 | /** | |
517 | * amdgpu_vm_pt_next_dfs - get the next node for a deep first search | |
518 | * | |
519 | * @adev: amdgpu_device structure | |
520 | * @cursor: current state | |
521 | * | |
522 | * Move the cursor to the next node in a deep first search. | |
523 | */ | |
524 | static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev, | |
525 | struct amdgpu_vm_pt_cursor *cursor) | |
526 | { | |
527 | if (!cursor->entry) | |
528 | return; | |
529 | ||
530 | if (!cursor->parent) | |
531 | cursor->entry = NULL; | |
532 | else if (amdgpu_vm_pt_sibling(adev, cursor)) | |
533 | while (amdgpu_vm_pt_descendant(adev, cursor)); | |
534 | else | |
535 | amdgpu_vm_pt_ancestor(cursor); | |
536 | } | |
537 | ||
538 | /** | |
539 | * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs | |
540 | */ | |
e35fb064 CK |
541 | #define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) \ |
542 | for (amdgpu_vm_pt_first_dfs((adev), (vm), (start), &(cursor)), \ | |
73633e32 | 543 | (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\ |
e35fb064 CK |
544 | amdgpu_vm_pt_continue_dfs((start), (entry)); \ |
545 | (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor))) | |
73633e32 | 546 | |
d38ceaf9 | 547 | /** |
56467ebf | 548 | * amdgpu_vm_get_pd_bo - add the VM PD to a validation list |
d38ceaf9 AD |
549 | * |
550 | * @vm: vm providing the BOs | |
3c0eea6c | 551 | * @validated: head of validation list |
56467ebf | 552 | * @entry: entry to add |
d38ceaf9 AD |
553 | * |
554 | * Add the page directory to the list of BOs to | |
56467ebf | 555 | * validate for command submission. |
d38ceaf9 | 556 | */ |
56467ebf CK |
557 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
558 | struct list_head *validated, | |
559 | struct amdgpu_bo_list_entry *entry) | |
d38ceaf9 | 560 | { |
56467ebf | 561 | entry->priority = 0; |
e83dfe4d | 562 | entry->tv.bo = &vm->root.base.bo->tbo; |
07daa8a0 CK |
563 | /* One for the VM updates, one for TTM and one for the CS job */ |
564 | entry->tv.num_shared = 3; | |
2f568dbd | 565 | entry->user_pages = NULL; |
56467ebf CK |
566 | list_add(&entry->tv.head, validated); |
567 | } | |
d38ceaf9 | 568 | |
b61857b5 CZ |
569 | void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo) |
570 | { | |
571 | struct amdgpu_bo *abo; | |
572 | struct amdgpu_vm_bo_base *bo_base; | |
573 | ||
574 | if (!amdgpu_bo_is_amdgpu_bo(bo)) | |
575 | return; | |
576 | ||
577 | if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) | |
578 | return; | |
579 | ||
580 | abo = ttm_to_amdgpu_bo(bo); | |
581 | if (!abo->parent) | |
582 | return; | |
583 | for (bo_base = abo->vm_bo; bo_base; bo_base = bo_base->next) { | |
584 | struct amdgpu_vm *vm = bo_base->vm; | |
585 | ||
586 | if (abo->tbo.resv == vm->root.base.bo->tbo.resv) | |
587 | vm->bulk_moveable = false; | |
588 | } | |
589 | ||
590 | } | |
f921661b HR |
591 | /** |
592 | * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU | |
593 | * | |
594 | * @adev: amdgpu device pointer | |
595 | * @vm: vm providing the BOs | |
596 | * | |
597 | * Move all BOs to the end of LRU and remember their positions to put them | |
598 | * together. | |
599 | */ | |
600 | void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev, | |
601 | struct amdgpu_vm *vm) | |
602 | { | |
603 | struct ttm_bo_global *glob = adev->mman.bdev.glob; | |
604 | struct amdgpu_vm_bo_base *bo_base; | |
605 | ||
a213c2c7 | 606 | #if 0 |
f921661b HR |
607 | if (vm->bulk_moveable) { |
608 | spin_lock(&glob->lru_lock); | |
609 | ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move); | |
610 | spin_unlock(&glob->lru_lock); | |
611 | return; | |
612 | } | |
a213c2c7 | 613 | #endif |
f921661b HR |
614 | |
615 | memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move)); | |
616 | ||
617 | spin_lock(&glob->lru_lock); | |
618 | list_for_each_entry(bo_base, &vm->idle, vm_status) { | |
619 | struct amdgpu_bo *bo = bo_base->bo; | |
620 | ||
621 | if (!bo->parent) | |
622 | continue; | |
623 | ||
624 | ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move); | |
625 | if (bo->shadow) | |
626 | ttm_bo_move_to_lru_tail(&bo->shadow->tbo, | |
627 | &vm->lru_bulk_move); | |
628 | } | |
629 | spin_unlock(&glob->lru_lock); | |
630 | ||
631 | vm->bulk_moveable = true; | |
632 | } | |
633 | ||
670fecc8 | 634 | /** |
f7da30d9 | 635 | * amdgpu_vm_validate_pt_bos - validate the page table BOs |
670fecc8 | 636 | * |
5a712a87 | 637 | * @adev: amdgpu device pointer |
56467ebf | 638 | * @vm: vm providing the BOs |
670fecc8 CK |
639 | * @validate: callback to do the validation |
640 | * @param: parameter for the validation callback | |
641 | * | |
642 | * Validate the page table BOs on command submission if neccessary. | |
7fc48e59 AG |
643 | * |
644 | * Returns: | |
645 | * Validation result. | |
670fecc8 | 646 | */ |
f7da30d9 CK |
647 | int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
648 | int (*validate)(void *p, struct amdgpu_bo *bo), | |
649 | void *param) | |
670fecc8 | 650 | { |
91ccdd24 CK |
651 | struct amdgpu_vm_bo_base *bo_base, *tmp; |
652 | int r = 0; | |
670fecc8 | 653 | |
39bbd331 CK |
654 | vm->bulk_moveable &= list_empty(&vm->evicted); |
655 | ||
91ccdd24 CK |
656 | list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) { |
657 | struct amdgpu_bo *bo = bo_base->bo; | |
670fecc8 | 658 | |
262b9c39 CK |
659 | r = validate(param, bo); |
660 | if (r) | |
661 | break; | |
670fecc8 | 662 | |
af4c0f65 | 663 | if (bo->tbo.type != ttm_bo_type_kernel) { |
bcdc9fd6 | 664 | amdgpu_vm_bo_moved(bo_base); |
af4c0f65 | 665 | } else { |
ecf96b52 | 666 | vm->update_funcs->map_table(bo); |
fda43ab6 CK |
667 | if (bo->parent) |
668 | amdgpu_vm_bo_relocated(bo_base); | |
17cc5252 | 669 | else |
fda43ab6 | 670 | amdgpu_vm_bo_idle(bo_base); |
af4c0f65 | 671 | } |
670fecc8 CK |
672 | } |
673 | ||
91ccdd24 | 674 | return r; |
670fecc8 CK |
675 | } |
676 | ||
56467ebf | 677 | /** |
34d7be5d | 678 | * amdgpu_vm_ready - check VM is ready for updates |
56467ebf | 679 | * |
34d7be5d | 680 | * @vm: VM to check |
d38ceaf9 | 681 | * |
34d7be5d | 682 | * Check if all VM PDs/PTs are ready for updates |
7fc48e59 AG |
683 | * |
684 | * Returns: | |
685 | * True if eviction list is empty. | |
d38ceaf9 | 686 | */ |
3f3333f8 | 687 | bool amdgpu_vm_ready(struct amdgpu_vm *vm) |
d38ceaf9 | 688 | { |
af4c0f65 | 689 | return list_empty(&vm->evicted); |
d711e139 CK |
690 | } |
691 | ||
13307f7e CK |
692 | /** |
693 | * amdgpu_vm_clear_bo - initially clear the PDs/PTs | |
694 | * | |
695 | * @adev: amdgpu_device pointer | |
7fc48e59 | 696 | * @vm: VM to clear BO from |
13307f7e | 697 | * @bo: BO to clear |
13307f7e CK |
698 | * |
699 | * Root PD needs to be reserved when calling this. | |
7fc48e59 AG |
700 | * |
701 | * Returns: | |
702 | * 0 on success, errno otherwise. | |
13307f7e CK |
703 | */ |
704 | static int amdgpu_vm_clear_bo(struct amdgpu_device *adev, | |
780637cb CK |
705 | struct amdgpu_vm *vm, |
706 | struct amdgpu_bo *bo) | |
13307f7e CK |
707 | { |
708 | struct ttm_operation_ctx ctx = { true, false }; | |
780637cb | 709 | unsigned level = adev->vm_manager.root_level; |
adc7e863 | 710 | struct amdgpu_vm_update_params params; |
780637cb | 711 | struct amdgpu_bo *ancestor = bo; |
4584312d | 712 | unsigned entries, ats_entries; |
4584312d | 713 | uint64_t addr; |
13307f7e CK |
714 | int r; |
715 | ||
780637cb CK |
716 | /* Figure out our place in the hierarchy */ |
717 | if (ancestor->parent) { | |
718 | ++level; | |
719 | while (ancestor->parent->parent) { | |
720 | ++level; | |
721 | ancestor = ancestor->parent; | |
722 | } | |
723 | } | |
724 | ||
4584312d | 725 | entries = amdgpu_bo_size(bo) / 8; |
780637cb CK |
726 | if (!vm->pte_support_ats) { |
727 | ats_entries = 0; | |
4584312d | 728 | |
780637cb CK |
729 | } else if (!bo->parent) { |
730 | ats_entries = amdgpu_vm_num_ats_entries(adev); | |
731 | ats_entries = min(ats_entries, entries); | |
732 | entries -= ats_entries; | |
4584312d | 733 | |
780637cb CK |
734 | } else { |
735 | struct amdgpu_vm_pt *pt; | |
736 | ||
737 | pt = container_of(ancestor->vm_bo, struct amdgpu_vm_pt, base); | |
738 | ats_entries = amdgpu_vm_num_ats_entries(adev); | |
739 | if ((pt - vm->root.entries) >= ats_entries) { | |
740 | ats_entries = 0; | |
4584312d CK |
741 | } else { |
742 | ats_entries = entries; | |
743 | entries = 0; | |
744 | } | |
13307f7e CK |
745 | } |
746 | ||
13307f7e CK |
747 | r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); |
748 | if (r) | |
83cd8397 | 749 | return r; |
13307f7e | 750 | |
83cd8397 CK |
751 | if (bo->shadow) { |
752 | r = ttm_bo_validate(&bo->shadow->tbo, &bo->shadow->placement, | |
753 | &ctx); | |
754 | if (r) | |
755 | return r; | |
83cd8397 CK |
756 | } |
757 | ||
ecf96b52 | 758 | r = vm->update_funcs->map_table(bo); |
284dec43 CK |
759 | if (r) |
760 | return r; | |
761 | ||
adc7e863 CK |
762 | memset(¶ms, 0, sizeof(params)); |
763 | params.adev = adev; | |
764 | params.vm = vm; | |
765 | ||
766 | r = vm->update_funcs->prepare(¶ms, AMDGPU_FENCE_OWNER_KFD, NULL); | |
13307f7e | 767 | if (r) |
83cd8397 | 768 | return r; |
13307f7e | 769 | |
adc7e863 | 770 | addr = 0; |
4584312d | 771 | if (ats_entries) { |
5fa76a9d | 772 | uint64_t value = 0, flags; |
4584312d | 773 | |
5fa76a9d CK |
774 | flags = AMDGPU_PTE_DEFAULT_ATC; |
775 | if (level != AMDGPU_VM_PTB) { | |
776 | /* Handle leaf PDEs as PTEs */ | |
777 | flags |= AMDGPU_PDE_PTE; | |
778 | amdgpu_gmc_get_vm_pde(adev, level, &value, &flags); | |
779 | } | |
4584312d | 780 | |
adc7e863 | 781 | r = vm->update_funcs->update(¶ms, bo, addr, 0, ats_entries, |
5fa76a9d | 782 | value, flags); |
adc7e863 CK |
783 | if (r) |
784 | return r; | |
13307f7e | 785 | |
4584312d CK |
786 | addr += ats_entries * 8; |
787 | } | |
788 | ||
e95b93ce CK |
789 | if (entries) { |
790 | uint64_t value = 0; | |
791 | ||
792 | /* Workaround for fault priority problem on GMC9 */ | |
adc7e863 CK |
793 | if (level == AMDGPU_VM_PTB && |
794 | adev->asic_type >= CHIP_VEGA10) | |
e95b93ce CK |
795 | value = AMDGPU_PTE_EXECUTABLE; |
796 | ||
adc7e863 CK |
797 | r = vm->update_funcs->update(¶ms, bo, addr, 0, entries, |
798 | 0, value); | |
799 | if (r) | |
800 | return r; | |
e95b93ce | 801 | } |
4584312d | 802 | |
adc7e863 | 803 | return vm->update_funcs->commit(¶ms, NULL); |
13307f7e CK |
804 | } |
805 | ||
e21eb261 CK |
806 | /** |
807 | * amdgpu_vm_bo_param - fill in parameters for PD/PT allocation | |
808 | * | |
809 | * @adev: amdgpu_device pointer | |
810 | * @vm: requesting vm | |
811 | * @bp: resulting BO allocation parameters | |
812 | */ | |
813 | static void amdgpu_vm_bo_param(struct amdgpu_device *adev, struct amdgpu_vm *vm, | |
814 | int level, struct amdgpu_bo_param *bp) | |
815 | { | |
816 | memset(bp, 0, sizeof(*bp)); | |
817 | ||
818 | bp->size = amdgpu_vm_bo_size(adev, level); | |
819 | bp->byte_align = AMDGPU_GPU_PAGE_SIZE; | |
820 | bp->domain = AMDGPU_GEM_DOMAIN_VRAM; | |
284dec43 CK |
821 | bp->domain = amdgpu_bo_get_preferred_pin_domain(adev, bp->domain); |
822 | bp->flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | | |
823 | AMDGPU_GEM_CREATE_CPU_GTT_USWC; | |
e21eb261 CK |
824 | if (vm->use_cpu_for_update) |
825 | bp->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; | |
03e9dee1 FK |
826 | else if (!vm->root.base.bo || vm->root.base.bo->shadow) |
827 | bp->flags |= AMDGPU_GEM_CREATE_SHADOW; | |
e21eb261 CK |
828 | bp->type = ttm_bo_type_kernel; |
829 | if (vm->root.base.bo) | |
830 | bp->resv = vm->root.base.bo->tbo.resv; | |
831 | } | |
832 | ||
663e4577 | 833 | /** |
98ae7f98 | 834 | * amdgpu_vm_alloc_pts - Allocate a specific page table |
663e4577 CK |
835 | * |
836 | * @adev: amdgpu_device pointer | |
837 | * @vm: VM to allocate page tables for | |
98ae7f98 | 838 | * @cursor: Which page table to allocate |
663e4577 | 839 | * |
98ae7f98 | 840 | * Make sure a specific page table or directory is allocated. |
7fc48e59 AG |
841 | * |
842 | * Returns: | |
98ae7f98 FK |
843 | * 1 if page table needed to be allocated, 0 if page table was already |
844 | * allocated, negative errno if an error occurred. | |
663e4577 | 845 | */ |
0ce15d6f CK |
846 | static int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, |
847 | struct amdgpu_vm *vm, | |
848 | struct amdgpu_vm_pt_cursor *cursor) | |
663e4577 | 849 | { |
0ce15d6f CK |
850 | struct amdgpu_vm_pt *entry = cursor->entry; |
851 | struct amdgpu_bo_param bp; | |
d72a6887 | 852 | struct amdgpu_bo *pt; |
d72a6887 | 853 | int r; |
663e4577 | 854 | |
0ce15d6f CK |
855 | if (cursor->level < AMDGPU_VM_PTB && !entry->entries) { |
856 | unsigned num_entries; | |
663e4577 | 857 | |
0ce15d6f CK |
858 | num_entries = amdgpu_vm_num_entries(adev, cursor->level); |
859 | entry->entries = kvmalloc_array(num_entries, | |
860 | sizeof(*entry->entries), | |
861 | GFP_KERNEL | __GFP_ZERO); | |
862 | if (!entry->entries) | |
863 | return -ENOMEM; | |
4584312d CK |
864 | } |
865 | ||
0ce15d6f CK |
866 | if (entry->base.bo) |
867 | return 0; | |
d72a6887 | 868 | |
0ce15d6f | 869 | amdgpu_vm_bo_param(adev, vm, cursor->level, &bp); |
d72a6887 | 870 | |
0ce15d6f CK |
871 | r = amdgpu_bo_create(adev, &bp, &pt); |
872 | if (r) | |
873 | return r; | |
d72a6887 | 874 | |
0ce15d6f CK |
875 | /* Keep a reference to the root directory to avoid |
876 | * freeing them up in the wrong order. | |
877 | */ | |
878 | pt->parent = amdgpu_bo_ref(cursor->parent->base.bo); | |
879 | amdgpu_vm_bo_base_init(&entry->base, vm, pt); | |
1e293037 | 880 | |
0ce15d6f CK |
881 | r = amdgpu_vm_clear_bo(adev, vm, pt); |
882 | if (r) | |
883 | goto error_free_pt; | |
d72a6887 CK |
884 | |
885 | return 0; | |
886 | ||
887 | error_free_pt: | |
888 | amdgpu_bo_unref(&pt->shadow); | |
889 | amdgpu_bo_unref(&pt); | |
890 | return r; | |
663e4577 CK |
891 | } |
892 | ||
e35fb064 CK |
893 | /** |
894 | * amdgpu_vm_free_table - fre one PD/PT | |
895 | * | |
896 | * @entry: PDE to free | |
897 | */ | |
898 | static void amdgpu_vm_free_table(struct amdgpu_vm_pt *entry) | |
899 | { | |
900 | if (entry->base.bo) { | |
901 | entry->base.bo->vm_bo = NULL; | |
902 | list_del(&entry->base.vm_status); | |
903 | amdgpu_bo_unref(&entry->base.bo->shadow); | |
904 | amdgpu_bo_unref(&entry->base.bo); | |
905 | } | |
906 | kvfree(entry->entries); | |
907 | entry->entries = NULL; | |
908 | } | |
909 | ||
229a37f8 CK |
910 | /** |
911 | * amdgpu_vm_free_pts - free PD/PT levels | |
912 | * | |
913 | * @adev: amdgpu device structure | |
769f846e | 914 | * @vm: amdgpu vm structure |
e35fb064 | 915 | * @start: optional cursor where to start freeing PDs/PTs |
229a37f8 CK |
916 | * |
917 | * Free the page directory or page table level and all sub levels. | |
918 | */ | |
919 | static void amdgpu_vm_free_pts(struct amdgpu_device *adev, | |
e35fb064 CK |
920 | struct amdgpu_vm *vm, |
921 | struct amdgpu_vm_pt_cursor *start) | |
229a37f8 CK |
922 | { |
923 | struct amdgpu_vm_pt_cursor cursor; | |
924 | struct amdgpu_vm_pt *entry; | |
925 | ||
e35fb064 | 926 | vm->bulk_moveable = false; |
229a37f8 | 927 | |
e35fb064 CK |
928 | for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) |
929 | amdgpu_vm_free_table(entry); | |
229a37f8 | 930 | |
e35fb064 CK |
931 | if (start) |
932 | amdgpu_vm_free_table(start->entry); | |
229a37f8 CK |
933 | } |
934 | ||
e59c0205 AX |
935 | /** |
936 | * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug | |
937 | * | |
938 | * @adev: amdgpu_device pointer | |
939 | */ | |
940 | void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev) | |
93dcc37d | 941 | { |
a1255107 | 942 | const struct amdgpu_ip_block *ip_block; |
e59c0205 AX |
943 | bool has_compute_vm_bug; |
944 | struct amdgpu_ring *ring; | |
945 | int i; | |
93dcc37d | 946 | |
e59c0205 | 947 | has_compute_vm_bug = false; |
93dcc37d | 948 | |
2990a1fc | 949 | ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); |
e59c0205 AX |
950 | if (ip_block) { |
951 | /* Compute has a VM bug for GFX version < 7. | |
952 | Compute has a VM bug for GFX 8 MEC firmware version < 673.*/ | |
953 | if (ip_block->version->major <= 7) | |
954 | has_compute_vm_bug = true; | |
955 | else if (ip_block->version->major == 8) | |
956 | if (adev->gfx.mec_fw_version < 673) | |
957 | has_compute_vm_bug = true; | |
958 | } | |
93dcc37d | 959 | |
e59c0205 AX |
960 | for (i = 0; i < adev->num_rings; i++) { |
961 | ring = adev->rings[i]; | |
962 | if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) | |
963 | /* only compute rings */ | |
964 | ring->has_compute_vm_bug = has_compute_vm_bug; | |
93dcc37d | 965 | else |
e59c0205 | 966 | ring->has_compute_vm_bug = false; |
93dcc37d | 967 | } |
93dcc37d AD |
968 | } |
969 | ||
7fc48e59 AG |
970 | /** |
971 | * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job. | |
972 | * | |
973 | * @ring: ring on which the job will be submitted | |
974 | * @job: job to submit | |
975 | * | |
976 | * Returns: | |
977 | * True if sync is needed. | |
978 | */ | |
b9bf33d5 CZ |
979 | bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring, |
980 | struct amdgpu_job *job) | |
e60f8db5 | 981 | { |
b9bf33d5 CZ |
982 | struct amdgpu_device *adev = ring->adev; |
983 | unsigned vmhub = ring->funcs->vmhub; | |
620f774f CK |
984 | struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; |
985 | struct amdgpu_vmid *id; | |
b9bf33d5 | 986 | bool gds_switch_needed; |
e59c0205 | 987 | bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug; |
b9bf33d5 | 988 | |
c4f46f22 | 989 | if (job->vmid == 0) |
b9bf33d5 | 990 | return false; |
c4f46f22 | 991 | id = &id_mgr->ids[job->vmid]; |
b9bf33d5 CZ |
992 | gds_switch_needed = ring->funcs->emit_gds_switch && ( |
993 | id->gds_base != job->gds_base || | |
994 | id->gds_size != job->gds_size || | |
995 | id->gws_base != job->gws_base || | |
996 | id->gws_size != job->gws_size || | |
997 | id->oa_base != job->oa_base || | |
998 | id->oa_size != job->oa_size); | |
e60f8db5 | 999 | |
620f774f | 1000 | if (amdgpu_vmid_had_gpu_reset(adev, id)) |
b9bf33d5 | 1001 | return true; |
e60f8db5 | 1002 | |
bb37b67d | 1003 | return vm_flush_needed || gds_switch_needed; |
b9bf33d5 CZ |
1004 | } |
1005 | ||
d38ceaf9 AD |
1006 | /** |
1007 | * amdgpu_vm_flush - hardware flush the vm | |
1008 | * | |
1009 | * @ring: ring to use for flush | |
00553cf8 | 1010 | * @job: related job |
7fc48e59 | 1011 | * @need_pipe_sync: is pipe sync needed |
d38ceaf9 | 1012 | * |
4ff37a83 | 1013 | * Emit a VM flush when it is necessary. |
7fc48e59 AG |
1014 | * |
1015 | * Returns: | |
1016 | * 0 on success, errno otherwise. | |
d38ceaf9 | 1017 | */ |
8fdf074f | 1018 | int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync) |
d38ceaf9 | 1019 | { |
971fe9a9 | 1020 | struct amdgpu_device *adev = ring->adev; |
7645670d | 1021 | unsigned vmhub = ring->funcs->vmhub; |
620f774f | 1022 | struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; |
c4f46f22 | 1023 | struct amdgpu_vmid *id = &id_mgr->ids[job->vmid]; |
d564a06e | 1024 | bool gds_switch_needed = ring->funcs->emit_gds_switch && ( |
fd53be30 CZ |
1025 | id->gds_base != job->gds_base || |
1026 | id->gds_size != job->gds_size || | |
1027 | id->gws_base != job->gws_base || | |
1028 | id->gws_size != job->gws_size || | |
1029 | id->oa_base != job->oa_base || | |
1030 | id->oa_size != job->oa_size); | |
de37e68a | 1031 | bool vm_flush_needed = job->vm_needs_flush; |
b3cd285f CK |
1032 | bool pasid_mapping_needed = id->pasid != job->pasid || |
1033 | !id->pasid_mapping || | |
1034 | !dma_fence_is_signaled(id->pasid_mapping); | |
1035 | struct dma_fence *fence = NULL; | |
c0e51931 | 1036 | unsigned patch_offset = 0; |
41d9eb2c | 1037 | int r; |
d564a06e | 1038 | |
620f774f | 1039 | if (amdgpu_vmid_had_gpu_reset(adev, id)) { |
f7d015b9 CK |
1040 | gds_switch_needed = true; |
1041 | vm_flush_needed = true; | |
b3cd285f | 1042 | pasid_mapping_needed = true; |
f7d015b9 | 1043 | } |
971fe9a9 | 1044 | |
b3cd285f | 1045 | gds_switch_needed &= !!ring->funcs->emit_gds_switch; |
d8de8260 AG |
1046 | vm_flush_needed &= !!ring->funcs->emit_vm_flush && |
1047 | job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET; | |
b3cd285f CK |
1048 | pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping && |
1049 | ring->funcs->emit_wreg; | |
1050 | ||
8fdf074f | 1051 | if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync) |
f7d015b9 | 1052 | return 0; |
41d9eb2c | 1053 | |
c0e51931 CK |
1054 | if (ring->funcs->init_cond_exec) |
1055 | patch_offset = amdgpu_ring_init_cond_exec(ring); | |
41d9eb2c | 1056 | |
8fdf074f ML |
1057 | if (need_pipe_sync) |
1058 | amdgpu_ring_emit_pipeline_sync(ring); | |
1059 | ||
b3cd285f | 1060 | if (vm_flush_needed) { |
c4f46f22 | 1061 | trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr); |
c633c00b | 1062 | amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr); |
b3cd285f CK |
1063 | } |
1064 | ||
1065 | if (pasid_mapping_needed) | |
1066 | amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid); | |
e9d672b2 | 1067 | |
b3cd285f | 1068 | if (vm_flush_needed || pasid_mapping_needed) { |
d240cd9e | 1069 | r = amdgpu_fence_emit(ring, &fence, 0); |
c0e51931 CK |
1070 | if (r) |
1071 | return r; | |
b3cd285f | 1072 | } |
e9d672b2 | 1073 | |
b3cd285f | 1074 | if (vm_flush_needed) { |
7645670d | 1075 | mutex_lock(&id_mgr->lock); |
c0e51931 | 1076 | dma_fence_put(id->last_flush); |
b3cd285f CK |
1077 | id->last_flush = dma_fence_get(fence); |
1078 | id->current_gpu_reset_count = | |
1079 | atomic_read(&adev->gpu_reset_counter); | |
7645670d | 1080 | mutex_unlock(&id_mgr->lock); |
c0e51931 | 1081 | } |
e9d672b2 | 1082 | |
b3cd285f CK |
1083 | if (pasid_mapping_needed) { |
1084 | id->pasid = job->pasid; | |
1085 | dma_fence_put(id->pasid_mapping); | |
1086 | id->pasid_mapping = dma_fence_get(fence); | |
1087 | } | |
1088 | dma_fence_put(fence); | |
1089 | ||
7c4378f4 | 1090 | if (ring->funcs->emit_gds_switch && gds_switch_needed) { |
c0e51931 CK |
1091 | id->gds_base = job->gds_base; |
1092 | id->gds_size = job->gds_size; | |
1093 | id->gws_base = job->gws_base; | |
1094 | id->gws_size = job->gws_size; | |
1095 | id->oa_base = job->oa_base; | |
1096 | id->oa_size = job->oa_size; | |
c4f46f22 | 1097 | amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, |
c0e51931 CK |
1098 | job->gds_size, job->gws_base, |
1099 | job->gws_size, job->oa_base, | |
1100 | job->oa_size); | |
1101 | } | |
1102 | ||
1103 | if (ring->funcs->patch_cond_exec) | |
1104 | amdgpu_ring_patch_cond_exec(ring, patch_offset); | |
1105 | ||
1106 | /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */ | |
1107 | if (ring->funcs->emit_switch_buffer) { | |
1108 | amdgpu_ring_emit_switch_buffer(ring); | |
1109 | amdgpu_ring_emit_switch_buffer(ring); | |
e9d672b2 | 1110 | } |
41d9eb2c | 1111 | return 0; |
971fe9a9 CK |
1112 | } |
1113 | ||
d38ceaf9 AD |
1114 | /** |
1115 | * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo | |
1116 | * | |
1117 | * @vm: requested vm | |
1118 | * @bo: requested buffer object | |
1119 | * | |
8843dbbb | 1120 | * Find @bo inside the requested vm. |
d38ceaf9 AD |
1121 | * Search inside the @bos vm list for the requested vm |
1122 | * Returns the found bo_va or NULL if none is found | |
1123 | * | |
1124 | * Object has to be reserved! | |
7fc48e59 AG |
1125 | * |
1126 | * Returns: | |
1127 | * Found bo_va or NULL. | |
d38ceaf9 AD |
1128 | */ |
1129 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, | |
1130 | struct amdgpu_bo *bo) | |
1131 | { | |
646b9025 | 1132 | struct amdgpu_vm_bo_base *base; |
d38ceaf9 | 1133 | |
646b9025 CK |
1134 | for (base = bo->vm_bo; base; base = base->next) { |
1135 | if (base->vm != vm) | |
1136 | continue; | |
1137 | ||
1138 | return container_of(base, struct amdgpu_bo_va, base); | |
d38ceaf9 AD |
1139 | } |
1140 | return NULL; | |
1141 | } | |
1142 | ||
d38ceaf9 | 1143 | /** |
b07c9d2a | 1144 | * amdgpu_vm_map_gart - Resolve gart mapping of addr |
d38ceaf9 | 1145 | * |
b07c9d2a | 1146 | * @pages_addr: optional DMA address to use for lookup |
d38ceaf9 AD |
1147 | * @addr: the unmapped addr |
1148 | * | |
1149 | * Look up the physical address of the page that the pte resolves | |
7fc48e59 AG |
1150 | * to. |
1151 | * | |
1152 | * Returns: | |
1153 | * The pointer for the page table entry. | |
d38ceaf9 | 1154 | */ |
6dd09027 | 1155 | uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) |
d38ceaf9 AD |
1156 | { |
1157 | uint64_t result; | |
1158 | ||
de9ea7bd CK |
1159 | /* page table offset */ |
1160 | result = pages_addr[addr >> PAGE_SHIFT]; | |
b07c9d2a | 1161 | |
de9ea7bd CK |
1162 | /* in case cpu page size != gpu page size*/ |
1163 | result |= addr & (~PAGE_MASK); | |
d38ceaf9 | 1164 | |
b07c9d2a | 1165 | result &= 0xFFFFFFFFFFFFF000ULL; |
d38ceaf9 AD |
1166 | |
1167 | return result; | |
1168 | } | |
1169 | ||
f8991bab | 1170 | /* |
6989f246 | 1171 | * amdgpu_vm_update_pde - update a single level in the hierarchy |
f8991bab | 1172 | * |
6989f246 | 1173 | * @param: parameters for the update |
f8991bab | 1174 | * @vm: requested vm |
6989f246 | 1175 | * @entry: entry to update |
f8991bab | 1176 | * |
6989f246 | 1177 | * Makes sure the requested entry in parent is up to date. |
f8991bab | 1178 | */ |
e6899d55 CK |
1179 | static int amdgpu_vm_update_pde(struct amdgpu_vm_update_params *params, |
1180 | struct amdgpu_vm *vm, | |
e6899d55 | 1181 | struct amdgpu_vm_pt *entry) |
d38ceaf9 | 1182 | { |
fda43ab6 | 1183 | struct amdgpu_vm_pt *parent = amdgpu_vm_pt_parent(entry); |
373ac645 | 1184 | struct amdgpu_bo *bo = parent->base.bo, *pbo; |
3de676d8 CK |
1185 | uint64_t pde, pt, flags; |
1186 | unsigned level; | |
d5fc5e82 | 1187 | |
373ac645 | 1188 | for (level = 0, pbo = bo->parent; pbo; ++level) |
3de676d8 CK |
1189 | pbo = pbo->parent; |
1190 | ||
196f7489 | 1191 | level += params->adev->vm_manager.root_level; |
24a8d289 | 1192 | amdgpu_gmc_get_pde_for_bo(entry->base.bo, level, &pt, &flags); |
373ac645 | 1193 | pde = (entry - parent->entries) * 8; |
e6899d55 | 1194 | return vm->update_funcs->update(params, bo, pde, pt, 1, 0, flags); |
d38ceaf9 AD |
1195 | } |
1196 | ||
92456b93 | 1197 | /* |
d4085ea9 | 1198 | * amdgpu_vm_invalidate_pds - mark all PDs as invalid |
92456b93 | 1199 | * |
7fc48e59 AG |
1200 | * @adev: amdgpu_device pointer |
1201 | * @vm: related vm | |
92456b93 CK |
1202 | * |
1203 | * Mark all PD level as invalid after an error. | |
1204 | */ | |
d4085ea9 CK |
1205 | static void amdgpu_vm_invalidate_pds(struct amdgpu_device *adev, |
1206 | struct amdgpu_vm *vm) | |
92456b93 | 1207 | { |
d4085ea9 CK |
1208 | struct amdgpu_vm_pt_cursor cursor; |
1209 | struct amdgpu_vm_pt *entry; | |
92456b93 | 1210 | |
e35fb064 | 1211 | for_each_amdgpu_vm_pt_dfs_safe(adev, vm, NULL, cursor, entry) |
d4085ea9 | 1212 | if (entry->base.bo && !entry->base.moved) |
bcdc9fd6 | 1213 | amdgpu_vm_bo_relocated(&entry->base); |
92456b93 CK |
1214 | } |
1215 | ||
194d2161 CK |
1216 | /* |
1217 | * amdgpu_vm_update_directories - make sure that all directories are valid | |
1218 | * | |
1219 | * @adev: amdgpu_device pointer | |
1220 | * @vm: requested vm | |
1221 | * | |
1222 | * Makes sure all directories are up to date. | |
7fc48e59 AG |
1223 | * |
1224 | * Returns: | |
1225 | * 0 for success, error for failure. | |
194d2161 CK |
1226 | */ |
1227 | int amdgpu_vm_update_directories(struct amdgpu_device *adev, | |
1228 | struct amdgpu_vm *vm) | |
1229 | { | |
d1e29462 | 1230 | struct amdgpu_vm_update_params params; |
e6899d55 | 1231 | int r; |
92456b93 | 1232 | |
6989f246 CK |
1233 | if (list_empty(&vm->relocated)) |
1234 | return 0; | |
1235 | ||
6989f246 CK |
1236 | memset(¶ms, 0, sizeof(params)); |
1237 | params.adev = adev; | |
e6899d55 | 1238 | params.vm = vm; |
6989f246 | 1239 | |
e6899d55 CK |
1240 | r = vm->update_funcs->prepare(¶ms, AMDGPU_FENCE_OWNER_VM, NULL); |
1241 | if (r) | |
1242 | return r; | |
6989f246 | 1243 | |
ea09729c | 1244 | while (!list_empty(&vm->relocated)) { |
fda43ab6 | 1245 | struct amdgpu_vm_pt *entry; |
ea09729c | 1246 | |
ba79fde4 CK |
1247 | entry = list_first_entry(&vm->relocated, struct amdgpu_vm_pt, |
1248 | base.vm_status); | |
1249 | amdgpu_vm_bo_idle(&entry->base); | |
ea09729c | 1250 | |
fda43ab6 | 1251 | r = amdgpu_vm_update_pde(¶ms, vm, entry); |
6989f246 CK |
1252 | if (r) |
1253 | goto error; | |
68c62306 CK |
1254 | } |
1255 | ||
e6899d55 CK |
1256 | r = vm->update_funcs->commit(¶ms, &vm->last_update); |
1257 | if (r) | |
1258 | goto error; | |
6989f246 CK |
1259 | return 0; |
1260 | ||
1261 | error: | |
d4085ea9 | 1262 | amdgpu_vm_invalidate_pds(adev, vm); |
92456b93 | 1263 | return r; |
194d2161 CK |
1264 | } |
1265 | ||
cf2f0a37 | 1266 | /** |
e95b93ce | 1267 | * amdgpu_vm_update_flags - figure out flags for PTE updates |
cf2f0a37 | 1268 | * |
dfcd99f6 | 1269 | * Make sure to set the right flags for the PTEs at the desired level. |
cf2f0a37 | 1270 | */ |
d1e29462 | 1271 | static void amdgpu_vm_update_flags(struct amdgpu_vm_update_params *params, |
e95b93ce CK |
1272 | struct amdgpu_bo *bo, unsigned level, |
1273 | uint64_t pe, uint64_t addr, | |
1274 | unsigned count, uint32_t incr, | |
1275 | uint64_t flags) | |
cf2f0a37 | 1276 | |
dfcd99f6 CK |
1277 | { |
1278 | if (level != AMDGPU_VM_PTB) { | |
cf2f0a37 | 1279 | flags |= AMDGPU_PDE_PTE; |
dfcd99f6 | 1280 | amdgpu_gmc_get_vm_pde(params->adev, level, &addr, &flags); |
e95b93ce CK |
1281 | |
1282 | } else if (params->adev->asic_type >= CHIP_VEGA10 && | |
1283 | !(flags & AMDGPU_PTE_VALID) && | |
1284 | !(flags & AMDGPU_PTE_PRT)) { | |
1285 | ||
1286 | /* Workaround for fault priority problem on GMC9 */ | |
1287 | flags |= AMDGPU_PTE_EXECUTABLE; | |
cf2f0a37 AD |
1288 | } |
1289 | ||
c3546695 CK |
1290 | params->vm->update_funcs->update(params, bo, pe, addr, count, incr, |
1291 | flags); | |
dfcd99f6 CK |
1292 | } |
1293 | ||
1294 | /** | |
1295 | * amdgpu_vm_fragment - get fragment for PTEs | |
1296 | * | |
d1e29462 | 1297 | * @params: see amdgpu_vm_update_params definition |
dfcd99f6 CK |
1298 | * @start: first PTE to handle |
1299 | * @end: last PTE to handle | |
1300 | * @flags: hw mapping flags | |
1301 | * @frag: resulting fragment size | |
1302 | * @frag_end: end of this fragment | |
1303 | * | |
1304 | * Returns the first possible fragment for the start and end address. | |
1305 | */ | |
d1e29462 | 1306 | static void amdgpu_vm_fragment(struct amdgpu_vm_update_params *params, |
dfcd99f6 CK |
1307 | uint64_t start, uint64_t end, uint64_t flags, |
1308 | unsigned int *frag, uint64_t *frag_end) | |
1309 | { | |
1310 | /** | |
1311 | * The MC L1 TLB supports variable sized pages, based on a fragment | |
1312 | * field in the PTE. When this field is set to a non-zero value, page | |
1313 | * granularity is increased from 4KB to (1 << (12 + frag)). The PTE | |
1314 | * flags are considered valid for all PTEs within the fragment range | |
1315 | * and corresponding mappings are assumed to be physically contiguous. | |
1316 | * | |
1317 | * The L1 TLB can store a single PTE for the whole fragment, | |
1318 | * significantly increasing the space available for translation | |
1319 | * caching. This leads to large improvements in throughput when the | |
1320 | * TLB is under pressure. | |
1321 | * | |
1322 | * The L2 TLB distributes small and large fragments into two | |
1323 | * asymmetric partitions. The large fragment cache is significantly | |
1324 | * larger. Thus, we try to use large fragments wherever possible. | |
1325 | * Userspace can support this by aligning virtual base address and | |
1326 | * allocation size to the fragment size. | |
1b1d5c43 CK |
1327 | * |
1328 | * Starting with Vega10 the fragment size only controls the L1. The L2 | |
1329 | * is now directly feed with small/huge/giant pages from the walker. | |
dfcd99f6 | 1330 | */ |
1b1d5c43 CK |
1331 | unsigned max_frag; |
1332 | ||
1333 | if (params->adev->asic_type < CHIP_VEGA10) | |
1334 | max_frag = params->adev->vm_manager.fragment_size; | |
1335 | else | |
1336 | max_frag = 31; | |
dfcd99f6 CK |
1337 | |
1338 | /* system pages are non continuously */ | |
072b7a0b | 1339 | if (params->pages_addr) { |
dfcd99f6 CK |
1340 | *frag = 0; |
1341 | *frag_end = end; | |
ec5207c9 | 1342 | return; |
3cc1d3ea | 1343 | } |
cf2f0a37 | 1344 | |
dfcd99f6 CK |
1345 | /* This intentionally wraps around if no bit is set */ |
1346 | *frag = min((unsigned)ffs(start) - 1, (unsigned)fls64(end - start) - 1); | |
1347 | if (*frag >= max_frag) { | |
1348 | *frag = max_frag; | |
1349 | *frag_end = end & ~((1ULL << max_frag) - 1); | |
1350 | } else { | |
1351 | *frag_end = start + (1 << *frag); | |
1352 | } | |
4e2cb640 CK |
1353 | } |
1354 | ||
d38ceaf9 AD |
1355 | /** |
1356 | * amdgpu_vm_update_ptes - make sure that page tables are valid | |
1357 | * | |
d1e29462 | 1358 | * @params: see amdgpu_vm_update_params definition |
d38ceaf9 AD |
1359 | * @start: start of GPU address range |
1360 | * @end: end of GPU address range | |
677131a1 | 1361 | * @dst: destination address to map to, the next dst inside the function |
d38ceaf9 AD |
1362 | * @flags: mapping flags |
1363 | * | |
8843dbbb | 1364 | * Update the page tables in the range @start - @end. |
7fc48e59 AG |
1365 | * |
1366 | * Returns: | |
1367 | * 0 for success, -EINVAL for failure. | |
d38ceaf9 | 1368 | */ |
d1e29462 | 1369 | static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params, |
dfcd99f6 CK |
1370 | uint64_t start, uint64_t end, |
1371 | uint64_t dst, uint64_t flags) | |
d38ceaf9 | 1372 | { |
36b32a68 | 1373 | struct amdgpu_device *adev = params->adev; |
dfa70550 | 1374 | struct amdgpu_vm_pt_cursor cursor; |
dfcd99f6 CK |
1375 | uint64_t frag_start = start, frag_end; |
1376 | unsigned int frag; | |
0ce15d6f | 1377 | int r; |
dfcd99f6 CK |
1378 | |
1379 | /* figure out the initial fragment */ | |
1380 | amdgpu_vm_fragment(params, frag_start, end, flags, &frag, &frag_end); | |
d38ceaf9 | 1381 | |
dfcd99f6 CK |
1382 | /* walk over the address space and update the PTs */ |
1383 | amdgpu_vm_pt_start(adev, params->vm, start, &cursor); | |
1384 | while (cursor.pfn < end) { | |
cb90b97b | 1385 | unsigned shift, parent_shift, mask; |
dfcd99f6 | 1386 | uint64_t incr, entry_end, pe_start; |
0ce15d6f | 1387 | struct amdgpu_bo *pt; |
cf2f0a37 | 1388 | |
0ce15d6f | 1389 | r = amdgpu_vm_alloc_pts(params->adev, params->vm, &cursor); |
adc7e863 | 1390 | if (r) |
0ce15d6f CK |
1391 | return r; |
1392 | ||
1393 | pt = cursor.entry->base.bo; | |
4e2cb640 | 1394 | |
dfcd99f6 CK |
1395 | /* The root level can't be a huge page */ |
1396 | if (cursor.level == adev->vm_manager.root_level) { | |
1397 | if (!amdgpu_vm_pt_descendant(adev, &cursor)) | |
1398 | return -ENOENT; | |
cf2f0a37 | 1399 | continue; |
dfa70550 | 1400 | } |
cf2f0a37 | 1401 | |
dfcd99f6 CK |
1402 | shift = amdgpu_vm_level_shift(adev, cursor.level); |
1403 | parent_shift = amdgpu_vm_level_shift(adev, cursor.level - 1); | |
8ce1f7e7 CK |
1404 | if (adev->asic_type < CHIP_VEGA10 && |
1405 | (flags & AMDGPU_PTE_VALID)) { | |
dfcd99f6 CK |
1406 | /* No huge page support before GMC v9 */ |
1407 | if (cursor.level != AMDGPU_VM_PTB) { | |
1408 | if (!amdgpu_vm_pt_descendant(adev, &cursor)) | |
1409 | return -ENOENT; | |
1410 | continue; | |
1411 | } | |
1412 | } else if (frag < shift) { | |
1413 | /* We can't use this level when the fragment size is | |
1414 | * smaller than the address shift. Go to the next | |
1415 | * child entry and try again. | |
1416 | */ | |
1417 | if (!amdgpu_vm_pt_descendant(adev, &cursor)) | |
1418 | return -ENOENT; | |
1419 | continue; | |
1954db15 FK |
1420 | } else if (frag >= parent_shift && |
1421 | cursor.level - 1 != adev->vm_manager.root_level) { | |
dfcd99f6 | 1422 | /* If the fragment size is even larger than the parent |
1954db15 FK |
1423 | * shift we should go up one level and check it again |
1424 | * unless one level up is the root level. | |
dfcd99f6 CK |
1425 | */ |
1426 | if (!amdgpu_vm_pt_ancestor(&cursor)) | |
1427 | return -ENOENT; | |
1428 | continue; | |
6849d47c RH |
1429 | } |
1430 | ||
dfcd99f6 | 1431 | /* Looks good so far, calculate parameters for the update */ |
9ce2b991 | 1432 | incr = (uint64_t)AMDGPU_GPU_PAGE_SIZE << shift; |
cb90b97b CK |
1433 | mask = amdgpu_vm_entries_mask(adev, cursor.level); |
1434 | pe_start = ((cursor.pfn >> shift) & mask) * 8; | |
9ce2b991 | 1435 | entry_end = (uint64_t)(mask + 1) << shift; |
dfcd99f6 CK |
1436 | entry_end += cursor.pfn & ~(entry_end - 1); |
1437 | entry_end = min(entry_end, end); | |
1438 | ||
1439 | do { | |
1440 | uint64_t upd_end = min(entry_end, frag_end); | |
1441 | unsigned nptes = (upd_end - frag_start) >> shift; | |
1442 | ||
e95b93ce CK |
1443 | amdgpu_vm_update_flags(params, pt, cursor.level, |
1444 | pe_start, dst, nptes, incr, | |
1445 | flags | AMDGPU_PTE_FRAG(frag)); | |
dfcd99f6 CK |
1446 | |
1447 | pe_start += nptes * 8; | |
9ce2b991 | 1448 | dst += (uint64_t)nptes * AMDGPU_GPU_PAGE_SIZE << shift; |
dfcd99f6 CK |
1449 | |
1450 | frag_start = upd_end; | |
1451 | if (frag_start >= frag_end) { | |
1452 | /* figure out the next fragment */ | |
1453 | amdgpu_vm_fragment(params, frag_start, end, | |
1454 | flags, &frag, &frag_end); | |
1455 | if (frag < shift) | |
1456 | break; | |
1457 | } | |
1458 | } while (frag_start < entry_end); | |
92696dd5 | 1459 | |
c1a17777 | 1460 | if (amdgpu_vm_pt_descendant(adev, &cursor)) { |
adc7bfe5 | 1461 | /* Free all child entries */ |
c1a17777 | 1462 | while (cursor.pfn < frag_start) { |
e35fb064 | 1463 | amdgpu_vm_free_pts(adev, params->vm, &cursor); |
c1a17777 CK |
1464 | amdgpu_vm_pt_next(adev, &cursor); |
1465 | } | |
1466 | ||
1467 | } else if (frag >= shift) { | |
1468 | /* or just move on to the next on the same level. */ | |
dfcd99f6 | 1469 | amdgpu_vm_pt_next(adev, &cursor); |
c1a17777 | 1470 | } |
92696dd5 | 1471 | } |
6849d47c RH |
1472 | |
1473 | return 0; | |
d38ceaf9 AD |
1474 | } |
1475 | ||
d38ceaf9 AD |
1476 | /** |
1477 | * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table | |
1478 | * | |
1479 | * @adev: amdgpu_device pointer | |
3cabaa54 | 1480 | * @exclusive: fence we need to sync to |
fa3ab3c7 | 1481 | * @pages_addr: DMA addresses to use for mapping |
d38ceaf9 | 1482 | * @vm: requested vm |
a14faa65 CK |
1483 | * @start: start of mapped range |
1484 | * @last: last mapped entry | |
1485 | * @flags: flags for the entries | |
d38ceaf9 | 1486 | * @addr: addr to set the area to |
d38ceaf9 AD |
1487 | * @fence: optional resulting fence |
1488 | * | |
a14faa65 | 1489 | * Fill in the page table entries between @start and @last. |
7fc48e59 AG |
1490 | * |
1491 | * Returns: | |
1492 | * 0 for success, -EINVAL for failure. | |
d38ceaf9 AD |
1493 | */ |
1494 | static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, | |
f54d1867 | 1495 | struct dma_fence *exclusive, |
fa3ab3c7 | 1496 | dma_addr_t *pages_addr, |
d38ceaf9 | 1497 | struct amdgpu_vm *vm, |
a14faa65 | 1498 | uint64_t start, uint64_t last, |
6b777607 | 1499 | uint64_t flags, uint64_t addr, |
f54d1867 | 1500 | struct dma_fence **fence) |
d38ceaf9 | 1501 | { |
d1e29462 | 1502 | struct amdgpu_vm_update_params params; |
a1e08d3b | 1503 | void *owner = AMDGPU_FENCE_OWNER_VM; |
d38ceaf9 AD |
1504 | int r; |
1505 | ||
afef8b8f CK |
1506 | memset(¶ms, 0, sizeof(params)); |
1507 | params.adev = adev; | |
49ac8a24 | 1508 | params.vm = vm; |
072b7a0b | 1509 | params.pages_addr = pages_addr; |
afef8b8f | 1510 | |
8db588d5 | 1511 | /* sync to everything except eviction fences on unmapping */ |
a33cab7a | 1512 | if (!(flags & AMDGPU_PTE_VALID)) |
8db588d5 | 1513 | owner = AMDGPU_FENCE_OWNER_KFD; |
a33cab7a | 1514 | |
c3546695 | 1515 | r = vm->update_funcs->prepare(¶ms, owner, exclusive); |
d71518b5 | 1516 | if (r) |
d38ceaf9 | 1517 | return r; |
d71518b5 | 1518 | |
dfcd99f6 | 1519 | r = amdgpu_vm_update_ptes(¶ms, start, last + 1, addr, flags); |
cc28c4ed | 1520 | if (r) |
c3546695 | 1521 | return r; |
d5fc5e82 | 1522 | |
c3546695 | 1523 | return vm->update_funcs->commit(¶ms, fence); |
d38ceaf9 AD |
1524 | } |
1525 | ||
a14faa65 CK |
1526 | /** |
1527 | * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks | |
1528 | * | |
1529 | * @adev: amdgpu_device pointer | |
3cabaa54 | 1530 | * @exclusive: fence we need to sync to |
8358dcee | 1531 | * @pages_addr: DMA addresses to use for mapping |
a14faa65 CK |
1532 | * @vm: requested vm |
1533 | * @mapping: mapped range and flags to use for the update | |
8358dcee | 1534 | * @flags: HW flags for the mapping |
a690aa0f | 1535 | * @bo_adev: amdgpu_device pointer that bo actually been allocated |
63e0ba40 | 1536 | * @nodes: array of drm_mm_nodes with the MC addresses |
a14faa65 CK |
1537 | * @fence: optional resulting fence |
1538 | * | |
1539 | * Split the mapping into smaller chunks so that each update fits | |
1540 | * into a SDMA IB. | |
7fc48e59 AG |
1541 | * |
1542 | * Returns: | |
1543 | * 0 for success, -EINVAL for failure. | |
a14faa65 CK |
1544 | */ |
1545 | static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev, | |
f54d1867 | 1546 | struct dma_fence *exclusive, |
8358dcee | 1547 | dma_addr_t *pages_addr, |
a14faa65 CK |
1548 | struct amdgpu_vm *vm, |
1549 | struct amdgpu_bo_va_mapping *mapping, | |
6b777607 | 1550 | uint64_t flags, |
a690aa0f | 1551 | struct amdgpu_device *bo_adev, |
63e0ba40 | 1552 | struct drm_mm_node *nodes, |
f54d1867 | 1553 | struct dma_fence **fence) |
a14faa65 | 1554 | { |
9fc8fc70 | 1555 | unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size; |
570144c6 | 1556 | uint64_t pfn, start = mapping->start; |
a14faa65 CK |
1557 | int r; |
1558 | ||
1559 | /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here | |
1560 | * but in case of something, we filter the flags in first place | |
1561 | */ | |
1562 | if (!(mapping->flags & AMDGPU_PTE_READABLE)) | |
1563 | flags &= ~AMDGPU_PTE_READABLE; | |
1564 | if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) | |
1565 | flags &= ~AMDGPU_PTE_WRITEABLE; | |
1566 | ||
15b31c59 AX |
1567 | flags &= ~AMDGPU_PTE_EXECUTABLE; |
1568 | flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE; | |
1569 | ||
b0fd18b0 AX |
1570 | flags &= ~AMDGPU_PTE_MTYPE_MASK; |
1571 | flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK); | |
1572 | ||
d0766e98 ZJ |
1573 | if ((mapping->flags & AMDGPU_PTE_PRT) && |
1574 | (adev->asic_type >= CHIP_VEGA10)) { | |
1575 | flags |= AMDGPU_PTE_PRT; | |
1576 | flags &= ~AMDGPU_PTE_VALID; | |
1577 | } | |
1578 | ||
a14faa65 CK |
1579 | trace_amdgpu_vm_bo_update(mapping); |
1580 | ||
63e0ba40 CK |
1581 | pfn = mapping->offset >> PAGE_SHIFT; |
1582 | if (nodes) { | |
1583 | while (pfn >= nodes->size) { | |
1584 | pfn -= nodes->size; | |
1585 | ++nodes; | |
1586 | } | |
fa3ab3c7 | 1587 | } |
a14faa65 | 1588 | |
63e0ba40 | 1589 | do { |
9fc8fc70 | 1590 | dma_addr_t *dma_addr = NULL; |
63e0ba40 CK |
1591 | uint64_t max_entries; |
1592 | uint64_t addr, last; | |
a14faa65 | 1593 | |
63e0ba40 CK |
1594 | if (nodes) { |
1595 | addr = nodes->start << PAGE_SHIFT; | |
1596 | max_entries = (nodes->size - pfn) * | |
463d2fe8 | 1597 | AMDGPU_GPU_PAGES_IN_CPU_PAGE; |
63e0ba40 CK |
1598 | } else { |
1599 | addr = 0; | |
1600 | max_entries = S64_MAX; | |
1601 | } | |
a14faa65 | 1602 | |
63e0ba40 | 1603 | if (pages_addr) { |
9fc8fc70 CK |
1604 | uint64_t count; |
1605 | ||
38e624a1 | 1606 | for (count = 1; |
463d2fe8 | 1607 | count < max_entries / AMDGPU_GPU_PAGES_IN_CPU_PAGE; |
38e624a1 | 1608 | ++count) { |
9fc8fc70 CK |
1609 | uint64_t idx = pfn + count; |
1610 | ||
1611 | if (pages_addr[idx] != | |
1612 | (pages_addr[idx - 1] + PAGE_SIZE)) | |
1613 | break; | |
1614 | } | |
1615 | ||
1616 | if (count < min_linear_pages) { | |
1617 | addr = pfn << PAGE_SHIFT; | |
1618 | dma_addr = pages_addr; | |
1619 | } else { | |
1620 | addr = pages_addr[pfn]; | |
463d2fe8 | 1621 | max_entries = count * AMDGPU_GPU_PAGES_IN_CPU_PAGE; |
9fc8fc70 CK |
1622 | } |
1623 | ||
63e0ba40 | 1624 | } else if (flags & AMDGPU_PTE_VALID) { |
a690aa0f | 1625 | addr += bo_adev->vm_manager.vram_base_offset; |
9fc8fc70 | 1626 | addr += pfn << PAGE_SHIFT; |
63e0ba40 | 1627 | } |
63e0ba40 | 1628 | |
a9f87f64 | 1629 | last = min((uint64_t)mapping->last, start + max_entries - 1); |
9fc8fc70 | 1630 | r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm, |
a14faa65 CK |
1631 | start, last, flags, addr, |
1632 | fence); | |
1633 | if (r) | |
1634 | return r; | |
1635 | ||
463d2fe8 | 1636 | pfn += (last - start + 1) / AMDGPU_GPU_PAGES_IN_CPU_PAGE; |
63e0ba40 CK |
1637 | if (nodes && nodes->size == pfn) { |
1638 | pfn = 0; | |
1639 | ++nodes; | |
1640 | } | |
a14faa65 | 1641 | start = last + 1; |
63e0ba40 | 1642 | |
a9f87f64 | 1643 | } while (unlikely(start != mapping->last + 1)); |
a14faa65 CK |
1644 | |
1645 | return 0; | |
1646 | } | |
1647 | ||
d38ceaf9 AD |
1648 | /** |
1649 | * amdgpu_vm_bo_update - update all BO mappings in the vm page table | |
1650 | * | |
1651 | * @adev: amdgpu_device pointer | |
1652 | * @bo_va: requested BO and VM object | |
99e124f4 | 1653 | * @clear: if true clear the entries |
d38ceaf9 AD |
1654 | * |
1655 | * Fill in the page table entries for @bo_va. | |
7fc48e59 AG |
1656 | * |
1657 | * Returns: | |
1658 | * 0 for success, -EINVAL for failure. | |
d38ceaf9 AD |
1659 | */ |
1660 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, | |
1661 | struct amdgpu_bo_va *bo_va, | |
99e124f4 | 1662 | bool clear) |
d38ceaf9 | 1663 | { |
ec681545 CK |
1664 | struct amdgpu_bo *bo = bo_va->base.bo; |
1665 | struct amdgpu_vm *vm = bo_va->base.vm; | |
d38ceaf9 | 1666 | struct amdgpu_bo_va_mapping *mapping; |
8358dcee | 1667 | dma_addr_t *pages_addr = NULL; |
99e124f4 | 1668 | struct ttm_mem_reg *mem; |
63e0ba40 | 1669 | struct drm_mm_node *nodes; |
4e55eb38 | 1670 | struct dma_fence *exclusive, **last_update; |
457e0fee | 1671 | uint64_t flags; |
86f7bae5 | 1672 | struct amdgpu_device *bo_adev = adev; |
d38ceaf9 AD |
1673 | int r; |
1674 | ||
7eb80427 | 1675 | if (clear || !bo) { |
99e124f4 | 1676 | mem = NULL; |
63e0ba40 | 1677 | nodes = NULL; |
99e124f4 CK |
1678 | exclusive = NULL; |
1679 | } else { | |
8358dcee CK |
1680 | struct ttm_dma_tt *ttm; |
1681 | ||
7eb80427 | 1682 | mem = &bo->tbo.mem; |
63e0ba40 CK |
1683 | nodes = mem->mm_node; |
1684 | if (mem->mem_type == TTM_PL_TT) { | |
7eb80427 | 1685 | ttm = container_of(bo->tbo.ttm, struct ttm_dma_tt, ttm); |
8358dcee | 1686 | pages_addr = ttm->dma_address; |
9ab21462 | 1687 | } |
ec681545 | 1688 | exclusive = reservation_object_get_excl(bo->tbo.resv); |
d38ceaf9 AD |
1689 | } |
1690 | ||
a690aa0f | 1691 | if (bo) { |
ec681545 | 1692 | flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem); |
a690aa0f | 1693 | bo_adev = amdgpu_ttm_adev(bo->tbo.bdev); |
1694 | } else { | |
a5f6b5b1 | 1695 | flags = 0x0; |
a690aa0f | 1696 | } |
d38ceaf9 | 1697 | |
4e55eb38 CK |
1698 | if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv)) |
1699 | last_update = &vm->last_update; | |
1700 | else | |
1701 | last_update = &bo_va->last_pt_update; | |
1702 | ||
3d7d4d3a CK |
1703 | if (!clear && bo_va->base.moved) { |
1704 | bo_va->base.moved = false; | |
7fc11959 | 1705 | list_splice_init(&bo_va->valids, &bo_va->invalids); |
3d7d4d3a | 1706 | |
cb7b6ec2 CK |
1707 | } else if (bo_va->cleared != clear) { |
1708 | list_splice_init(&bo_va->valids, &bo_va->invalids); | |
3d7d4d3a | 1709 | } |
7fc11959 CK |
1710 | |
1711 | list_for_each_entry(mapping, &bo_va->invalids, list) { | |
457e0fee | 1712 | r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm, |
a690aa0f | 1713 | mapping, flags, bo_adev, nodes, |
4e55eb38 | 1714 | last_update); |
d38ceaf9 AD |
1715 | if (r) |
1716 | return r; | |
1717 | } | |
1718 | ||
cb7b6ec2 CK |
1719 | if (vm->use_cpu_for_update) { |
1720 | /* Flush HDP */ | |
1721 | mb(); | |
69882565 | 1722 | amdgpu_asic_flush_hdp(adev, NULL); |
d6c10f6b CK |
1723 | } |
1724 | ||
bb475839 JZ |
1725 | /* If the BO is not in its preferred location add it back to |
1726 | * the evicted list so that it gets validated again on the | |
1727 | * next command submission. | |
1728 | */ | |
806f043f CK |
1729 | if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) { |
1730 | uint32_t mem_type = bo->tbo.mem.mem_type; | |
1731 | ||
1732 | if (!(bo->preferred_domains & amdgpu_mem_type_to_domain(mem_type))) | |
bcdc9fd6 | 1733 | amdgpu_vm_bo_evicted(&bo_va->base); |
806f043f | 1734 | else |
bcdc9fd6 | 1735 | amdgpu_vm_bo_idle(&bo_va->base); |
c12a2ee5 | 1736 | } else { |
bcdc9fd6 | 1737 | amdgpu_vm_bo_done(&bo_va->base); |
806f043f | 1738 | } |
d38ceaf9 | 1739 | |
cb7b6ec2 CK |
1740 | list_splice_init(&bo_va->invalids, &bo_va->valids); |
1741 | bo_va->cleared = clear; | |
1742 | ||
1743 | if (trace_amdgpu_vm_bo_mapping_enabled()) { | |
1744 | list_for_each_entry(mapping, &bo_va->valids, list) | |
1745 | trace_amdgpu_vm_bo_mapping(mapping); | |
68c62306 CK |
1746 | } |
1747 | ||
d38ceaf9 AD |
1748 | return 0; |
1749 | } | |
1750 | ||
284710fa CK |
1751 | /** |
1752 | * amdgpu_vm_update_prt_state - update the global PRT state | |
7fc48e59 AG |
1753 | * |
1754 | * @adev: amdgpu_device pointer | |
284710fa CK |
1755 | */ |
1756 | static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) | |
1757 | { | |
1758 | unsigned long flags; | |
1759 | bool enable; | |
1760 | ||
1761 | spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); | |
451bc8eb | 1762 | enable = !!atomic_read(&adev->vm_manager.num_prt_users); |
132f34e4 | 1763 | adev->gmc.gmc_funcs->set_prt(adev, enable); |
284710fa CK |
1764 | spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); |
1765 | } | |
1766 | ||
451bc8eb | 1767 | /** |
4388fc2a | 1768 | * amdgpu_vm_prt_get - add a PRT user |
7fc48e59 AG |
1769 | * |
1770 | * @adev: amdgpu_device pointer | |
451bc8eb CK |
1771 | */ |
1772 | static void amdgpu_vm_prt_get(struct amdgpu_device *adev) | |
1773 | { | |
132f34e4 | 1774 | if (!adev->gmc.gmc_funcs->set_prt) |
4388fc2a CK |
1775 | return; |
1776 | ||
451bc8eb CK |
1777 | if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) |
1778 | amdgpu_vm_update_prt_state(adev); | |
1779 | } | |
1780 | ||
0b15f2fc CK |
1781 | /** |
1782 | * amdgpu_vm_prt_put - drop a PRT user | |
7fc48e59 AG |
1783 | * |
1784 | * @adev: amdgpu_device pointer | |
0b15f2fc CK |
1785 | */ |
1786 | static void amdgpu_vm_prt_put(struct amdgpu_device *adev) | |
1787 | { | |
451bc8eb | 1788 | if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) |
0b15f2fc CK |
1789 | amdgpu_vm_update_prt_state(adev); |
1790 | } | |
1791 | ||
284710fa | 1792 | /** |
451bc8eb | 1793 | * amdgpu_vm_prt_cb - callback for updating the PRT status |
7fc48e59 AG |
1794 | * |
1795 | * @fence: fence for the callback | |
00553cf8 | 1796 | * @_cb: the callback function |
284710fa CK |
1797 | */ |
1798 | static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) | |
1799 | { | |
1800 | struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); | |
1801 | ||
0b15f2fc | 1802 | amdgpu_vm_prt_put(cb->adev); |
284710fa CK |
1803 | kfree(cb); |
1804 | } | |
1805 | ||
451bc8eb CK |
1806 | /** |
1807 | * amdgpu_vm_add_prt_cb - add callback for updating the PRT status | |
7fc48e59 AG |
1808 | * |
1809 | * @adev: amdgpu_device pointer | |
1810 | * @fence: fence for the callback | |
451bc8eb CK |
1811 | */ |
1812 | static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, | |
1813 | struct dma_fence *fence) | |
1814 | { | |
4388fc2a | 1815 | struct amdgpu_prt_cb *cb; |
451bc8eb | 1816 | |
132f34e4 | 1817 | if (!adev->gmc.gmc_funcs->set_prt) |
4388fc2a CK |
1818 | return; |
1819 | ||
1820 | cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); | |
451bc8eb CK |
1821 | if (!cb) { |
1822 | /* Last resort when we are OOM */ | |
1823 | if (fence) | |
1824 | dma_fence_wait(fence, false); | |
1825 | ||
486a68f5 | 1826 | amdgpu_vm_prt_put(adev); |
451bc8eb CK |
1827 | } else { |
1828 | cb->adev = adev; | |
1829 | if (!fence || dma_fence_add_callback(fence, &cb->cb, | |
1830 | amdgpu_vm_prt_cb)) | |
1831 | amdgpu_vm_prt_cb(fence, &cb->cb); | |
1832 | } | |
1833 | } | |
1834 | ||
284710fa CK |
1835 | /** |
1836 | * amdgpu_vm_free_mapping - free a mapping | |
1837 | * | |
1838 | * @adev: amdgpu_device pointer | |
1839 | * @vm: requested vm | |
1840 | * @mapping: mapping to be freed | |
1841 | * @fence: fence of the unmap operation | |
1842 | * | |
1843 | * Free a mapping and make sure we decrease the PRT usage count if applicable. | |
1844 | */ | |
1845 | static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, | |
1846 | struct amdgpu_vm *vm, | |
1847 | struct amdgpu_bo_va_mapping *mapping, | |
1848 | struct dma_fence *fence) | |
1849 | { | |
451bc8eb CK |
1850 | if (mapping->flags & AMDGPU_PTE_PRT) |
1851 | amdgpu_vm_add_prt_cb(adev, fence); | |
1852 | kfree(mapping); | |
1853 | } | |
284710fa | 1854 | |
451bc8eb CK |
1855 | /** |
1856 | * amdgpu_vm_prt_fini - finish all prt mappings | |
1857 | * | |
1858 | * @adev: amdgpu_device pointer | |
1859 | * @vm: requested vm | |
1860 | * | |
1861 | * Register a cleanup callback to disable PRT support after VM dies. | |
1862 | */ | |
1863 | static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
1864 | { | |
3f3333f8 | 1865 | struct reservation_object *resv = vm->root.base.bo->tbo.resv; |
451bc8eb CK |
1866 | struct dma_fence *excl, **shared; |
1867 | unsigned i, shared_count; | |
1868 | int r; | |
0b15f2fc | 1869 | |
451bc8eb CK |
1870 | r = reservation_object_get_fences_rcu(resv, &excl, |
1871 | &shared_count, &shared); | |
1872 | if (r) { | |
1873 | /* Not enough memory to grab the fence list, as last resort | |
1874 | * block for all the fences to complete. | |
1875 | */ | |
1876 | reservation_object_wait_timeout_rcu(resv, true, false, | |
1877 | MAX_SCHEDULE_TIMEOUT); | |
1878 | return; | |
284710fa | 1879 | } |
451bc8eb CK |
1880 | |
1881 | /* Add a callback for each fence in the reservation object */ | |
1882 | amdgpu_vm_prt_get(adev); | |
1883 | amdgpu_vm_add_prt_cb(adev, excl); | |
1884 | ||
1885 | for (i = 0; i < shared_count; ++i) { | |
1886 | amdgpu_vm_prt_get(adev); | |
1887 | amdgpu_vm_add_prt_cb(adev, shared[i]); | |
1888 | } | |
1889 | ||
1890 | kfree(shared); | |
284710fa CK |
1891 | } |
1892 | ||
d38ceaf9 AD |
1893 | /** |
1894 | * amdgpu_vm_clear_freed - clear freed BOs in the PT | |
1895 | * | |
1896 | * @adev: amdgpu_device pointer | |
1897 | * @vm: requested vm | |
f3467818 NH |
1898 | * @fence: optional resulting fence (unchanged if no work needed to be done |
1899 | * or if an error occurred) | |
d38ceaf9 AD |
1900 | * |
1901 | * Make sure all freed BOs are cleared in the PT. | |
d38ceaf9 | 1902 | * PTs have to be reserved and mutex must be locked! |
7fc48e59 AG |
1903 | * |
1904 | * Returns: | |
1905 | * 0 for success. | |
1906 | * | |
d38ceaf9 AD |
1907 | */ |
1908 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, | |
f3467818 NH |
1909 | struct amdgpu_vm *vm, |
1910 | struct dma_fence **fence) | |
d38ceaf9 AD |
1911 | { |
1912 | struct amdgpu_bo_va_mapping *mapping; | |
4584312d | 1913 | uint64_t init_pte_value = 0; |
f3467818 | 1914 | struct dma_fence *f = NULL; |
d38ceaf9 AD |
1915 | int r; |
1916 | ||
1917 | while (!list_empty(&vm->freed)) { | |
1918 | mapping = list_first_entry(&vm->freed, | |
1919 | struct amdgpu_bo_va_mapping, list); | |
1920 | list_del(&mapping->list); | |
e17841b9 | 1921 | |
ad9a5b78 CK |
1922 | if (vm->pte_support_ats && |
1923 | mapping->start < AMDGPU_GMC_HOLE_START) | |
6d16dac8 | 1924 | init_pte_value = AMDGPU_PTE_DEFAULT_ATC; |
51ac7eec | 1925 | |
570144c6 | 1926 | r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm, |
fc6aa33d | 1927 | mapping->start, mapping->last, |
51ac7eec | 1928 | init_pte_value, 0, &f); |
f3467818 | 1929 | amdgpu_vm_free_mapping(adev, vm, mapping, f); |
284710fa | 1930 | if (r) { |
f3467818 | 1931 | dma_fence_put(f); |
d38ceaf9 | 1932 | return r; |
284710fa | 1933 | } |
f3467818 | 1934 | } |
d38ceaf9 | 1935 | |
f3467818 NH |
1936 | if (fence && f) { |
1937 | dma_fence_put(*fence); | |
1938 | *fence = f; | |
1939 | } else { | |
1940 | dma_fence_put(f); | |
d38ceaf9 | 1941 | } |
f3467818 | 1942 | |
d38ceaf9 AD |
1943 | return 0; |
1944 | ||
1945 | } | |
1946 | ||
1947 | /** | |
73fb16e7 | 1948 | * amdgpu_vm_handle_moved - handle moved BOs in the PT |
d38ceaf9 AD |
1949 | * |
1950 | * @adev: amdgpu_device pointer | |
1951 | * @vm: requested vm | |
1952 | * | |
73fb16e7 | 1953 | * Make sure all BOs which are moved are updated in the PTs. |
7fc48e59 AG |
1954 | * |
1955 | * Returns: | |
1956 | * 0 for success. | |
d38ceaf9 | 1957 | * |
73fb16e7 | 1958 | * PTs have to be reserved! |
d38ceaf9 | 1959 | */ |
73fb16e7 | 1960 | int amdgpu_vm_handle_moved(struct amdgpu_device *adev, |
4e55eb38 | 1961 | struct amdgpu_vm *vm) |
d38ceaf9 | 1962 | { |
789f3317 | 1963 | struct amdgpu_bo_va *bo_va, *tmp; |
c12a2ee5 | 1964 | struct reservation_object *resv; |
73fb16e7 | 1965 | bool clear; |
789f3317 | 1966 | int r; |
d38ceaf9 | 1967 | |
c12a2ee5 CK |
1968 | list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { |
1969 | /* Per VM BOs never need to bo cleared in the page tables */ | |
1970 | r = amdgpu_vm_bo_update(adev, bo_va, false); | |
1971 | if (r) | |
1972 | return r; | |
1973 | } | |
32b41ac2 | 1974 | |
c12a2ee5 CK |
1975 | spin_lock(&vm->invalidated_lock); |
1976 | while (!list_empty(&vm->invalidated)) { | |
1977 | bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va, | |
1978 | base.vm_status); | |
1979 | resv = bo_va->base.bo->tbo.resv; | |
1980 | spin_unlock(&vm->invalidated_lock); | |
ec363e0d | 1981 | |
ec363e0d | 1982 | /* Try to reserve the BO to avoid clearing its ptes */ |
c12a2ee5 | 1983 | if (!amdgpu_vm_debug && reservation_object_trylock(resv)) |
ec363e0d CK |
1984 | clear = false; |
1985 | /* Somebody else is using the BO right now */ | |
1986 | else | |
1987 | clear = true; | |
73fb16e7 CK |
1988 | |
1989 | r = amdgpu_vm_bo_update(adev, bo_va, clear); | |
c12a2ee5 | 1990 | if (r) |
d38ceaf9 AD |
1991 | return r; |
1992 | ||
c12a2ee5 | 1993 | if (!clear) |
ec363e0d | 1994 | reservation_object_unlock(resv); |
c12a2ee5 | 1995 | spin_lock(&vm->invalidated_lock); |
d38ceaf9 | 1996 | } |
c12a2ee5 | 1997 | spin_unlock(&vm->invalidated_lock); |
d38ceaf9 | 1998 | |
789f3317 | 1999 | return 0; |
d38ceaf9 AD |
2000 | } |
2001 | ||
2002 | /** | |
2003 | * amdgpu_vm_bo_add - add a bo to a specific vm | |
2004 | * | |
2005 | * @adev: amdgpu_device pointer | |
2006 | * @vm: requested vm | |
2007 | * @bo: amdgpu buffer object | |
2008 | * | |
8843dbbb | 2009 | * Add @bo into the requested vm. |
d38ceaf9 | 2010 | * Add @bo to the list of bos associated with the vm |
7fc48e59 AG |
2011 | * |
2012 | * Returns: | |
2013 | * Newly added bo_va or NULL for failure | |
d38ceaf9 AD |
2014 | * |
2015 | * Object has to be reserved! | |
2016 | */ | |
2017 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, | |
2018 | struct amdgpu_vm *vm, | |
2019 | struct amdgpu_bo *bo) | |
2020 | { | |
2021 | struct amdgpu_bo_va *bo_va; | |
2022 | ||
2023 | bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); | |
2024 | if (bo_va == NULL) { | |
2025 | return NULL; | |
2026 | } | |
3f4299be | 2027 | amdgpu_vm_bo_base_init(&bo_va->base, vm, bo); |
ec681545 | 2028 | |
d38ceaf9 | 2029 | bo_va->ref_count = 1; |
7fc11959 CK |
2030 | INIT_LIST_HEAD(&bo_va->valids); |
2031 | INIT_LIST_HEAD(&bo_va->invalids); | |
32b41ac2 | 2032 | |
df399b06 | 2033 | if (bo && amdgpu_xgmi_same_hive(adev, amdgpu_ttm_adev(bo->tbo.bdev))) { |
2034 | bo_va->is_xgmi = true; | |
2035 | mutex_lock(&adev->vm_manager.lock_pstate); | |
2036 | /* Power up XGMI if it can be potentially used */ | |
2037 | if (++adev->vm_manager.xgmi_map_counter == 1) | |
2038 | amdgpu_xgmi_set_pstate(adev, 1); | |
2039 | mutex_unlock(&adev->vm_manager.lock_pstate); | |
2040 | } | |
2041 | ||
d38ceaf9 AD |
2042 | return bo_va; |
2043 | } | |
2044 | ||
73fb16e7 CK |
2045 | |
2046 | /** | |
2047 | * amdgpu_vm_bo_insert_mapping - insert a new mapping | |
2048 | * | |
2049 | * @adev: amdgpu_device pointer | |
2050 | * @bo_va: bo_va to store the address | |
2051 | * @mapping: the mapping to insert | |
2052 | * | |
2053 | * Insert a new mapping into all structures. | |
2054 | */ | |
2055 | static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, | |
2056 | struct amdgpu_bo_va *bo_va, | |
2057 | struct amdgpu_bo_va_mapping *mapping) | |
2058 | { | |
2059 | struct amdgpu_vm *vm = bo_va->base.vm; | |
2060 | struct amdgpu_bo *bo = bo_va->base.bo; | |
2061 | ||
aebc5e6f | 2062 | mapping->bo_va = bo_va; |
73fb16e7 CK |
2063 | list_add(&mapping->list, &bo_va->invalids); |
2064 | amdgpu_vm_it_insert(mapping, &vm->va); | |
2065 | ||
2066 | if (mapping->flags & AMDGPU_PTE_PRT) | |
2067 | amdgpu_vm_prt_get(adev); | |
2068 | ||
862b8c57 CK |
2069 | if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv && |
2070 | !bo_va->base.moved) { | |
862b8c57 | 2071 | list_move(&bo_va->base.vm_status, &vm->moved); |
73fb16e7 CK |
2072 | } |
2073 | trace_amdgpu_vm_bo_map(bo_va, mapping); | |
2074 | } | |
2075 | ||
d38ceaf9 AD |
2076 | /** |
2077 | * amdgpu_vm_bo_map - map bo inside a vm | |
2078 | * | |
2079 | * @adev: amdgpu_device pointer | |
2080 | * @bo_va: bo_va to store the address | |
2081 | * @saddr: where to map the BO | |
2082 | * @offset: requested offset in the BO | |
00553cf8 | 2083 | * @size: BO size in bytes |
d38ceaf9 AD |
2084 | * @flags: attributes of pages (read/write/valid/etc.) |
2085 | * | |
2086 | * Add a mapping of the BO at the specefied addr into the VM. | |
7fc48e59 AG |
2087 | * |
2088 | * Returns: | |
2089 | * 0 for success, error for failure. | |
d38ceaf9 | 2090 | * |
49b02b18 | 2091 | * Object has to be reserved and unreserved outside! |
d38ceaf9 AD |
2092 | */ |
2093 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, | |
2094 | struct amdgpu_bo_va *bo_va, | |
2095 | uint64_t saddr, uint64_t offset, | |
268c3001 | 2096 | uint64_t size, uint64_t flags) |
d38ceaf9 | 2097 | { |
a9f87f64 | 2098 | struct amdgpu_bo_va_mapping *mapping, *tmp; |
ec681545 CK |
2099 | struct amdgpu_bo *bo = bo_va->base.bo; |
2100 | struct amdgpu_vm *vm = bo_va->base.vm; | |
d38ceaf9 | 2101 | uint64_t eaddr; |
d38ceaf9 | 2102 | |
0be52de9 CK |
2103 | /* validate the parameters */ |
2104 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || | |
49b02b18 | 2105 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
0be52de9 | 2106 | return -EINVAL; |
0be52de9 | 2107 | |
d38ceaf9 | 2108 | /* make sure object fit at this offset */ |
005ae95e | 2109 | eaddr = saddr + size - 1; |
a5f6b5b1 | 2110 | if (saddr >= eaddr || |
ec681545 | 2111 | (bo && offset + size > amdgpu_bo_size(bo))) |
d38ceaf9 | 2112 | return -EINVAL; |
d38ceaf9 | 2113 | |
d38ceaf9 AD |
2114 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
2115 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
2116 | ||
a9f87f64 CK |
2117 | tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); |
2118 | if (tmp) { | |
d38ceaf9 AD |
2119 | /* bo and tmp overlap, invalid addr */ |
2120 | dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " | |
ec681545 | 2121 | "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr, |
a9f87f64 | 2122 | tmp->start, tmp->last + 1); |
663e4577 | 2123 | return -EINVAL; |
d38ceaf9 AD |
2124 | } |
2125 | ||
2126 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); | |
663e4577 CK |
2127 | if (!mapping) |
2128 | return -ENOMEM; | |
d38ceaf9 | 2129 | |
a9f87f64 CK |
2130 | mapping->start = saddr; |
2131 | mapping->last = eaddr; | |
d38ceaf9 AD |
2132 | mapping->offset = offset; |
2133 | mapping->flags = flags; | |
2134 | ||
73fb16e7 | 2135 | amdgpu_vm_bo_insert_map(adev, bo_va, mapping); |
80f95c57 CK |
2136 | |
2137 | return 0; | |
2138 | } | |
2139 | ||
2140 | /** | |
2141 | * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings | |
2142 | * | |
2143 | * @adev: amdgpu_device pointer | |
2144 | * @bo_va: bo_va to store the address | |
2145 | * @saddr: where to map the BO | |
2146 | * @offset: requested offset in the BO | |
00553cf8 | 2147 | * @size: BO size in bytes |
80f95c57 CK |
2148 | * @flags: attributes of pages (read/write/valid/etc.) |
2149 | * | |
2150 | * Add a mapping of the BO at the specefied addr into the VM. Replace existing | |
2151 | * mappings as we do so. | |
7fc48e59 AG |
2152 | * |
2153 | * Returns: | |
2154 | * 0 for success, error for failure. | |
80f95c57 CK |
2155 | * |
2156 | * Object has to be reserved and unreserved outside! | |
2157 | */ | |
2158 | int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, | |
2159 | struct amdgpu_bo_va *bo_va, | |
2160 | uint64_t saddr, uint64_t offset, | |
2161 | uint64_t size, uint64_t flags) | |
2162 | { | |
2163 | struct amdgpu_bo_va_mapping *mapping; | |
ec681545 | 2164 | struct amdgpu_bo *bo = bo_va->base.bo; |
80f95c57 CK |
2165 | uint64_t eaddr; |
2166 | int r; | |
2167 | ||
2168 | /* validate the parameters */ | |
2169 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || | |
2170 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) | |
2171 | return -EINVAL; | |
2172 | ||
2173 | /* make sure object fit at this offset */ | |
2174 | eaddr = saddr + size - 1; | |
2175 | if (saddr >= eaddr || | |
ec681545 | 2176 | (bo && offset + size > amdgpu_bo_size(bo))) |
80f95c57 CK |
2177 | return -EINVAL; |
2178 | ||
2179 | /* Allocate all the needed memory */ | |
2180 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); | |
2181 | if (!mapping) | |
2182 | return -ENOMEM; | |
2183 | ||
ec681545 | 2184 | r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size); |
80f95c57 CK |
2185 | if (r) { |
2186 | kfree(mapping); | |
2187 | return r; | |
2188 | } | |
2189 | ||
2190 | saddr /= AMDGPU_GPU_PAGE_SIZE; | |
2191 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
2192 | ||
a9f87f64 CK |
2193 | mapping->start = saddr; |
2194 | mapping->last = eaddr; | |
80f95c57 CK |
2195 | mapping->offset = offset; |
2196 | mapping->flags = flags; | |
2197 | ||
73fb16e7 | 2198 | amdgpu_vm_bo_insert_map(adev, bo_va, mapping); |
4388fc2a | 2199 | |
d38ceaf9 | 2200 | return 0; |
d38ceaf9 AD |
2201 | } |
2202 | ||
2203 | /** | |
2204 | * amdgpu_vm_bo_unmap - remove bo mapping from vm | |
2205 | * | |
2206 | * @adev: amdgpu_device pointer | |
2207 | * @bo_va: bo_va to remove the address from | |
2208 | * @saddr: where to the BO is mapped | |
2209 | * | |
2210 | * Remove a mapping of the BO at the specefied addr from the VM. | |
7fc48e59 AG |
2211 | * |
2212 | * Returns: | |
2213 | * 0 for success, error for failure. | |
d38ceaf9 | 2214 | * |
49b02b18 | 2215 | * Object has to be reserved and unreserved outside! |
d38ceaf9 AD |
2216 | */ |
2217 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, | |
2218 | struct amdgpu_bo_va *bo_va, | |
2219 | uint64_t saddr) | |
2220 | { | |
2221 | struct amdgpu_bo_va_mapping *mapping; | |
ec681545 | 2222 | struct amdgpu_vm *vm = bo_va->base.vm; |
7fc11959 | 2223 | bool valid = true; |
d38ceaf9 | 2224 | |
6c7fc503 | 2225 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
32b41ac2 | 2226 | |
7fc11959 | 2227 | list_for_each_entry(mapping, &bo_va->valids, list) { |
a9f87f64 | 2228 | if (mapping->start == saddr) |
d38ceaf9 AD |
2229 | break; |
2230 | } | |
2231 | ||
7fc11959 CK |
2232 | if (&mapping->list == &bo_va->valids) { |
2233 | valid = false; | |
2234 | ||
2235 | list_for_each_entry(mapping, &bo_va->invalids, list) { | |
a9f87f64 | 2236 | if (mapping->start == saddr) |
7fc11959 CK |
2237 | break; |
2238 | } | |
2239 | ||
32b41ac2 | 2240 | if (&mapping->list == &bo_va->invalids) |
7fc11959 | 2241 | return -ENOENT; |
d38ceaf9 | 2242 | } |
32b41ac2 | 2243 | |
d38ceaf9 | 2244 | list_del(&mapping->list); |
a9f87f64 | 2245 | amdgpu_vm_it_remove(mapping, &vm->va); |
aebc5e6f | 2246 | mapping->bo_va = NULL; |
93e3e438 | 2247 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
d38ceaf9 | 2248 | |
e17841b9 | 2249 | if (valid) |
d38ceaf9 | 2250 | list_add(&mapping->list, &vm->freed); |
e17841b9 | 2251 | else |
284710fa CK |
2252 | amdgpu_vm_free_mapping(adev, vm, mapping, |
2253 | bo_va->last_pt_update); | |
d38ceaf9 AD |
2254 | |
2255 | return 0; | |
2256 | } | |
2257 | ||
dc54d3d1 CK |
2258 | /** |
2259 | * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range | |
2260 | * | |
2261 | * @adev: amdgpu_device pointer | |
2262 | * @vm: VM structure to use | |
2263 | * @saddr: start of the range | |
2264 | * @size: size of the range | |
2265 | * | |
2266 | * Remove all mappings in a range, split them as appropriate. | |
7fc48e59 AG |
2267 | * |
2268 | * Returns: | |
2269 | * 0 for success, error for failure. | |
dc54d3d1 CK |
2270 | */ |
2271 | int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, | |
2272 | struct amdgpu_vm *vm, | |
2273 | uint64_t saddr, uint64_t size) | |
2274 | { | |
2275 | struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; | |
dc54d3d1 CK |
2276 | LIST_HEAD(removed); |
2277 | uint64_t eaddr; | |
2278 | ||
2279 | eaddr = saddr + size - 1; | |
2280 | saddr /= AMDGPU_GPU_PAGE_SIZE; | |
2281 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
2282 | ||
2283 | /* Allocate all the needed memory */ | |
2284 | before = kzalloc(sizeof(*before), GFP_KERNEL); | |
2285 | if (!before) | |
2286 | return -ENOMEM; | |
27f6d610 | 2287 | INIT_LIST_HEAD(&before->list); |
dc54d3d1 CK |
2288 | |
2289 | after = kzalloc(sizeof(*after), GFP_KERNEL); | |
2290 | if (!after) { | |
2291 | kfree(before); | |
2292 | return -ENOMEM; | |
2293 | } | |
27f6d610 | 2294 | INIT_LIST_HEAD(&after->list); |
dc54d3d1 CK |
2295 | |
2296 | /* Now gather all removed mappings */ | |
a9f87f64 CK |
2297 | tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); |
2298 | while (tmp) { | |
dc54d3d1 | 2299 | /* Remember mapping split at the start */ |
a9f87f64 CK |
2300 | if (tmp->start < saddr) { |
2301 | before->start = tmp->start; | |
2302 | before->last = saddr - 1; | |
dc54d3d1 CK |
2303 | before->offset = tmp->offset; |
2304 | before->flags = tmp->flags; | |
387f49e5 JZ |
2305 | before->bo_va = tmp->bo_va; |
2306 | list_add(&before->list, &tmp->bo_va->invalids); | |
dc54d3d1 CK |
2307 | } |
2308 | ||
2309 | /* Remember mapping split at the end */ | |
a9f87f64 CK |
2310 | if (tmp->last > eaddr) { |
2311 | after->start = eaddr + 1; | |
2312 | after->last = tmp->last; | |
dc54d3d1 | 2313 | after->offset = tmp->offset; |
a9f87f64 | 2314 | after->offset += after->start - tmp->start; |
dc54d3d1 | 2315 | after->flags = tmp->flags; |
387f49e5 JZ |
2316 | after->bo_va = tmp->bo_va; |
2317 | list_add(&after->list, &tmp->bo_va->invalids); | |
dc54d3d1 CK |
2318 | } |
2319 | ||
2320 | list_del(&tmp->list); | |
2321 | list_add(&tmp->list, &removed); | |
a9f87f64 CK |
2322 | |
2323 | tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr); | |
dc54d3d1 CK |
2324 | } |
2325 | ||
2326 | /* And free them up */ | |
2327 | list_for_each_entry_safe(tmp, next, &removed, list) { | |
a9f87f64 | 2328 | amdgpu_vm_it_remove(tmp, &vm->va); |
dc54d3d1 CK |
2329 | list_del(&tmp->list); |
2330 | ||
a9f87f64 CK |
2331 | if (tmp->start < saddr) |
2332 | tmp->start = saddr; | |
2333 | if (tmp->last > eaddr) | |
2334 | tmp->last = eaddr; | |
dc54d3d1 | 2335 | |
aebc5e6f | 2336 | tmp->bo_va = NULL; |
dc54d3d1 CK |
2337 | list_add(&tmp->list, &vm->freed); |
2338 | trace_amdgpu_vm_bo_unmap(NULL, tmp); | |
2339 | } | |
2340 | ||
27f6d610 JZ |
2341 | /* Insert partial mapping before the range */ |
2342 | if (!list_empty(&before->list)) { | |
a9f87f64 | 2343 | amdgpu_vm_it_insert(before, &vm->va); |
dc54d3d1 CK |
2344 | if (before->flags & AMDGPU_PTE_PRT) |
2345 | amdgpu_vm_prt_get(adev); | |
2346 | } else { | |
2347 | kfree(before); | |
2348 | } | |
2349 | ||
2350 | /* Insert partial mapping after the range */ | |
27f6d610 | 2351 | if (!list_empty(&after->list)) { |
a9f87f64 | 2352 | amdgpu_vm_it_insert(after, &vm->va); |
dc54d3d1 CK |
2353 | if (after->flags & AMDGPU_PTE_PRT) |
2354 | amdgpu_vm_prt_get(adev); | |
2355 | } else { | |
2356 | kfree(after); | |
2357 | } | |
2358 | ||
2359 | return 0; | |
2360 | } | |
2361 | ||
aebc5e6f CK |
2362 | /** |
2363 | * amdgpu_vm_bo_lookup_mapping - find mapping by address | |
2364 | * | |
2365 | * @vm: the requested VM | |
00553cf8 | 2366 | * @addr: the address |
aebc5e6f CK |
2367 | * |
2368 | * Find a mapping by it's address. | |
7fc48e59 AG |
2369 | * |
2370 | * Returns: | |
2371 | * The amdgpu_bo_va_mapping matching for addr or NULL | |
2372 | * | |
aebc5e6f CK |
2373 | */ |
2374 | struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm, | |
2375 | uint64_t addr) | |
2376 | { | |
2377 | return amdgpu_vm_it_iter_first(&vm->va, addr, addr); | |
2378 | } | |
2379 | ||
8ab19ea6 CK |
2380 | /** |
2381 | * amdgpu_vm_bo_trace_cs - trace all reserved mappings | |
2382 | * | |
2383 | * @vm: the requested vm | |
2384 | * @ticket: CS ticket | |
2385 | * | |
2386 | * Trace all mappings of BOs reserved during a command submission. | |
2387 | */ | |
2388 | void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket) | |
2389 | { | |
2390 | struct amdgpu_bo_va_mapping *mapping; | |
2391 | ||
2392 | if (!trace_amdgpu_vm_bo_cs_enabled()) | |
2393 | return; | |
2394 | ||
2395 | for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping; | |
2396 | mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) { | |
2397 | if (mapping->bo_va && mapping->bo_va->base.bo) { | |
2398 | struct amdgpu_bo *bo; | |
2399 | ||
2400 | bo = mapping->bo_va->base.bo; | |
2401 | if (READ_ONCE(bo->tbo.resv->lock.ctx) != ticket) | |
2402 | continue; | |
2403 | } | |
2404 | ||
2405 | trace_amdgpu_vm_bo_cs(mapping); | |
2406 | } | |
2407 | } | |
2408 | ||
d38ceaf9 AD |
2409 | /** |
2410 | * amdgpu_vm_bo_rmv - remove a bo to a specific vm | |
2411 | * | |
2412 | * @adev: amdgpu_device pointer | |
2413 | * @bo_va: requested bo_va | |
2414 | * | |
8843dbbb | 2415 | * Remove @bo_va->bo from the requested vm. |
d38ceaf9 AD |
2416 | * |
2417 | * Object have to be reserved! | |
2418 | */ | |
2419 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, | |
2420 | struct amdgpu_bo_va *bo_va) | |
2421 | { | |
2422 | struct amdgpu_bo_va_mapping *mapping, *next; | |
fbbf794c | 2423 | struct amdgpu_bo *bo = bo_va->base.bo; |
ec681545 | 2424 | struct amdgpu_vm *vm = bo_va->base.vm; |
646b9025 | 2425 | struct amdgpu_vm_bo_base **base; |
d38ceaf9 | 2426 | |
646b9025 CK |
2427 | if (bo) { |
2428 | if (bo->tbo.resv == vm->root.base.bo->tbo.resv) | |
2429 | vm->bulk_moveable = false; | |
fbbf794c | 2430 | |
646b9025 CK |
2431 | for (base = &bo_va->base.bo->vm_bo; *base; |
2432 | base = &(*base)->next) { | |
2433 | if (*base != &bo_va->base) | |
2434 | continue; | |
2435 | ||
2436 | *base = bo_va->base.next; | |
2437 | break; | |
2438 | } | |
2439 | } | |
d38ceaf9 | 2440 | |
c12a2ee5 | 2441 | spin_lock(&vm->invalidated_lock); |
ec681545 | 2442 | list_del(&bo_va->base.vm_status); |
c12a2ee5 | 2443 | spin_unlock(&vm->invalidated_lock); |
d38ceaf9 | 2444 | |
7fc11959 | 2445 | list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { |
d38ceaf9 | 2446 | list_del(&mapping->list); |
a9f87f64 | 2447 | amdgpu_vm_it_remove(mapping, &vm->va); |
aebc5e6f | 2448 | mapping->bo_va = NULL; |
93e3e438 | 2449 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
7fc11959 CK |
2450 | list_add(&mapping->list, &vm->freed); |
2451 | } | |
2452 | list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { | |
2453 | list_del(&mapping->list); | |
a9f87f64 | 2454 | amdgpu_vm_it_remove(mapping, &vm->va); |
284710fa CK |
2455 | amdgpu_vm_free_mapping(adev, vm, mapping, |
2456 | bo_va->last_pt_update); | |
d38ceaf9 | 2457 | } |
32b41ac2 | 2458 | |
f54d1867 | 2459 | dma_fence_put(bo_va->last_pt_update); |
df399b06 | 2460 | |
2461 | if (bo && bo_va->is_xgmi) { | |
2462 | mutex_lock(&adev->vm_manager.lock_pstate); | |
2463 | if (--adev->vm_manager.xgmi_map_counter == 0) | |
2464 | amdgpu_xgmi_set_pstate(adev, 0); | |
2465 | mutex_unlock(&adev->vm_manager.lock_pstate); | |
2466 | } | |
2467 | ||
d38ceaf9 | 2468 | kfree(bo_va); |
d38ceaf9 AD |
2469 | } |
2470 | ||
2471 | /** | |
2472 | * amdgpu_vm_bo_invalidate - mark the bo as invalid | |
2473 | * | |
2474 | * @adev: amdgpu_device pointer | |
d38ceaf9 | 2475 | * @bo: amdgpu buffer object |
00553cf8 | 2476 | * @evicted: is the BO evicted |
d38ceaf9 | 2477 | * |
8843dbbb | 2478 | * Mark @bo as invalid. |
d38ceaf9 AD |
2479 | */ |
2480 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, | |
3f3333f8 | 2481 | struct amdgpu_bo *bo, bool evicted) |
d38ceaf9 | 2482 | { |
ec681545 CK |
2483 | struct amdgpu_vm_bo_base *bo_base; |
2484 | ||
4bebccee CZ |
2485 | /* shadow bo doesn't have bo base, its validation needs its parent */ |
2486 | if (bo->parent && bo->parent->shadow == bo) | |
2487 | bo = bo->parent; | |
2488 | ||
646b9025 | 2489 | for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { |
3f3333f8 CK |
2490 | struct amdgpu_vm *vm = bo_base->vm; |
2491 | ||
3f3333f8 | 2492 | if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) { |
bcdc9fd6 | 2493 | amdgpu_vm_bo_evicted(bo_base); |
3f3333f8 CK |
2494 | continue; |
2495 | } | |
2496 | ||
bcdc9fd6 | 2497 | if (bo_base->moved) |
3f3333f8 | 2498 | continue; |
bcdc9fd6 | 2499 | bo_base->moved = true; |
3f3333f8 | 2500 | |
bcdc9fd6 CK |
2501 | if (bo->tbo.type == ttm_bo_type_kernel) |
2502 | amdgpu_vm_bo_relocated(bo_base); | |
2503 | else if (bo->tbo.resv == vm->root.base.bo->tbo.resv) | |
2504 | amdgpu_vm_bo_moved(bo_base); | |
2505 | else | |
2506 | amdgpu_vm_bo_invalidated(bo_base); | |
d38ceaf9 AD |
2507 | } |
2508 | } | |
2509 | ||
7fc48e59 AG |
2510 | /** |
2511 | * amdgpu_vm_get_block_size - calculate VM page table size as power of two | |
2512 | * | |
2513 | * @vm_size: VM size | |
2514 | * | |
2515 | * Returns: | |
2516 | * VM page table as power of two | |
2517 | */ | |
bab4fee7 JZ |
2518 | static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) |
2519 | { | |
2520 | /* Total bits covered by PD + PTs */ | |
2521 | unsigned bits = ilog2(vm_size) + 18; | |
2522 | ||
2523 | /* Make sure the PD is 4K in size up to 8GB address space. | |
2524 | Above that split equal between PD and PTs */ | |
2525 | if (vm_size <= 8) | |
2526 | return (bits - 9); | |
2527 | else | |
2528 | return ((bits + 3) / 2); | |
2529 | } | |
2530 | ||
d07f14be RH |
2531 | /** |
2532 | * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size | |
bab4fee7 JZ |
2533 | * |
2534 | * @adev: amdgpu_device pointer | |
43370c4c | 2535 | * @min_vm_size: the minimum vm size in GB if it's set auto |
00553cf8 AG |
2536 | * @fragment_size_default: Default PTE fragment size |
2537 | * @max_level: max VMPT level | |
2538 | * @max_bits: max address space size in bits | |
2539 | * | |
bab4fee7 | 2540 | */ |
43370c4c | 2541 | void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size, |
f3368128 CK |
2542 | uint32_t fragment_size_default, unsigned max_level, |
2543 | unsigned max_bits) | |
bab4fee7 | 2544 | { |
43370c4c FK |
2545 | unsigned int max_size = 1 << (max_bits - 30); |
2546 | unsigned int vm_size; | |
36539dce CK |
2547 | uint64_t tmp; |
2548 | ||
2549 | /* adjust vm size first */ | |
f3368128 | 2550 | if (amdgpu_vm_size != -1) { |
fdd5faaa | 2551 | vm_size = amdgpu_vm_size; |
f3368128 CK |
2552 | if (vm_size > max_size) { |
2553 | dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n", | |
2554 | amdgpu_vm_size, max_size); | |
2555 | vm_size = max_size; | |
2556 | } | |
43370c4c FK |
2557 | } else { |
2558 | struct sysinfo si; | |
2559 | unsigned int phys_ram_gb; | |
2560 | ||
2561 | /* Optimal VM size depends on the amount of physical | |
2562 | * RAM available. Underlying requirements and | |
2563 | * assumptions: | |
2564 | * | |
2565 | * - Need to map system memory and VRAM from all GPUs | |
2566 | * - VRAM from other GPUs not known here | |
2567 | * - Assume VRAM <= system memory | |
2568 | * - On GFX8 and older, VM space can be segmented for | |
2569 | * different MTYPEs | |
2570 | * - Need to allow room for fragmentation, guard pages etc. | |
2571 | * | |
2572 | * This adds up to a rough guess of system memory x3. | |
2573 | * Round up to power of two to maximize the available | |
2574 | * VM size with the given page table size. | |
2575 | */ | |
2576 | si_meminfo(&si); | |
2577 | phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit + | |
2578 | (1 << 30) - 1) >> 30; | |
2579 | vm_size = roundup_pow_of_two( | |
2580 | min(max(phys_ram_gb * 3, min_vm_size), max_size)); | |
f3368128 | 2581 | } |
fdd5faaa CK |
2582 | |
2583 | adev->vm_manager.max_pfn = (uint64_t)vm_size << 18; | |
36539dce CK |
2584 | |
2585 | tmp = roundup_pow_of_two(adev->vm_manager.max_pfn); | |
97489129 CK |
2586 | if (amdgpu_vm_block_size != -1) |
2587 | tmp >>= amdgpu_vm_block_size - 9; | |
36539dce CK |
2588 | tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1; |
2589 | adev->vm_manager.num_level = min(max_level, (unsigned)tmp); | |
196f7489 CZ |
2590 | switch (adev->vm_manager.num_level) { |
2591 | case 3: | |
2592 | adev->vm_manager.root_level = AMDGPU_VM_PDB2; | |
2593 | break; | |
2594 | case 2: | |
2595 | adev->vm_manager.root_level = AMDGPU_VM_PDB1; | |
2596 | break; | |
2597 | case 1: | |
2598 | adev->vm_manager.root_level = AMDGPU_VM_PDB0; | |
2599 | break; | |
2600 | default: | |
2601 | dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n"); | |
2602 | } | |
b38f41eb | 2603 | /* block size depends on vm size and hw setup*/ |
97489129 | 2604 | if (amdgpu_vm_block_size != -1) |
bab4fee7 | 2605 | adev->vm_manager.block_size = |
97489129 CK |
2606 | min((unsigned)amdgpu_vm_block_size, max_bits |
2607 | - AMDGPU_GPU_PAGE_SHIFT | |
2608 | - 9 * adev->vm_manager.num_level); | |
2609 | else if (adev->vm_manager.num_level > 1) | |
2610 | adev->vm_manager.block_size = 9; | |
bab4fee7 | 2611 | else |
97489129 | 2612 | adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp); |
bab4fee7 | 2613 | |
b38f41eb CK |
2614 | if (amdgpu_vm_fragment_size == -1) |
2615 | adev->vm_manager.fragment_size = fragment_size_default; | |
2616 | else | |
2617 | adev->vm_manager.fragment_size = amdgpu_vm_fragment_size; | |
d07f14be | 2618 | |
36539dce CK |
2619 | DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n", |
2620 | vm_size, adev->vm_manager.num_level + 1, | |
2621 | adev->vm_manager.block_size, | |
fdd5faaa | 2622 | adev->vm_manager.fragment_size); |
bab4fee7 JZ |
2623 | } |
2624 | ||
56753e73 CK |
2625 | /** |
2626 | * amdgpu_vm_wait_idle - wait for the VM to become idle | |
2627 | * | |
2628 | * @vm: VM object to wait for | |
2629 | * @timeout: timeout to wait for VM to become idle | |
2630 | */ | |
2631 | long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout) | |
240cd9a6 | 2632 | { |
56753e73 CK |
2633 | return reservation_object_wait_timeout_rcu(vm->root.base.bo->tbo.resv, |
2634 | true, true, timeout); | |
240cd9a6 OZ |
2635 | } |
2636 | ||
d38ceaf9 AD |
2637 | /** |
2638 | * amdgpu_vm_init - initialize a vm instance | |
2639 | * | |
2640 | * @adev: amdgpu_device pointer | |
2641 | * @vm: requested vm | |
9a4b7d4c | 2642 | * @vm_context: Indicates if it GFX or Compute context |
00553cf8 | 2643 | * @pasid: Process address space identifier |
d38ceaf9 | 2644 | * |
8843dbbb | 2645 | * Init @vm fields. |
7fc48e59 AG |
2646 | * |
2647 | * Returns: | |
2648 | * 0 for success, error for failure. | |
d38ceaf9 | 2649 | */ |
9a4b7d4c | 2650 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
02208441 | 2651 | int vm_context, unsigned int pasid) |
d38ceaf9 | 2652 | { |
3216c6b7 | 2653 | struct amdgpu_bo_param bp; |
3f4299be | 2654 | struct amdgpu_bo *root; |
36bbf3bf | 2655 | int r, i; |
d38ceaf9 | 2656 | |
f808c13f | 2657 | vm->va = RB_ROOT_CACHED; |
36bbf3bf CZ |
2658 | for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) |
2659 | vm->reserved_vmid[i] = NULL; | |
3f3333f8 | 2660 | INIT_LIST_HEAD(&vm->evicted); |
ea09729c | 2661 | INIT_LIST_HEAD(&vm->relocated); |
27c7b9ae | 2662 | INIT_LIST_HEAD(&vm->moved); |
806f043f | 2663 | INIT_LIST_HEAD(&vm->idle); |
c12a2ee5 CK |
2664 | INIT_LIST_HEAD(&vm->invalidated); |
2665 | spin_lock_init(&vm->invalidated_lock); | |
d38ceaf9 | 2666 | INIT_LIST_HEAD(&vm->freed); |
20250215 | 2667 | |
2bd9ccfa | 2668 | /* create scheduler entity for page table updates */ |
3798e9a6 CK |
2669 | r = drm_sched_entity_init(&vm->entity, adev->vm_manager.vm_pte_rqs, |
2670 | adev->vm_manager.vm_pte_num_rqs, NULL); | |
2bd9ccfa | 2671 | if (r) |
f566ceb1 | 2672 | return r; |
2bd9ccfa | 2673 | |
51ac7eec YZ |
2674 | vm->pte_support_ats = false; |
2675 | ||
2676 | if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) { | |
9a4b7d4c HK |
2677 | vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & |
2678 | AMDGPU_VM_USE_CPU_FOR_COMPUTE); | |
51ac7eec | 2679 | |
741deade | 2680 | if (adev->asic_type == CHIP_RAVEN) |
51ac7eec | 2681 | vm->pte_support_ats = true; |
13307f7e | 2682 | } else { |
9a4b7d4c HK |
2683 | vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & |
2684 | AMDGPU_VM_USE_CPU_FOR_GFX); | |
13307f7e | 2685 | } |
9a4b7d4c HK |
2686 | DRM_DEBUG_DRIVER("VM update mode is %s\n", |
2687 | vm->use_cpu_for_update ? "CPU" : "SDMA"); | |
0855c9c9 | 2688 | WARN_ONCE((vm->use_cpu_for_update && !amdgpu_gmc_vram_full_visible(&adev->gmc)), |
9a4b7d4c | 2689 | "CPU update of VM recommended only for large BAR system\n"); |
6dd09027 CK |
2690 | |
2691 | if (vm->use_cpu_for_update) | |
2692 | vm->update_funcs = &amdgpu_vm_cpu_funcs; | |
2693 | else | |
2694 | vm->update_funcs = &amdgpu_vm_sdma_funcs; | |
d5884513 | 2695 | vm->last_update = NULL; |
05906dec | 2696 | |
e21eb261 | 2697 | amdgpu_vm_bo_param(adev, vm, adev->vm_manager.root_level, &bp); |
03e9dee1 FK |
2698 | if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) |
2699 | bp.flags &= ~AMDGPU_GEM_CREATE_SHADOW; | |
3f4299be | 2700 | r = amdgpu_bo_create(adev, &bp, &root); |
d38ceaf9 | 2701 | if (r) |
2bd9ccfa CK |
2702 | goto error_free_sched_entity; |
2703 | ||
3f4299be | 2704 | r = amdgpu_bo_reserve(root, true); |
d3aab672 CK |
2705 | if (r) |
2706 | goto error_free_root; | |
2707 | ||
0aa7aa24 CK |
2708 | r = reservation_object_reserve_shared(root->tbo.resv, 1); |
2709 | if (r) | |
2710 | goto error_unreserve; | |
2711 | ||
1e293037 CK |
2712 | amdgpu_vm_bo_base_init(&vm->root.base, vm, root); |
2713 | ||
780637cb | 2714 | r = amdgpu_vm_clear_bo(adev, vm, root); |
13307f7e CK |
2715 | if (r) |
2716 | goto error_unreserve; | |
2717 | ||
d3aab672 | 2718 | amdgpu_bo_unreserve(vm->root.base.bo); |
d38ceaf9 | 2719 | |
02208441 FK |
2720 | if (pasid) { |
2721 | unsigned long flags; | |
2722 | ||
2723 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); | |
2724 | r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1, | |
2725 | GFP_ATOMIC); | |
2726 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); | |
2727 | if (r < 0) | |
2728 | goto error_free_root; | |
2729 | ||
2730 | vm->pasid = pasid; | |
0a096fb6 CK |
2731 | } |
2732 | ||
a2f14820 | 2733 | INIT_KFIFO(vm->faults); |
d38ceaf9 AD |
2734 | |
2735 | return 0; | |
2bd9ccfa | 2736 | |
13307f7e CK |
2737 | error_unreserve: |
2738 | amdgpu_bo_unreserve(vm->root.base.bo); | |
2739 | ||
67003a15 | 2740 | error_free_root: |
3f3333f8 CK |
2741 | amdgpu_bo_unref(&vm->root.base.bo->shadow); |
2742 | amdgpu_bo_unref(&vm->root.base.bo); | |
2743 | vm->root.base.bo = NULL; | |
2bd9ccfa CK |
2744 | |
2745 | error_free_sched_entity: | |
cdc50176 | 2746 | drm_sched_entity_destroy(&vm->entity); |
2bd9ccfa CK |
2747 | |
2748 | return r; | |
d38ceaf9 AD |
2749 | } |
2750 | ||
b236fa1d FK |
2751 | /** |
2752 | * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM | |
2753 | * | |
7fc48e59 AG |
2754 | * @adev: amdgpu_device pointer |
2755 | * @vm: requested vm | |
2756 | * | |
b236fa1d FK |
2757 | * This only works on GFX VMs that don't have any BOs added and no |
2758 | * page tables allocated yet. | |
2759 | * | |
2760 | * Changes the following VM parameters: | |
2761 | * - use_cpu_for_update | |
2762 | * - pte_supports_ats | |
2763 | * - pasid (old PASID is released, because compute manages its own PASIDs) | |
2764 | * | |
2765 | * Reinitializes the page directory to reflect the changed ATS | |
b5d21aac | 2766 | * setting. |
b236fa1d | 2767 | * |
7fc48e59 AG |
2768 | * Returns: |
2769 | * 0 for success, -errno for errors. | |
b236fa1d | 2770 | */ |
1685b01a | 2771 | int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm, unsigned int pasid) |
b236fa1d | 2772 | { |
741deade | 2773 | bool pte_support_ats = (adev->asic_type == CHIP_RAVEN); |
b236fa1d FK |
2774 | int r; |
2775 | ||
2776 | r = amdgpu_bo_reserve(vm->root.base.bo, true); | |
2777 | if (r) | |
2778 | return r; | |
2779 | ||
2780 | /* Sanity checks */ | |
2781 | if (!RB_EMPTY_ROOT(&vm->va.rb_root) || vm->root.entries) { | |
2782 | r = -EINVAL; | |
1685b01a OZ |
2783 | goto unreserve_bo; |
2784 | } | |
2785 | ||
2786 | if (pasid) { | |
2787 | unsigned long flags; | |
2788 | ||
2789 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); | |
2790 | r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1, | |
2791 | GFP_ATOMIC); | |
2792 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); | |
2793 | ||
2794 | if (r == -ENOSPC) | |
2795 | goto unreserve_bo; | |
2796 | r = 0; | |
b236fa1d FK |
2797 | } |
2798 | ||
2799 | /* Check if PD needs to be reinitialized and do it before | |
2800 | * changing any other state, in case it fails. | |
2801 | */ | |
2802 | if (pte_support_ats != vm->pte_support_ats) { | |
780637cb CK |
2803 | vm->pte_support_ats = pte_support_ats; |
2804 | r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo); | |
b236fa1d | 2805 | if (r) |
1685b01a | 2806 | goto free_idr; |
b236fa1d FK |
2807 | } |
2808 | ||
2809 | /* Update VM state */ | |
2810 | vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & | |
2811 | AMDGPU_VM_USE_CPU_FOR_COMPUTE); | |
b236fa1d FK |
2812 | DRM_DEBUG_DRIVER("VM update mode is %s\n", |
2813 | vm->use_cpu_for_update ? "CPU" : "SDMA"); | |
0855c9c9 | 2814 | WARN_ONCE((vm->use_cpu_for_update && !amdgpu_gmc_vram_full_visible(&adev->gmc)), |
b236fa1d FK |
2815 | "CPU update of VM recommended only for large BAR system\n"); |
2816 | ||
2817 | if (vm->pasid) { | |
2818 | unsigned long flags; | |
2819 | ||
2820 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); | |
2821 | idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); | |
2822 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); | |
2823 | ||
1685b01a OZ |
2824 | /* Free the original amdgpu allocated pasid |
2825 | * Will be replaced with kfd allocated pasid | |
2826 | */ | |
2827 | amdgpu_pasid_free(vm->pasid); | |
b236fa1d FK |
2828 | vm->pasid = 0; |
2829 | } | |
2830 | ||
b5d21aac SL |
2831 | /* Free the shadow bo for compute VM */ |
2832 | amdgpu_bo_unref(&vm->root.base.bo->shadow); | |
2833 | ||
1685b01a OZ |
2834 | if (pasid) |
2835 | vm->pasid = pasid; | |
2836 | ||
2837 | goto unreserve_bo; | |
2838 | ||
2839 | free_idr: | |
2840 | if (pasid) { | |
2841 | unsigned long flags; | |
2842 | ||
2843 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); | |
2844 | idr_remove(&adev->vm_manager.pasid_idr, pasid); | |
2845 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); | |
2846 | } | |
2847 | unreserve_bo: | |
b236fa1d FK |
2848 | amdgpu_bo_unreserve(vm->root.base.bo); |
2849 | return r; | |
2850 | } | |
2851 | ||
bf47afba OZ |
2852 | /** |
2853 | * amdgpu_vm_release_compute - release a compute vm | |
2854 | * @adev: amdgpu_device pointer | |
2855 | * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute | |
2856 | * | |
2857 | * This is a correspondant of amdgpu_vm_make_compute. It decouples compute | |
2858 | * pasid from vm. Compute should stop use of vm after this call. | |
2859 | */ | |
2860 | void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
2861 | { | |
2862 | if (vm->pasid) { | |
2863 | unsigned long flags; | |
2864 | ||
2865 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); | |
2866 | idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); | |
2867 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); | |
2868 | } | |
2869 | vm->pasid = 0; | |
2870 | } | |
2871 | ||
d38ceaf9 AD |
2872 | /** |
2873 | * amdgpu_vm_fini - tear down a vm instance | |
2874 | * | |
2875 | * @adev: amdgpu_device pointer | |
2876 | * @vm: requested vm | |
2877 | * | |
8843dbbb | 2878 | * Tear down @vm. |
d38ceaf9 AD |
2879 | * Unbind the VM and remove all bos from the vm bo list |
2880 | */ | |
2881 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
2882 | { | |
2883 | struct amdgpu_bo_va_mapping *mapping, *tmp; | |
132f34e4 | 2884 | bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt; |
2642cf11 | 2885 | struct amdgpu_bo *root; |
2642cf11 | 2886 | int i, r; |
d38ceaf9 | 2887 | |
ede0dd86 FK |
2888 | amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm); |
2889 | ||
02208441 FK |
2890 | if (vm->pasid) { |
2891 | unsigned long flags; | |
2892 | ||
2893 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); | |
2894 | idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); | |
2895 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); | |
2896 | } | |
2897 | ||
cdc50176 | 2898 | drm_sched_entity_destroy(&vm->entity); |
2bd9ccfa | 2899 | |
f808c13f | 2900 | if (!RB_EMPTY_ROOT(&vm->va.rb_root)) { |
d38ceaf9 AD |
2901 | dev_err(adev->dev, "still active bo inside vm\n"); |
2902 | } | |
f808c13f DB |
2903 | rbtree_postorder_for_each_entry_safe(mapping, tmp, |
2904 | &vm->va.rb_root, rb) { | |
0af5c656 CK |
2905 | /* Don't remove the mapping here, we don't want to trigger a |
2906 | * rebalance and the tree is about to be destroyed anyway. | |
2907 | */ | |
d38ceaf9 | 2908 | list_del(&mapping->list); |
d38ceaf9 AD |
2909 | kfree(mapping); |
2910 | } | |
2911 | list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { | |
4388fc2a | 2912 | if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { |
451bc8eb | 2913 | amdgpu_vm_prt_fini(adev, vm); |
4388fc2a | 2914 | prt_fini_needed = false; |
451bc8eb | 2915 | } |
284710fa | 2916 | |
d38ceaf9 | 2917 | list_del(&mapping->list); |
451bc8eb | 2918 | amdgpu_vm_free_mapping(adev, vm, mapping, NULL); |
d38ceaf9 AD |
2919 | } |
2920 | ||
2642cf11 CK |
2921 | root = amdgpu_bo_ref(vm->root.base.bo); |
2922 | r = amdgpu_bo_reserve(root, true); | |
2923 | if (r) { | |
2924 | dev_err(adev->dev, "Leaking page tables because BO reservation failed\n"); | |
2925 | } else { | |
e35fb064 | 2926 | amdgpu_vm_free_pts(adev, vm, NULL); |
2642cf11 CK |
2927 | amdgpu_bo_unreserve(root); |
2928 | } | |
2929 | amdgpu_bo_unref(&root); | |
e35fb064 | 2930 | WARN_ON(vm->root.base.bo); |
d5884513 | 2931 | dma_fence_put(vm->last_update); |
1e9ef26f | 2932 | for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) |
620f774f | 2933 | amdgpu_vmid_free_reserved(adev, vm, i); |
d38ceaf9 | 2934 | } |
ea89f8c9 | 2935 | |
a9a78b32 CK |
2936 | /** |
2937 | * amdgpu_vm_manager_init - init the VM manager | |
2938 | * | |
2939 | * @adev: amdgpu_device pointer | |
2940 | * | |
2941 | * Initialize the VM manager structures | |
2942 | */ | |
2943 | void amdgpu_vm_manager_init(struct amdgpu_device *adev) | |
2944 | { | |
620f774f | 2945 | unsigned i; |
a9a78b32 | 2946 | |
620f774f | 2947 | amdgpu_vmid_mgr_init(adev); |
2d55e45a | 2948 | |
f54d1867 CW |
2949 | adev->vm_manager.fence_context = |
2950 | dma_fence_context_alloc(AMDGPU_MAX_RINGS); | |
1fbb2e92 CK |
2951 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
2952 | adev->vm_manager.seqno[i] = 0; | |
2953 | ||
284710fa | 2954 | spin_lock_init(&adev->vm_manager.prt_lock); |
451bc8eb | 2955 | atomic_set(&adev->vm_manager.num_prt_users, 0); |
9a4b7d4c HK |
2956 | |
2957 | /* If not overridden by the user, by default, only in large BAR systems | |
2958 | * Compute VM tables will be updated by CPU | |
2959 | */ | |
2960 | #ifdef CONFIG_X86_64 | |
2961 | if (amdgpu_vm_update_mode == -1) { | |
c8c5e569 | 2962 | if (amdgpu_gmc_vram_full_visible(&adev->gmc)) |
9a4b7d4c HK |
2963 | adev->vm_manager.vm_update_mode = |
2964 | AMDGPU_VM_USE_CPU_FOR_COMPUTE; | |
2965 | else | |
2966 | adev->vm_manager.vm_update_mode = 0; | |
2967 | } else | |
2968 | adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode; | |
2969 | #else | |
2970 | adev->vm_manager.vm_update_mode = 0; | |
2971 | #endif | |
2972 | ||
02208441 FK |
2973 | idr_init(&adev->vm_manager.pasid_idr); |
2974 | spin_lock_init(&adev->vm_manager.pasid_lock); | |
df399b06 | 2975 | |
2976 | adev->vm_manager.xgmi_map_counter = 0; | |
2977 | mutex_init(&adev->vm_manager.lock_pstate); | |
a9a78b32 CK |
2978 | } |
2979 | ||
ea89f8c9 CK |
2980 | /** |
2981 | * amdgpu_vm_manager_fini - cleanup VM manager | |
2982 | * | |
2983 | * @adev: amdgpu_device pointer | |
2984 | * | |
2985 | * Cleanup the VM manager and free resources. | |
2986 | */ | |
2987 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev) | |
2988 | { | |
02208441 FK |
2989 | WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr)); |
2990 | idr_destroy(&adev->vm_manager.pasid_idr); | |
2991 | ||
620f774f | 2992 | amdgpu_vmid_mgr_fini(adev); |
ea89f8c9 | 2993 | } |
cfbcacf4 | 2994 | |
7fc48e59 AG |
2995 | /** |
2996 | * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs. | |
2997 | * | |
2998 | * @dev: drm device pointer | |
2999 | * @data: drm_amdgpu_vm | |
3000 | * @filp: drm file pointer | |
3001 | * | |
3002 | * Returns: | |
3003 | * 0 for success, -errno for errors. | |
3004 | */ | |
cfbcacf4 CZ |
3005 | int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) |
3006 | { | |
3007 | union drm_amdgpu_vm *args = data; | |
1e9ef26f CZ |
3008 | struct amdgpu_device *adev = dev->dev_private; |
3009 | struct amdgpu_fpriv *fpriv = filp->driver_priv; | |
3010 | int r; | |
cfbcacf4 CZ |
3011 | |
3012 | switch (args->in.op) { | |
3013 | case AMDGPU_VM_OP_RESERVE_VMID: | |
1e9ef26f | 3014 | /* current, we only have requirement to reserve vmid from gfxhub */ |
620f774f | 3015 | r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB); |
1e9ef26f CZ |
3016 | if (r) |
3017 | return r; | |
3018 | break; | |
cfbcacf4 | 3019 | case AMDGPU_VM_OP_UNRESERVE_VMID: |
620f774f | 3020 | amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB); |
cfbcacf4 CZ |
3021 | break; |
3022 | default: | |
3023 | return -EINVAL; | |
3024 | } | |
3025 | ||
3026 | return 0; | |
3027 | } | |
2aa37bf5 AG |
3028 | |
3029 | /** | |
3030 | * amdgpu_vm_get_task_info - Extracts task info for a PASID. | |
3031 | * | |
989edc69 | 3032 | * @adev: drm device pointer |
2aa37bf5 AG |
3033 | * @pasid: PASID identifier for VM |
3034 | * @task_info: task_info to fill. | |
3035 | */ | |
3036 | void amdgpu_vm_get_task_info(struct amdgpu_device *adev, unsigned int pasid, | |
3037 | struct amdgpu_task_info *task_info) | |
3038 | { | |
3039 | struct amdgpu_vm *vm; | |
0a5f49cb | 3040 | unsigned long flags; |
2aa37bf5 | 3041 | |
0a5f49cb | 3042 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); |
2aa37bf5 AG |
3043 | |
3044 | vm = idr_find(&adev->vm_manager.pasid_idr, pasid); | |
3045 | if (vm) | |
3046 | *task_info = vm->task_info; | |
3047 | ||
0a5f49cb | 3048 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); |
2aa37bf5 AG |
3049 | } |
3050 | ||
3051 | /** | |
3052 | * amdgpu_vm_set_task_info - Sets VMs task info. | |
3053 | * | |
3054 | * @vm: vm for which to set the info | |
3055 | */ | |
3056 | void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) | |
3057 | { | |
3058 | if (!vm->task_info.pid) { | |
3059 | vm->task_info.pid = current->pid; | |
3060 | get_task_comm(vm->task_info.task_name, current); | |
3061 | ||
3062 | if (current->group_leader->mm == current->mm) { | |
3063 | vm->task_info.tgid = current->group_leader->pid; | |
3064 | get_task_comm(vm->task_info.process_name, current->group_leader); | |
3065 | } | |
3066 | } | |
3067 | } |