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