]>
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" | |
35 | ||
36 | /* | |
37 | * GPUVM | |
38 | * GPUVM is similar to the legacy gart on older asics, however | |
39 | * rather than there being a single global gart table | |
40 | * for the entire GPU, there are multiple VM page tables active | |
41 | * at any given time. The VM page tables can contain a mix | |
42 | * vram pages and system memory pages and system memory pages | |
43 | * can be mapped as snooped (cached system pages) or unsnooped | |
44 | * (uncached system pages). | |
45 | * Each VM has an ID associated with it and there is a page table | |
46 | * associated with each VMID. When execting a command buffer, | |
47 | * the kernel tells the the ring what VMID to use for that command | |
48 | * buffer. VMIDs are allocated dynamically as commands are submitted. | |
49 | * The userspace drivers maintain their own address space and the kernel | |
50 | * sets up their pages tables accordingly when they submit their | |
51 | * command buffers and a VMID is assigned. | |
52 | * Cayman/Trinity support up to 8 active VMs at any given time; | |
53 | * SI supports 16. | |
54 | */ | |
55 | ||
a9f87f64 CK |
56 | #define START(node) ((node)->start) |
57 | #define LAST(node) ((node)->last) | |
58 | ||
59 | INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last, | |
60 | START, LAST, static, amdgpu_vm_it) | |
61 | ||
62 | #undef START | |
63 | #undef LAST | |
64 | ||
f4833c4f HK |
65 | /* Local structure. Encapsulate some VM table update parameters to reduce |
66 | * the number of function parameters | |
67 | */ | |
29efc4f5 | 68 | struct amdgpu_pte_update_params { |
27c5f36f CK |
69 | /* amdgpu device we do this update for */ |
70 | struct amdgpu_device *adev; | |
49ac8a24 CK |
71 | /* optional amdgpu_vm we do this update for */ |
72 | struct amdgpu_vm *vm; | |
f4833c4f HK |
73 | /* address where to copy page table entries from */ |
74 | uint64_t src; | |
f4833c4f HK |
75 | /* indirect buffer to fill with commands */ |
76 | struct amdgpu_ib *ib; | |
afef8b8f | 77 | /* Function which actually does the update */ |
373ac645 CK |
78 | void (*func)(struct amdgpu_pte_update_params *params, |
79 | struct amdgpu_bo *bo, uint64_t pe, | |
afef8b8f | 80 | uint64_t addr, unsigned count, uint32_t incr, |
6b777607 | 81 | uint64_t flags); |
b4d42511 HK |
82 | /* The next two are used during VM update by CPU |
83 | * DMA addresses to use for mapping | |
84 | * Kernel pointer of PD/PT BO that needs to be updated | |
85 | */ | |
86 | dma_addr_t *pages_addr; | |
87 | void *kptr; | |
f4833c4f HK |
88 | }; |
89 | ||
284710fa CK |
90 | /* Helper to disable partial resident texture feature from a fence callback */ |
91 | struct amdgpu_prt_cb { | |
92 | struct amdgpu_device *adev; | |
93 | struct dma_fence_cb cb; | |
94 | }; | |
95 | ||
50783147 CK |
96 | /** |
97 | * amdgpu_vm_level_shift - return the addr shift for each level | |
98 | * | |
99 | * @adev: amdgpu_device pointer | |
100 | * | |
101 | * Returns the number of bits the pfn needs to be right shifted for a level. | |
102 | */ | |
103 | static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev, | |
104 | unsigned level) | |
105 | { | |
196f7489 CZ |
106 | unsigned shift = 0xff; |
107 | ||
108 | switch (level) { | |
109 | case AMDGPU_VM_PDB2: | |
110 | case AMDGPU_VM_PDB1: | |
111 | case AMDGPU_VM_PDB0: | |
112 | shift = 9 * (AMDGPU_VM_PDB0 - level) + | |
50783147 | 113 | adev->vm_manager.block_size; |
196f7489 CZ |
114 | break; |
115 | case AMDGPU_VM_PTB: | |
116 | shift = 0; | |
117 | break; | |
118 | default: | |
119 | dev_err(adev->dev, "the level%d isn't supported.\n", level); | |
120 | } | |
121 | ||
122 | return shift; | |
50783147 CK |
123 | } |
124 | ||
d38ceaf9 | 125 | /** |
72a7ec5c | 126 | * amdgpu_vm_num_entries - return the number of entries in a PD/PT |
d38ceaf9 AD |
127 | * |
128 | * @adev: amdgpu_device pointer | |
129 | * | |
72a7ec5c | 130 | * Calculate the number of entries in a page directory or page table. |
d38ceaf9 | 131 | */ |
72a7ec5c CK |
132 | static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev, |
133 | unsigned level) | |
d38ceaf9 | 134 | { |
196f7489 CZ |
135 | unsigned shift = amdgpu_vm_level_shift(adev, |
136 | adev->vm_manager.root_level); | |
0410c5e5 | 137 | |
196f7489 | 138 | if (level == adev->vm_manager.root_level) |
72a7ec5c | 139 | /* For the root directory */ |
0410c5e5 | 140 | return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift; |
196f7489 | 141 | else if (level != AMDGPU_VM_PTB) |
0410c5e5 CK |
142 | /* Everything in between */ |
143 | return 512; | |
144 | else | |
72a7ec5c | 145 | /* For the page tables on the leaves */ |
36b32a68 | 146 | return AMDGPU_VM_PTE_COUNT(adev); |
d38ceaf9 AD |
147 | } |
148 | ||
149 | /** | |
72a7ec5c | 150 | * amdgpu_vm_bo_size - returns the size of the BOs in bytes |
d38ceaf9 AD |
151 | * |
152 | * @adev: amdgpu_device pointer | |
153 | * | |
72a7ec5c | 154 | * Calculate the size of the BO for a page directory or page table in bytes. |
d38ceaf9 | 155 | */ |
72a7ec5c | 156 | static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level) |
d38ceaf9 | 157 | { |
72a7ec5c | 158 | return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8); |
d38ceaf9 AD |
159 | } |
160 | ||
161 | /** | |
56467ebf | 162 | * amdgpu_vm_get_pd_bo - add the VM PD to a validation list |
d38ceaf9 AD |
163 | * |
164 | * @vm: vm providing the BOs | |
3c0eea6c | 165 | * @validated: head of validation list |
56467ebf | 166 | * @entry: entry to add |
d38ceaf9 AD |
167 | * |
168 | * Add the page directory to the list of BOs to | |
56467ebf | 169 | * validate for command submission. |
d38ceaf9 | 170 | */ |
56467ebf CK |
171 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
172 | struct list_head *validated, | |
173 | struct amdgpu_bo_list_entry *entry) | |
d38ceaf9 | 174 | { |
3f3333f8 | 175 | entry->robj = vm->root.base.bo; |
56467ebf | 176 | entry->priority = 0; |
67003a15 | 177 | entry->tv.bo = &entry->robj->tbo; |
56467ebf | 178 | entry->tv.shared = true; |
2f568dbd | 179 | entry->user_pages = NULL; |
56467ebf CK |
180 | list_add(&entry->tv.head, validated); |
181 | } | |
d38ceaf9 | 182 | |
670fecc8 | 183 | /** |
f7da30d9 | 184 | * amdgpu_vm_validate_pt_bos - validate the page table BOs |
670fecc8 | 185 | * |
5a712a87 | 186 | * @adev: amdgpu device pointer |
56467ebf | 187 | * @vm: vm providing the BOs |
670fecc8 CK |
188 | * @validate: callback to do the validation |
189 | * @param: parameter for the validation callback | |
190 | * | |
191 | * Validate the page table BOs on command submission if neccessary. | |
192 | */ | |
f7da30d9 CK |
193 | int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
194 | int (*validate)(void *p, struct amdgpu_bo *bo), | |
195 | void *param) | |
670fecc8 | 196 | { |
3f3333f8 | 197 | struct ttm_bo_global *glob = adev->mman.bdev.glob; |
670fecc8 CK |
198 | int r; |
199 | ||
3f3333f8 CK |
200 | spin_lock(&vm->status_lock); |
201 | while (!list_empty(&vm->evicted)) { | |
202 | struct amdgpu_vm_bo_base *bo_base; | |
203 | struct amdgpu_bo *bo; | |
670fecc8 | 204 | |
3f3333f8 CK |
205 | bo_base = list_first_entry(&vm->evicted, |
206 | struct amdgpu_vm_bo_base, | |
207 | vm_status); | |
208 | spin_unlock(&vm->status_lock); | |
670fecc8 | 209 | |
3f3333f8 CK |
210 | bo = bo_base->bo; |
211 | BUG_ON(!bo); | |
212 | if (bo->parent) { | |
213 | r = validate(param, bo); | |
214 | if (r) | |
215 | return r; | |
670fecc8 | 216 | |
3f3333f8 CK |
217 | spin_lock(&glob->lru_lock); |
218 | ttm_bo_move_to_lru_tail(&bo->tbo); | |
219 | if (bo->shadow) | |
220 | ttm_bo_move_to_lru_tail(&bo->shadow->tbo); | |
221 | spin_unlock(&glob->lru_lock); | |
222 | } | |
670fecc8 | 223 | |
73fb16e7 CK |
224 | if (bo->tbo.type == ttm_bo_type_kernel && |
225 | vm->use_cpu_for_update) { | |
3f3333f8 CK |
226 | r = amdgpu_bo_kmap(bo, NULL); |
227 | if (r) | |
228 | return r; | |
229 | } | |
b6369225 | 230 | |
3f3333f8 | 231 | spin_lock(&vm->status_lock); |
73fb16e7 CK |
232 | if (bo->tbo.type != ttm_bo_type_kernel) |
233 | list_move(&bo_base->vm_status, &vm->moved); | |
234 | else | |
235 | list_move(&bo_base->vm_status, &vm->relocated); | |
670fecc8 | 236 | } |
3f3333f8 | 237 | spin_unlock(&vm->status_lock); |
670fecc8 | 238 | |
34d7be5d | 239 | return 0; |
670fecc8 CK |
240 | } |
241 | ||
56467ebf | 242 | /** |
34d7be5d | 243 | * amdgpu_vm_ready - check VM is ready for updates |
56467ebf | 244 | * |
34d7be5d | 245 | * @vm: VM to check |
d38ceaf9 | 246 | * |
34d7be5d | 247 | * Check if all VM PDs/PTs are ready for updates |
d38ceaf9 | 248 | */ |
3f3333f8 | 249 | bool amdgpu_vm_ready(struct amdgpu_vm *vm) |
d38ceaf9 | 250 | { |
3f3333f8 | 251 | bool ready; |
d38ceaf9 | 252 | |
3f3333f8 CK |
253 | spin_lock(&vm->status_lock); |
254 | ready = list_empty(&vm->evicted); | |
255 | spin_unlock(&vm->status_lock); | |
5a712a87 | 256 | |
3f3333f8 | 257 | return ready; |
d711e139 CK |
258 | } |
259 | ||
13307f7e CK |
260 | /** |
261 | * amdgpu_vm_clear_bo - initially clear the PDs/PTs | |
262 | * | |
263 | * @adev: amdgpu_device pointer | |
264 | * @bo: BO to clear | |
265 | * @level: level this BO is at | |
266 | * | |
267 | * Root PD needs to be reserved when calling this. | |
268 | */ | |
269 | static int amdgpu_vm_clear_bo(struct amdgpu_device *adev, | |
4584312d CK |
270 | struct amdgpu_vm *vm, struct amdgpu_bo *bo, |
271 | unsigned level, bool pte_support_ats) | |
13307f7e CK |
272 | { |
273 | struct ttm_operation_ctx ctx = { true, false }; | |
274 | struct dma_fence *fence = NULL; | |
4584312d | 275 | unsigned entries, ats_entries; |
13307f7e CK |
276 | struct amdgpu_ring *ring; |
277 | struct amdgpu_job *job; | |
4584312d | 278 | uint64_t addr; |
13307f7e CK |
279 | int r; |
280 | ||
4584312d CK |
281 | addr = amdgpu_bo_gpu_offset(bo); |
282 | entries = amdgpu_bo_size(bo) / 8; | |
283 | ||
284 | if (pte_support_ats) { | |
285 | if (level == adev->vm_manager.root_level) { | |
286 | ats_entries = amdgpu_vm_level_shift(adev, level); | |
287 | ats_entries += AMDGPU_GPU_PAGE_SHIFT; | |
288 | ats_entries = AMDGPU_VA_HOLE_START >> ats_entries; | |
289 | ats_entries = min(ats_entries, entries); | |
290 | entries -= ats_entries; | |
291 | } else { | |
292 | ats_entries = entries; | |
293 | entries = 0; | |
294 | } | |
13307f7e | 295 | } else { |
4584312d | 296 | ats_entries = 0; |
13307f7e CK |
297 | } |
298 | ||
299 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); | |
300 | ||
301 | r = reservation_object_reserve_shared(bo->tbo.resv); | |
302 | if (r) | |
303 | return r; | |
304 | ||
305 | r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); | |
306 | if (r) | |
307 | goto error; | |
308 | ||
13307f7e CK |
309 | r = amdgpu_job_alloc_with_ib(adev, 64, &job); |
310 | if (r) | |
311 | goto error; | |
312 | ||
4584312d CK |
313 | if (ats_entries) { |
314 | uint64_t ats_value; | |
315 | ||
316 | ats_value = AMDGPU_PTE_DEFAULT_ATC; | |
317 | if (level != AMDGPU_VM_PTB) | |
318 | ats_value |= AMDGPU_PDE_PTE; | |
319 | ||
320 | amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0, | |
321 | ats_entries, 0, ats_value); | |
322 | addr += ats_entries * 8; | |
323 | } | |
324 | ||
325 | if (entries) | |
326 | amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0, | |
327 | entries, 0, 0); | |
328 | ||
13307f7e CK |
329 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); |
330 | ||
331 | WARN_ON(job->ibs[0].length_dw > 64); | |
29e8357b CK |
332 | r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv, |
333 | AMDGPU_FENCE_OWNER_UNDEFINED, false); | |
334 | if (r) | |
335 | goto error_free; | |
336 | ||
13307f7e CK |
337 | r = amdgpu_job_submit(job, ring, &vm->entity, |
338 | AMDGPU_FENCE_OWNER_UNDEFINED, &fence); | |
339 | if (r) | |
340 | goto error_free; | |
341 | ||
342 | amdgpu_bo_fence(bo, fence, true); | |
343 | dma_fence_put(fence); | |
e61736da CK |
344 | |
345 | if (bo->shadow) | |
346 | return amdgpu_vm_clear_bo(adev, vm, bo->shadow, | |
347 | level, pte_support_ats); | |
348 | ||
13307f7e CK |
349 | return 0; |
350 | ||
351 | error_free: | |
352 | amdgpu_job_free(job); | |
353 | ||
354 | error: | |
355 | return r; | |
356 | } | |
357 | ||
d711e139 | 358 | /** |
f566ceb1 CK |
359 | * amdgpu_vm_alloc_levels - allocate the PD/PT levels |
360 | * | |
361 | * @adev: amdgpu_device pointer | |
362 | * @vm: requested vm | |
363 | * @saddr: start of the address range | |
364 | * @eaddr: end of the address range | |
365 | * | |
366 | * Make sure the page directories and page tables are allocated | |
367 | */ | |
368 | static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev, | |
369 | struct amdgpu_vm *vm, | |
370 | struct amdgpu_vm_pt *parent, | |
371 | uint64_t saddr, uint64_t eaddr, | |
4584312d | 372 | unsigned level, bool ats) |
f566ceb1 | 373 | { |
50783147 | 374 | unsigned shift = amdgpu_vm_level_shift(adev, level); |
f566ceb1 | 375 | unsigned pt_idx, from, to; |
3c824172 | 376 | u64 flags; |
13307f7e | 377 | int r; |
f566ceb1 CK |
378 | |
379 | if (!parent->entries) { | |
380 | unsigned num_entries = amdgpu_vm_num_entries(adev, level); | |
381 | ||
2098105e MH |
382 | parent->entries = kvmalloc_array(num_entries, |
383 | sizeof(struct amdgpu_vm_pt), | |
384 | GFP_KERNEL | __GFP_ZERO); | |
f566ceb1 CK |
385 | if (!parent->entries) |
386 | return -ENOMEM; | |
387 | memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt)); | |
388 | } | |
389 | ||
1866bac8 FK |
390 | from = saddr >> shift; |
391 | to = eaddr >> shift; | |
392 | if (from >= amdgpu_vm_num_entries(adev, level) || | |
393 | to >= amdgpu_vm_num_entries(adev, level)) | |
394 | return -EINVAL; | |
f566ceb1 | 395 | |
f566ceb1 | 396 | ++level; |
1866bac8 FK |
397 | saddr = saddr & ((1 << shift) - 1); |
398 | eaddr = eaddr & ((1 << shift) - 1); | |
f566ceb1 | 399 | |
13307f7e | 400 | flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; |
3c824172 HK |
401 | if (vm->use_cpu_for_update) |
402 | flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; | |
403 | else | |
404 | flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS | | |
405 | AMDGPU_GEM_CREATE_SHADOW); | |
406 | ||
f566ceb1 CK |
407 | /* walk over the address space and allocate the page tables */ |
408 | for (pt_idx = from; pt_idx <= to; ++pt_idx) { | |
3f3333f8 | 409 | struct reservation_object *resv = vm->root.base.bo->tbo.resv; |
f566ceb1 CK |
410 | struct amdgpu_vm_pt *entry = &parent->entries[pt_idx]; |
411 | struct amdgpu_bo *pt; | |
412 | ||
3f3333f8 | 413 | if (!entry->base.bo) { |
f566ceb1 CK |
414 | r = amdgpu_bo_create(adev, |
415 | amdgpu_vm_bo_size(adev, level), | |
416 | AMDGPU_GPU_PAGE_SIZE, true, | |
13307f7e | 417 | AMDGPU_GEM_DOMAIN_VRAM, flags, |
8febe617 | 418 | NULL, resv, &pt); |
f566ceb1 CK |
419 | if (r) |
420 | return r; | |
421 | ||
4584312d | 422 | r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats); |
13307f7e | 423 | if (r) { |
e5197a4c | 424 | amdgpu_bo_unref(&pt->shadow); |
13307f7e CK |
425 | amdgpu_bo_unref(&pt); |
426 | return r; | |
427 | } | |
428 | ||
0a096fb6 CK |
429 | if (vm->use_cpu_for_update) { |
430 | r = amdgpu_bo_kmap(pt, NULL); | |
431 | if (r) { | |
e5197a4c | 432 | amdgpu_bo_unref(&pt->shadow); |
0a096fb6 CK |
433 | amdgpu_bo_unref(&pt); |
434 | return r; | |
435 | } | |
436 | } | |
437 | ||
f566ceb1 CK |
438 | /* Keep a reference to the root directory to avoid |
439 | * freeing them up in the wrong order. | |
440 | */ | |
0f2fc435 | 441 | pt->parent = amdgpu_bo_ref(parent->base.bo); |
f566ceb1 | 442 | |
3f3333f8 CK |
443 | entry->base.vm = vm; |
444 | entry->base.bo = pt; | |
445 | list_add_tail(&entry->base.bo_list, &pt->va); | |
ea09729c CK |
446 | spin_lock(&vm->status_lock); |
447 | list_add(&entry->base.vm_status, &vm->relocated); | |
448 | spin_unlock(&vm->status_lock); | |
f566ceb1 CK |
449 | } |
450 | ||
196f7489 | 451 | if (level < AMDGPU_VM_PTB) { |
1866bac8 FK |
452 | uint64_t sub_saddr = (pt_idx == from) ? saddr : 0; |
453 | uint64_t sub_eaddr = (pt_idx == to) ? eaddr : | |
454 | ((1 << shift) - 1); | |
455 | r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr, | |
4584312d | 456 | sub_eaddr, level, ats); |
f566ceb1 CK |
457 | if (r) |
458 | return r; | |
459 | } | |
460 | } | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
663e4577 CK |
465 | /** |
466 | * amdgpu_vm_alloc_pts - Allocate page tables. | |
467 | * | |
468 | * @adev: amdgpu_device pointer | |
469 | * @vm: VM to allocate page tables for | |
470 | * @saddr: Start address which needs to be allocated | |
471 | * @size: Size from start address we need. | |
472 | * | |
473 | * Make sure the page tables are allocated. | |
474 | */ | |
475 | int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, | |
476 | struct amdgpu_vm *vm, | |
477 | uint64_t saddr, uint64_t size) | |
478 | { | |
663e4577 | 479 | uint64_t eaddr; |
4584312d | 480 | bool ats = false; |
663e4577 CK |
481 | |
482 | /* validate the parameters */ | |
483 | if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK) | |
484 | return -EINVAL; | |
485 | ||
486 | eaddr = saddr + size - 1; | |
4584312d CK |
487 | |
488 | if (vm->pte_support_ats) | |
489 | ats = saddr < AMDGPU_VA_HOLE_START; | |
663e4577 CK |
490 | |
491 | saddr /= AMDGPU_GPU_PAGE_SIZE; | |
492 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
493 | ||
4584312d CK |
494 | if (eaddr >= adev->vm_manager.max_pfn) { |
495 | dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n", | |
496 | eaddr, adev->vm_manager.max_pfn); | |
497 | return -EINVAL; | |
498 | } | |
499 | ||
196f7489 | 500 | return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, |
4584312d | 501 | adev->vm_manager.root_level, ats); |
663e4577 CK |
502 | } |
503 | ||
e59c0205 AX |
504 | /** |
505 | * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug | |
506 | * | |
507 | * @adev: amdgpu_device pointer | |
508 | */ | |
509 | void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev) | |
93dcc37d | 510 | { |
a1255107 | 511 | const struct amdgpu_ip_block *ip_block; |
e59c0205 AX |
512 | bool has_compute_vm_bug; |
513 | struct amdgpu_ring *ring; | |
514 | int i; | |
93dcc37d | 515 | |
e59c0205 | 516 | has_compute_vm_bug = false; |
93dcc37d | 517 | |
2990a1fc | 518 | ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); |
e59c0205 AX |
519 | if (ip_block) { |
520 | /* Compute has a VM bug for GFX version < 7. | |
521 | Compute has a VM bug for GFX 8 MEC firmware version < 673.*/ | |
522 | if (ip_block->version->major <= 7) | |
523 | has_compute_vm_bug = true; | |
524 | else if (ip_block->version->major == 8) | |
525 | if (adev->gfx.mec_fw_version < 673) | |
526 | has_compute_vm_bug = true; | |
527 | } | |
93dcc37d | 528 | |
e59c0205 AX |
529 | for (i = 0; i < adev->num_rings; i++) { |
530 | ring = adev->rings[i]; | |
531 | if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) | |
532 | /* only compute rings */ | |
533 | ring->has_compute_vm_bug = has_compute_vm_bug; | |
93dcc37d | 534 | else |
e59c0205 | 535 | ring->has_compute_vm_bug = false; |
93dcc37d | 536 | } |
93dcc37d AD |
537 | } |
538 | ||
b9bf33d5 CZ |
539 | bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring, |
540 | struct amdgpu_job *job) | |
e60f8db5 | 541 | { |
b9bf33d5 CZ |
542 | struct amdgpu_device *adev = ring->adev; |
543 | unsigned vmhub = ring->funcs->vmhub; | |
620f774f CK |
544 | struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; |
545 | struct amdgpu_vmid *id; | |
b9bf33d5 | 546 | bool gds_switch_needed; |
e59c0205 | 547 | bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug; |
b9bf33d5 | 548 | |
c4f46f22 | 549 | if (job->vmid == 0) |
b9bf33d5 | 550 | return false; |
c4f46f22 | 551 | id = &id_mgr->ids[job->vmid]; |
b9bf33d5 CZ |
552 | gds_switch_needed = ring->funcs->emit_gds_switch && ( |
553 | id->gds_base != job->gds_base || | |
554 | id->gds_size != job->gds_size || | |
555 | id->gws_base != job->gws_base || | |
556 | id->gws_size != job->gws_size || | |
557 | id->oa_base != job->oa_base || | |
558 | id->oa_size != job->oa_size); | |
e60f8db5 | 559 | |
620f774f | 560 | if (amdgpu_vmid_had_gpu_reset(adev, id)) |
b9bf33d5 | 561 | return true; |
e60f8db5 | 562 | |
bb37b67d | 563 | return vm_flush_needed || gds_switch_needed; |
b9bf33d5 CZ |
564 | } |
565 | ||
9a4b7d4c HK |
566 | static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev) |
567 | { | |
770d13b1 | 568 | return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size); |
e60f8db5 AX |
569 | } |
570 | ||
d38ceaf9 AD |
571 | /** |
572 | * amdgpu_vm_flush - hardware flush the vm | |
573 | * | |
574 | * @ring: ring to use for flush | |
c4f46f22 | 575 | * @vmid: vmid number to use |
4ff37a83 | 576 | * @pd_addr: address of the page directory |
d38ceaf9 | 577 | * |
4ff37a83 | 578 | * Emit a VM flush when it is necessary. |
d38ceaf9 | 579 | */ |
8fdf074f | 580 | int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync) |
d38ceaf9 | 581 | { |
971fe9a9 | 582 | struct amdgpu_device *adev = ring->adev; |
7645670d | 583 | unsigned vmhub = ring->funcs->vmhub; |
620f774f | 584 | struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; |
c4f46f22 | 585 | struct amdgpu_vmid *id = &id_mgr->ids[job->vmid]; |
d564a06e | 586 | bool gds_switch_needed = ring->funcs->emit_gds_switch && ( |
fd53be30 CZ |
587 | id->gds_base != job->gds_base || |
588 | id->gds_size != job->gds_size || | |
589 | id->gws_base != job->gws_base || | |
590 | id->gws_size != job->gws_size || | |
591 | id->oa_base != job->oa_base || | |
592 | id->oa_size != job->oa_size); | |
de37e68a | 593 | bool vm_flush_needed = job->vm_needs_flush; |
c0e51931 | 594 | unsigned patch_offset = 0; |
41d9eb2c | 595 | int r; |
d564a06e | 596 | |
620f774f | 597 | if (amdgpu_vmid_had_gpu_reset(adev, id)) { |
f7d015b9 CK |
598 | gds_switch_needed = true; |
599 | vm_flush_needed = true; | |
600 | } | |
971fe9a9 | 601 | |
8fdf074f | 602 | if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync) |
f7d015b9 | 603 | return 0; |
41d9eb2c | 604 | |
c0e51931 CK |
605 | if (ring->funcs->init_cond_exec) |
606 | patch_offset = amdgpu_ring_init_cond_exec(ring); | |
41d9eb2c | 607 | |
8fdf074f ML |
608 | if (need_pipe_sync) |
609 | amdgpu_ring_emit_pipeline_sync(ring); | |
610 | ||
f7d015b9 | 611 | if (ring->funcs->emit_vm_flush && vm_flush_needed) { |
c0e51931 | 612 | struct dma_fence *fence; |
41d9eb2c | 613 | |
c4f46f22 | 614 | trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr); |
5a4633c4 CK |
615 | amdgpu_ring_emit_vm_flush(ring, job->vmid, job->pasid, |
616 | job->vm_pd_addr); | |
e9d672b2 | 617 | |
c0e51931 CK |
618 | r = amdgpu_fence_emit(ring, &fence); |
619 | if (r) | |
620 | return r; | |
e9d672b2 | 621 | |
7645670d | 622 | mutex_lock(&id_mgr->lock); |
c0e51931 CK |
623 | dma_fence_put(id->last_flush); |
624 | id->last_flush = fence; | |
bea39672 | 625 | id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter); |
7645670d | 626 | mutex_unlock(&id_mgr->lock); |
c0e51931 | 627 | } |
e9d672b2 | 628 | |
7c4378f4 | 629 | if (ring->funcs->emit_gds_switch && gds_switch_needed) { |
c0e51931 CK |
630 | id->gds_base = job->gds_base; |
631 | id->gds_size = job->gds_size; | |
632 | id->gws_base = job->gws_base; | |
633 | id->gws_size = job->gws_size; | |
634 | id->oa_base = job->oa_base; | |
635 | id->oa_size = job->oa_size; | |
c4f46f22 | 636 | amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, |
c0e51931 CK |
637 | job->gds_size, job->gws_base, |
638 | job->gws_size, job->oa_base, | |
639 | job->oa_size); | |
640 | } | |
641 | ||
642 | if (ring->funcs->patch_cond_exec) | |
643 | amdgpu_ring_patch_cond_exec(ring, patch_offset); | |
644 | ||
645 | /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */ | |
646 | if (ring->funcs->emit_switch_buffer) { | |
647 | amdgpu_ring_emit_switch_buffer(ring); | |
648 | amdgpu_ring_emit_switch_buffer(ring); | |
e9d672b2 | 649 | } |
41d9eb2c | 650 | return 0; |
971fe9a9 CK |
651 | } |
652 | ||
d38ceaf9 AD |
653 | /** |
654 | * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo | |
655 | * | |
656 | * @vm: requested vm | |
657 | * @bo: requested buffer object | |
658 | * | |
8843dbbb | 659 | * Find @bo inside the requested vm. |
d38ceaf9 AD |
660 | * Search inside the @bos vm list for the requested vm |
661 | * Returns the found bo_va or NULL if none is found | |
662 | * | |
663 | * Object has to be reserved! | |
664 | */ | |
665 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, | |
666 | struct amdgpu_bo *bo) | |
667 | { | |
668 | struct amdgpu_bo_va *bo_va; | |
669 | ||
ec681545 CK |
670 | list_for_each_entry(bo_va, &bo->va, base.bo_list) { |
671 | if (bo_va->base.vm == vm) { | |
d38ceaf9 AD |
672 | return bo_va; |
673 | } | |
674 | } | |
675 | return NULL; | |
676 | } | |
677 | ||
678 | /** | |
afef8b8f | 679 | * amdgpu_vm_do_set_ptes - helper to call the right asic function |
d38ceaf9 | 680 | * |
29efc4f5 | 681 | * @params: see amdgpu_pte_update_params definition |
373ac645 | 682 | * @bo: PD/PT to update |
d38ceaf9 AD |
683 | * @pe: addr of the page entry |
684 | * @addr: dst addr to write into pe | |
685 | * @count: number of page entries to update | |
686 | * @incr: increase next addr by incr bytes | |
687 | * @flags: hw access flags | |
d38ceaf9 AD |
688 | * |
689 | * Traces the parameters and calls the right asic functions | |
690 | * to setup the page table using the DMA. | |
691 | */ | |
afef8b8f | 692 | static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params, |
373ac645 | 693 | struct amdgpu_bo *bo, |
afef8b8f CK |
694 | uint64_t pe, uint64_t addr, |
695 | unsigned count, uint32_t incr, | |
6b777607 | 696 | uint64_t flags) |
d38ceaf9 | 697 | { |
373ac645 | 698 | pe += amdgpu_bo_gpu_offset(bo); |
ec2f05f0 | 699 | trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags); |
d38ceaf9 | 700 | |
afef8b8f | 701 | if (count < 3) { |
de9ea7bd CK |
702 | amdgpu_vm_write_pte(params->adev, params->ib, pe, |
703 | addr | flags, count, incr); | |
d38ceaf9 AD |
704 | |
705 | } else { | |
27c5f36f | 706 | amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr, |
d38ceaf9 AD |
707 | count, incr, flags); |
708 | } | |
709 | } | |
710 | ||
afef8b8f CK |
711 | /** |
712 | * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART | |
713 | * | |
714 | * @params: see amdgpu_pte_update_params definition | |
373ac645 | 715 | * @bo: PD/PT to update |
afef8b8f CK |
716 | * @pe: addr of the page entry |
717 | * @addr: dst addr to write into pe | |
718 | * @count: number of page entries to update | |
719 | * @incr: increase next addr by incr bytes | |
720 | * @flags: hw access flags | |
721 | * | |
722 | * Traces the parameters and calls the DMA function to copy the PTEs. | |
723 | */ | |
724 | static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params, | |
373ac645 | 725 | struct amdgpu_bo *bo, |
afef8b8f CK |
726 | uint64_t pe, uint64_t addr, |
727 | unsigned count, uint32_t incr, | |
6b777607 | 728 | uint64_t flags) |
afef8b8f | 729 | { |
ec2f05f0 | 730 | uint64_t src = (params->src + (addr >> 12) * 8); |
afef8b8f | 731 | |
373ac645 | 732 | pe += amdgpu_bo_gpu_offset(bo); |
ec2f05f0 CK |
733 | trace_amdgpu_vm_copy_ptes(pe, src, count); |
734 | ||
735 | amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count); | |
afef8b8f CK |
736 | } |
737 | ||
d38ceaf9 | 738 | /** |
b07c9d2a | 739 | * amdgpu_vm_map_gart - Resolve gart mapping of addr |
d38ceaf9 | 740 | * |
b07c9d2a | 741 | * @pages_addr: optional DMA address to use for lookup |
d38ceaf9 AD |
742 | * @addr: the unmapped addr |
743 | * | |
744 | * Look up the physical address of the page that the pte resolves | |
b07c9d2a | 745 | * to and return the pointer for the page table entry. |
d38ceaf9 | 746 | */ |
de9ea7bd | 747 | static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) |
d38ceaf9 AD |
748 | { |
749 | uint64_t result; | |
750 | ||
de9ea7bd CK |
751 | /* page table offset */ |
752 | result = pages_addr[addr >> PAGE_SHIFT]; | |
b07c9d2a | 753 | |
de9ea7bd CK |
754 | /* in case cpu page size != gpu page size*/ |
755 | result |= addr & (~PAGE_MASK); | |
d38ceaf9 | 756 | |
b07c9d2a | 757 | result &= 0xFFFFFFFFFFFFF000ULL; |
d38ceaf9 AD |
758 | |
759 | return result; | |
760 | } | |
761 | ||
3c824172 HK |
762 | /** |
763 | * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU | |
764 | * | |
765 | * @params: see amdgpu_pte_update_params definition | |
373ac645 | 766 | * @bo: PD/PT to update |
3c824172 HK |
767 | * @pe: kmap addr of the page entry |
768 | * @addr: dst addr to write into pe | |
769 | * @count: number of page entries to update | |
770 | * @incr: increase next addr by incr bytes | |
771 | * @flags: hw access flags | |
772 | * | |
773 | * Write count number of PT/PD entries directly. | |
774 | */ | |
775 | static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params, | |
373ac645 | 776 | struct amdgpu_bo *bo, |
3c824172 HK |
777 | uint64_t pe, uint64_t addr, |
778 | unsigned count, uint32_t incr, | |
779 | uint64_t flags) | |
780 | { | |
781 | unsigned int i; | |
b4d42511 | 782 | uint64_t value; |
3c824172 | 783 | |
373ac645 CK |
784 | pe += (unsigned long)amdgpu_bo_kptr(bo); |
785 | ||
03918b36 CK |
786 | trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags); |
787 | ||
3c824172 | 788 | for (i = 0; i < count; i++) { |
b4d42511 HK |
789 | value = params->pages_addr ? |
790 | amdgpu_vm_map_gart(params->pages_addr, addr) : | |
791 | addr; | |
132f34e4 CK |
792 | amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe, |
793 | i, value, flags); | |
3c824172 HK |
794 | addr += incr; |
795 | } | |
3c824172 HK |
796 | } |
797 | ||
a33cab7a CK |
798 | static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
799 | void *owner) | |
3c824172 HK |
800 | { |
801 | struct amdgpu_sync sync; | |
802 | int r; | |
803 | ||
804 | amdgpu_sync_create(&sync); | |
177ae09b | 805 | amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false); |
3c824172 HK |
806 | r = amdgpu_sync_wait(&sync, true); |
807 | amdgpu_sync_free(&sync); | |
808 | ||
809 | return r; | |
810 | } | |
811 | ||
f8991bab | 812 | /* |
6989f246 | 813 | * amdgpu_vm_update_pde - update a single level in the hierarchy |
f8991bab | 814 | * |
6989f246 | 815 | * @param: parameters for the update |
f8991bab | 816 | * @vm: requested vm |
194d2161 | 817 | * @parent: parent directory |
6989f246 | 818 | * @entry: entry to update |
f8991bab | 819 | * |
6989f246 | 820 | * Makes sure the requested entry in parent is up to date. |
f8991bab | 821 | */ |
6989f246 CK |
822 | static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params, |
823 | struct amdgpu_vm *vm, | |
824 | struct amdgpu_vm_pt *parent, | |
825 | struct amdgpu_vm_pt *entry) | |
d38ceaf9 | 826 | { |
373ac645 | 827 | struct amdgpu_bo *bo = parent->base.bo, *pbo; |
3de676d8 CK |
828 | uint64_t pde, pt, flags; |
829 | unsigned level; | |
d5fc5e82 | 830 | |
6989f246 CK |
831 | /* Don't update huge pages here */ |
832 | if (entry->huge) | |
833 | return; | |
d38ceaf9 | 834 | |
373ac645 | 835 | for (level = 0, pbo = bo->parent; pbo; ++level) |
3de676d8 CK |
836 | pbo = pbo->parent; |
837 | ||
196f7489 | 838 | level += params->adev->vm_manager.root_level; |
373ac645 | 839 | pt = amdgpu_bo_gpu_offset(entry->base.bo); |
3de676d8 | 840 | flags = AMDGPU_PTE_VALID; |
132f34e4 | 841 | amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags); |
373ac645 CK |
842 | pde = (entry - parent->entries) * 8; |
843 | if (bo->shadow) | |
844 | params->func(params, bo->shadow, pde, pt, 1, 0, flags); | |
845 | params->func(params, bo, pde, pt, 1, 0, flags); | |
d38ceaf9 AD |
846 | } |
847 | ||
92456b93 CK |
848 | /* |
849 | * amdgpu_vm_invalidate_level - mark all PD levels as invalid | |
850 | * | |
851 | * @parent: parent PD | |
852 | * | |
853 | * Mark all PD level as invalid after an error. | |
854 | */ | |
8f19cd78 CK |
855 | static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev, |
856 | struct amdgpu_vm *vm, | |
857 | struct amdgpu_vm_pt *parent, | |
858 | unsigned level) | |
92456b93 | 859 | { |
8f19cd78 | 860 | unsigned pt_idx, num_entries; |
92456b93 CK |
861 | |
862 | /* | |
863 | * Recurse into the subdirectories. This recursion is harmless because | |
864 | * we only have a maximum of 5 layers. | |
865 | */ | |
8f19cd78 CK |
866 | num_entries = amdgpu_vm_num_entries(adev, level); |
867 | for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) { | |
92456b93 CK |
868 | struct amdgpu_vm_pt *entry = &parent->entries[pt_idx]; |
869 | ||
3f3333f8 | 870 | if (!entry->base.bo) |
92456b93 CK |
871 | continue; |
872 | ||
ea09729c | 873 | spin_lock(&vm->status_lock); |
481c2e94 CK |
874 | if (list_empty(&entry->base.vm_status)) |
875 | list_add(&entry->base.vm_status, &vm->relocated); | |
ea09729c | 876 | spin_unlock(&vm->status_lock); |
8f19cd78 | 877 | amdgpu_vm_invalidate_level(adev, vm, entry, level + 1); |
92456b93 CK |
878 | } |
879 | } | |
880 | ||
194d2161 CK |
881 | /* |
882 | * amdgpu_vm_update_directories - make sure that all directories are valid | |
883 | * | |
884 | * @adev: amdgpu_device pointer | |
885 | * @vm: requested vm | |
886 | * | |
887 | * Makes sure all directories are up to date. | |
888 | * Returns 0 for success, error for failure. | |
889 | */ | |
890 | int amdgpu_vm_update_directories(struct amdgpu_device *adev, | |
891 | struct amdgpu_vm *vm) | |
892 | { | |
6989f246 CK |
893 | struct amdgpu_pte_update_params params; |
894 | struct amdgpu_job *job; | |
895 | unsigned ndw = 0; | |
78aa02c7 | 896 | int r = 0; |
92456b93 | 897 | |
6989f246 CK |
898 | if (list_empty(&vm->relocated)) |
899 | return 0; | |
900 | ||
901 | restart: | |
902 | memset(¶ms, 0, sizeof(params)); | |
903 | params.adev = adev; | |
904 | ||
905 | if (vm->use_cpu_for_update) { | |
906 | r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM); | |
907 | if (unlikely(r)) | |
908 | return r; | |
909 | ||
910 | params.func = amdgpu_vm_cpu_set_ptes; | |
911 | } else { | |
912 | ndw = 512 * 8; | |
913 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); | |
914 | if (r) | |
915 | return r; | |
916 | ||
917 | params.ib = &job->ibs[0]; | |
918 | params.func = amdgpu_vm_do_set_ptes; | |
919 | } | |
920 | ||
ea09729c CK |
921 | spin_lock(&vm->status_lock); |
922 | while (!list_empty(&vm->relocated)) { | |
6989f246 CK |
923 | struct amdgpu_vm_bo_base *bo_base, *parent; |
924 | struct amdgpu_vm_pt *pt, *entry; | |
ea09729c CK |
925 | struct amdgpu_bo *bo; |
926 | ||
927 | bo_base = list_first_entry(&vm->relocated, | |
928 | struct amdgpu_vm_bo_base, | |
929 | vm_status); | |
6989f246 | 930 | list_del_init(&bo_base->vm_status); |
ea09729c CK |
931 | spin_unlock(&vm->status_lock); |
932 | ||
933 | bo = bo_base->bo->parent; | |
6989f246 | 934 | if (!bo) { |
ea09729c | 935 | spin_lock(&vm->status_lock); |
6989f246 | 936 | continue; |
ea09729c | 937 | } |
6989f246 CK |
938 | |
939 | parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base, | |
940 | bo_list); | |
941 | pt = container_of(parent, struct amdgpu_vm_pt, base); | |
942 | entry = container_of(bo_base, struct amdgpu_vm_pt, base); | |
943 | ||
944 | amdgpu_vm_update_pde(¶ms, vm, pt, entry); | |
945 | ||
946 | spin_lock(&vm->status_lock); | |
947 | if (!vm->use_cpu_for_update && | |
948 | (ndw - params.ib->length_dw) < 32) | |
949 | break; | |
ea09729c CK |
950 | } |
951 | spin_unlock(&vm->status_lock); | |
92456b93 | 952 | |
68c62306 CK |
953 | if (vm->use_cpu_for_update) { |
954 | /* Flush HDP */ | |
955 | mb(); | |
69882565 | 956 | amdgpu_asic_flush_hdp(adev, NULL); |
6989f246 CK |
957 | } else if (params.ib->length_dw == 0) { |
958 | amdgpu_job_free(job); | |
959 | } else { | |
960 | struct amdgpu_bo *root = vm->root.base.bo; | |
961 | struct amdgpu_ring *ring; | |
962 | struct dma_fence *fence; | |
963 | ||
964 | ring = container_of(vm->entity.sched, struct amdgpu_ring, | |
965 | sched); | |
966 | ||
967 | amdgpu_ring_pad_ib(ring, params.ib); | |
968 | amdgpu_sync_resv(adev, &job->sync, root->tbo.resv, | |
969 | AMDGPU_FENCE_OWNER_VM, false); | |
6989f246 CK |
970 | WARN_ON(params.ib->length_dw > ndw); |
971 | r = amdgpu_job_submit(job, ring, &vm->entity, | |
972 | AMDGPU_FENCE_OWNER_VM, &fence); | |
973 | if (r) | |
974 | goto error; | |
975 | ||
976 | amdgpu_bo_fence(root, fence, true); | |
977 | dma_fence_put(vm->last_update); | |
978 | vm->last_update = fence; | |
68c62306 CK |
979 | } |
980 | ||
6989f246 CK |
981 | if (!list_empty(&vm->relocated)) |
982 | goto restart; | |
983 | ||
984 | return 0; | |
985 | ||
986 | error: | |
196f7489 CZ |
987 | amdgpu_vm_invalidate_level(adev, vm, &vm->root, |
988 | adev->vm_manager.root_level); | |
6989f246 | 989 | amdgpu_job_free(job); |
92456b93 | 990 | return r; |
194d2161 CK |
991 | } |
992 | ||
4e2cb640 | 993 | /** |
cf2f0a37 | 994 | * amdgpu_vm_find_entry - find the entry for an address |
4e2cb640 CK |
995 | * |
996 | * @p: see amdgpu_pte_update_params definition | |
997 | * @addr: virtual address in question | |
cf2f0a37 AD |
998 | * @entry: resulting entry or NULL |
999 | * @parent: parent entry | |
4e2cb640 | 1000 | * |
cf2f0a37 | 1001 | * Find the vm_pt entry and it's parent for the given address. |
4e2cb640 | 1002 | */ |
cf2f0a37 AD |
1003 | void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr, |
1004 | struct amdgpu_vm_pt **entry, | |
1005 | struct amdgpu_vm_pt **parent) | |
4e2cb640 | 1006 | { |
196f7489 | 1007 | unsigned level = p->adev->vm_manager.root_level; |
4e2cb640 | 1008 | |
cf2f0a37 AD |
1009 | *parent = NULL; |
1010 | *entry = &p->vm->root; | |
1011 | while ((*entry)->entries) { | |
e3a1b32a | 1012 | unsigned shift = amdgpu_vm_level_shift(p->adev, level++); |
50783147 | 1013 | |
cf2f0a37 | 1014 | *parent = *entry; |
e3a1b32a CK |
1015 | *entry = &(*entry)->entries[addr >> shift]; |
1016 | addr &= (1ULL << shift) - 1; | |
4e2cb640 CK |
1017 | } |
1018 | ||
196f7489 | 1019 | if (level != AMDGPU_VM_PTB) |
cf2f0a37 AD |
1020 | *entry = NULL; |
1021 | } | |
1022 | ||
1023 | /** | |
1024 | * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages | |
1025 | * | |
1026 | * @p: see amdgpu_pte_update_params definition | |
1027 | * @entry: vm_pt entry to check | |
1028 | * @parent: parent entry | |
1029 | * @nptes: number of PTEs updated with this operation | |
1030 | * @dst: destination address where the PTEs should point to | |
1031 | * @flags: access flags fro the PTEs | |
1032 | * | |
1033 | * Check if we can update the PD with a huge page. | |
1034 | */ | |
ec5207c9 CK |
1035 | static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p, |
1036 | struct amdgpu_vm_pt *entry, | |
1037 | struct amdgpu_vm_pt *parent, | |
1038 | unsigned nptes, uint64_t dst, | |
1039 | uint64_t flags) | |
cf2f0a37 | 1040 | { |
373ac645 | 1041 | uint64_t pde; |
cf2f0a37 AD |
1042 | |
1043 | /* In the case of a mixed PT the PDE must point to it*/ | |
3cc1d3ea CK |
1044 | if (p->adev->asic_type >= CHIP_VEGA10 && !p->src && |
1045 | nptes == AMDGPU_VM_PTE_COUNT(p->adev)) { | |
4ab4016a | 1046 | /* Set the huge page flag to stop scanning at this PDE */ |
cf2f0a37 AD |
1047 | flags |= AMDGPU_PDE_PTE; |
1048 | } | |
1049 | ||
3cc1d3ea CK |
1050 | if (!(flags & AMDGPU_PDE_PTE)) { |
1051 | if (entry->huge) { | |
1052 | /* Add the entry to the relocated list to update it. */ | |
1053 | entry->huge = false; | |
1054 | spin_lock(&p->vm->status_lock); | |
1055 | list_move(&entry->base.vm_status, &p->vm->relocated); | |
1056 | spin_unlock(&p->vm->status_lock); | |
1057 | } | |
ec5207c9 | 1058 | return; |
3cc1d3ea | 1059 | } |
cf2f0a37 | 1060 | |
3cc1d3ea | 1061 | entry->huge = true; |
132f34e4 | 1062 | amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags); |
3de676d8 | 1063 | |
373ac645 CK |
1064 | pde = (entry - parent->entries) * 8; |
1065 | if (parent->base.bo->shadow) | |
1066 | p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags); | |
1067 | p->func(p, parent->base.bo, pde, dst, 1, 0, flags); | |
4e2cb640 CK |
1068 | } |
1069 | ||
d38ceaf9 AD |
1070 | /** |
1071 | * amdgpu_vm_update_ptes - make sure that page tables are valid | |
1072 | * | |
29efc4f5 | 1073 | * @params: see amdgpu_pte_update_params definition |
d38ceaf9 AD |
1074 | * @vm: requested vm |
1075 | * @start: start of GPU address range | |
1076 | * @end: end of GPU address range | |
677131a1 | 1077 | * @dst: destination address to map to, the next dst inside the function |
d38ceaf9 AD |
1078 | * @flags: mapping flags |
1079 | * | |
8843dbbb | 1080 | * Update the page tables in the range @start - @end. |
cc28c4ed | 1081 | * Returns 0 for success, -EINVAL for failure. |
d38ceaf9 | 1082 | */ |
cc28c4ed | 1083 | static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params, |
a1e08d3b | 1084 | uint64_t start, uint64_t end, |
6b777607 | 1085 | uint64_t dst, uint64_t flags) |
d38ceaf9 | 1086 | { |
36b32a68 ZJ |
1087 | struct amdgpu_device *adev = params->adev; |
1088 | const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1; | |
31f6c1fe | 1089 | |
301654a4 | 1090 | uint64_t addr, pe_start; |
21718497 | 1091 | struct amdgpu_bo *pt; |
301654a4 | 1092 | unsigned nptes; |
d38ceaf9 AD |
1093 | |
1094 | /* walk over the address space and update the page tables */ | |
cf2f0a37 AD |
1095 | for (addr = start; addr < end; addr += nptes, |
1096 | dst += nptes * AMDGPU_GPU_PAGE_SIZE) { | |
1097 | struct amdgpu_vm_pt *entry, *parent; | |
1098 | ||
1099 | amdgpu_vm_get_entry(params, addr, &entry, &parent); | |
1100 | if (!entry) | |
1101 | return -ENOENT; | |
4e2cb640 | 1102 | |
d38ceaf9 AD |
1103 | if ((addr & ~mask) == (end & ~mask)) |
1104 | nptes = end - addr; | |
1105 | else | |
36b32a68 | 1106 | nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask); |
d38ceaf9 | 1107 | |
ec5207c9 CK |
1108 | amdgpu_vm_handle_huge_pages(params, entry, parent, |
1109 | nptes, dst, flags); | |
4ab4016a | 1110 | /* We don't need to update PTEs for huge pages */ |
78eb2f0c | 1111 | if (entry->huge) |
cf2f0a37 AD |
1112 | continue; |
1113 | ||
3f3333f8 | 1114 | pt = entry->base.bo; |
373ac645 CK |
1115 | pe_start = (addr & mask) * 8; |
1116 | if (pt->shadow) | |
1117 | params->func(params, pt->shadow, pe_start, dst, nptes, | |
1118 | AMDGPU_GPU_PAGE_SIZE, flags); | |
1119 | params->func(params, pt, pe_start, dst, nptes, | |
301654a4 | 1120 | AMDGPU_GPU_PAGE_SIZE, flags); |
d38ceaf9 AD |
1121 | } |
1122 | ||
cc28c4ed | 1123 | return 0; |
92696dd5 CK |
1124 | } |
1125 | ||
1126 | /* | |
1127 | * amdgpu_vm_frag_ptes - add fragment information to PTEs | |
1128 | * | |
1129 | * @params: see amdgpu_pte_update_params definition | |
1130 | * @vm: requested vm | |
1131 | * @start: first PTE to handle | |
1132 | * @end: last PTE to handle | |
1133 | * @dst: addr those PTEs should point to | |
1134 | * @flags: hw mapping flags | |
cc28c4ed | 1135 | * Returns 0 for success, -EINVAL for failure. |
92696dd5 | 1136 | */ |
cc28c4ed | 1137 | static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params, |
92696dd5 | 1138 | uint64_t start, uint64_t end, |
6b777607 | 1139 | uint64_t dst, uint64_t flags) |
92696dd5 CK |
1140 | { |
1141 | /** | |
1142 | * The MC L1 TLB supports variable sized pages, based on a fragment | |
1143 | * field in the PTE. When this field is set to a non-zero value, page | |
1144 | * granularity is increased from 4KB to (1 << (12 + frag)). The PTE | |
1145 | * flags are considered valid for all PTEs within the fragment range | |
1146 | * and corresponding mappings are assumed to be physically contiguous. | |
1147 | * | |
1148 | * The L1 TLB can store a single PTE for the whole fragment, | |
1149 | * significantly increasing the space available for translation | |
1150 | * caching. This leads to large improvements in throughput when the | |
1151 | * TLB is under pressure. | |
1152 | * | |
1153 | * The L2 TLB distributes small and large fragments into two | |
1154 | * asymmetric partitions. The large fragment cache is significantly | |
1155 | * larger. Thus, we try to use large fragments wherever possible. | |
1156 | * Userspace can support this by aligning virtual base address and | |
1157 | * allocation size to the fragment size. | |
1158 | */ | |
6849d47c RH |
1159 | unsigned max_frag = params->adev->vm_manager.fragment_size; |
1160 | int r; | |
92696dd5 CK |
1161 | |
1162 | /* system pages are non continuously */ | |
6849d47c | 1163 | if (params->src || !(flags & AMDGPU_PTE_VALID)) |
cc28c4ed | 1164 | return amdgpu_vm_update_ptes(params, start, end, dst, flags); |
92696dd5 | 1165 | |
6849d47c RH |
1166 | while (start != end) { |
1167 | uint64_t frag_flags, frag_end; | |
1168 | unsigned frag; | |
1169 | ||
1170 | /* This intentionally wraps around if no bit is set */ | |
1171 | frag = min((unsigned)ffs(start) - 1, | |
1172 | (unsigned)fls64(end - start) - 1); | |
1173 | if (frag >= max_frag) { | |
1174 | frag_flags = AMDGPU_PTE_FRAG(max_frag); | |
1175 | frag_end = end & ~((1ULL << max_frag) - 1); | |
1176 | } else { | |
1177 | frag_flags = AMDGPU_PTE_FRAG(frag); | |
1178 | frag_end = start + (1 << frag); | |
1179 | } | |
1180 | ||
1181 | r = amdgpu_vm_update_ptes(params, start, frag_end, dst, | |
1182 | flags | frag_flags); | |
cc28c4ed HK |
1183 | if (r) |
1184 | return r; | |
92696dd5 | 1185 | |
6849d47c RH |
1186 | dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE; |
1187 | start = frag_end; | |
92696dd5 | 1188 | } |
6849d47c RH |
1189 | |
1190 | return 0; | |
d38ceaf9 AD |
1191 | } |
1192 | ||
d38ceaf9 AD |
1193 | /** |
1194 | * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table | |
1195 | * | |
1196 | * @adev: amdgpu_device pointer | |
3cabaa54 | 1197 | * @exclusive: fence we need to sync to |
fa3ab3c7 | 1198 | * @pages_addr: DMA addresses to use for mapping |
d38ceaf9 | 1199 | * @vm: requested vm |
a14faa65 CK |
1200 | * @start: start of mapped range |
1201 | * @last: last mapped entry | |
1202 | * @flags: flags for the entries | |
d38ceaf9 | 1203 | * @addr: addr to set the area to |
d38ceaf9 AD |
1204 | * @fence: optional resulting fence |
1205 | * | |
a14faa65 | 1206 | * Fill in the page table entries between @start and @last. |
d38ceaf9 | 1207 | * Returns 0 for success, -EINVAL for failure. |
d38ceaf9 AD |
1208 | */ |
1209 | static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, | |
f54d1867 | 1210 | struct dma_fence *exclusive, |
fa3ab3c7 | 1211 | dma_addr_t *pages_addr, |
d38ceaf9 | 1212 | struct amdgpu_vm *vm, |
a14faa65 | 1213 | uint64_t start, uint64_t last, |
6b777607 | 1214 | uint64_t flags, uint64_t addr, |
f54d1867 | 1215 | struct dma_fence **fence) |
d38ceaf9 | 1216 | { |
2d55e45a | 1217 | struct amdgpu_ring *ring; |
a1e08d3b | 1218 | void *owner = AMDGPU_FENCE_OWNER_VM; |
d38ceaf9 | 1219 | unsigned nptes, ncmds, ndw; |
d71518b5 | 1220 | struct amdgpu_job *job; |
29efc4f5 | 1221 | struct amdgpu_pte_update_params params; |
f54d1867 | 1222 | struct dma_fence *f = NULL; |
d38ceaf9 AD |
1223 | int r; |
1224 | ||
afef8b8f CK |
1225 | memset(¶ms, 0, sizeof(params)); |
1226 | params.adev = adev; | |
49ac8a24 | 1227 | params.vm = vm; |
afef8b8f | 1228 | |
a33cab7a CK |
1229 | /* sync to everything on unmapping */ |
1230 | if (!(flags & AMDGPU_PTE_VALID)) | |
1231 | owner = AMDGPU_FENCE_OWNER_UNDEFINED; | |
1232 | ||
b4d42511 HK |
1233 | if (vm->use_cpu_for_update) { |
1234 | /* params.src is used as flag to indicate system Memory */ | |
1235 | if (pages_addr) | |
1236 | params.src = ~0; | |
1237 | ||
1238 | /* Wait for PT BOs to be free. PTs share the same resv. object | |
1239 | * as the root PD BO | |
1240 | */ | |
a33cab7a | 1241 | r = amdgpu_vm_wait_pd(adev, vm, owner); |
b4d42511 HK |
1242 | if (unlikely(r)) |
1243 | return r; | |
1244 | ||
1245 | params.func = amdgpu_vm_cpu_set_ptes; | |
1246 | params.pages_addr = pages_addr; | |
b4d42511 HK |
1247 | return amdgpu_vm_frag_ptes(¶ms, start, last + 1, |
1248 | addr, flags); | |
1249 | } | |
1250 | ||
2d55e45a | 1251 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
27c5f36f | 1252 | |
a14faa65 | 1253 | nptes = last - start + 1; |
d38ceaf9 AD |
1254 | |
1255 | /* | |
86209523 | 1256 | * reserve space for two commands every (1 << BLOCK_SIZE) |
d38ceaf9 | 1257 | * entries or 2k dwords (whatever is smaller) |
86209523 BN |
1258 | * |
1259 | * The second command is for the shadow pagetables. | |
d38ceaf9 | 1260 | */ |
104bd2ca ED |
1261 | if (vm->root.base.bo->shadow) |
1262 | ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2; | |
1263 | else | |
1264 | ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1); | |
d38ceaf9 AD |
1265 | |
1266 | /* padding, etc. */ | |
1267 | ndw = 64; | |
1268 | ||
570144c6 | 1269 | if (pages_addr) { |
b0456f93 | 1270 | /* copy commands needed */ |
e6d92197 | 1271 | ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw; |
d38ceaf9 | 1272 | |
b0456f93 | 1273 | /* and also PTEs */ |
d38ceaf9 AD |
1274 | ndw += nptes * 2; |
1275 | ||
afef8b8f CK |
1276 | params.func = amdgpu_vm_do_copy_ptes; |
1277 | ||
d38ceaf9 AD |
1278 | } else { |
1279 | /* set page commands needed */ | |
44e1baeb | 1280 | ndw += ncmds * 10; |
d38ceaf9 | 1281 | |
6849d47c | 1282 | /* extra commands for begin/end fragments */ |
44e1baeb | 1283 | ndw += 2 * 10 * adev->vm_manager.fragment_size; |
afef8b8f CK |
1284 | |
1285 | params.func = amdgpu_vm_do_set_ptes; | |
d38ceaf9 AD |
1286 | } |
1287 | ||
d71518b5 CK |
1288 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
1289 | if (r) | |
d38ceaf9 | 1290 | return r; |
d71518b5 | 1291 | |
29efc4f5 | 1292 | params.ib = &job->ibs[0]; |
d5fc5e82 | 1293 | |
570144c6 | 1294 | if (pages_addr) { |
b0456f93 CK |
1295 | uint64_t *pte; |
1296 | unsigned i; | |
1297 | ||
1298 | /* Put the PTEs at the end of the IB. */ | |
1299 | i = ndw - nptes * 2; | |
1300 | pte= (uint64_t *)&(job->ibs->ptr[i]); | |
1301 | params.src = job->ibs->gpu_addr + i * 4; | |
1302 | ||
1303 | for (i = 0; i < nptes; ++i) { | |
1304 | pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i * | |
1305 | AMDGPU_GPU_PAGE_SIZE); | |
1306 | pte[i] |= flags; | |
1307 | } | |
d7a4ac66 | 1308 | addr = 0; |
b0456f93 CK |
1309 | } |
1310 | ||
cebb52b7 | 1311 | r = amdgpu_sync_fence(adev, &job->sync, exclusive, false); |
3cabaa54 CK |
1312 | if (r) |
1313 | goto error_free; | |
1314 | ||
3f3333f8 | 1315 | r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv, |
177ae09b | 1316 | owner, false); |
a1e08d3b CK |
1317 | if (r) |
1318 | goto error_free; | |
d38ceaf9 | 1319 | |
3f3333f8 | 1320 | r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv); |
a1e08d3b CK |
1321 | if (r) |
1322 | goto error_free; | |
1323 | ||
cc28c4ed HK |
1324 | r = amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags); |
1325 | if (r) | |
1326 | goto error_free; | |
d38ceaf9 | 1327 | |
29efc4f5 CK |
1328 | amdgpu_ring_pad_ib(ring, params.ib); |
1329 | WARN_ON(params.ib->length_dw > ndw); | |
2bd9ccfa CK |
1330 | r = amdgpu_job_submit(job, ring, &vm->entity, |
1331 | AMDGPU_FENCE_OWNER_VM, &f); | |
4af9f07c CZ |
1332 | if (r) |
1333 | goto error_free; | |
d38ceaf9 | 1334 | |
3f3333f8 | 1335 | amdgpu_bo_fence(vm->root.base.bo, f, true); |
284710fa CK |
1336 | dma_fence_put(*fence); |
1337 | *fence = f; | |
d38ceaf9 | 1338 | return 0; |
d5fc5e82 CZ |
1339 | |
1340 | error_free: | |
d71518b5 | 1341 | amdgpu_job_free(job); |
4af9f07c | 1342 | return r; |
d38ceaf9 AD |
1343 | } |
1344 | ||
a14faa65 CK |
1345 | /** |
1346 | * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks | |
1347 | * | |
1348 | * @adev: amdgpu_device pointer | |
3cabaa54 | 1349 | * @exclusive: fence we need to sync to |
8358dcee | 1350 | * @pages_addr: DMA addresses to use for mapping |
a14faa65 CK |
1351 | * @vm: requested vm |
1352 | * @mapping: mapped range and flags to use for the update | |
8358dcee | 1353 | * @flags: HW flags for the mapping |
63e0ba40 | 1354 | * @nodes: array of drm_mm_nodes with the MC addresses |
a14faa65 CK |
1355 | * @fence: optional resulting fence |
1356 | * | |
1357 | * Split the mapping into smaller chunks so that each update fits | |
1358 | * into a SDMA IB. | |
1359 | * Returns 0 for success, -EINVAL for failure. | |
1360 | */ | |
1361 | static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev, | |
f54d1867 | 1362 | struct dma_fence *exclusive, |
8358dcee | 1363 | dma_addr_t *pages_addr, |
a14faa65 CK |
1364 | struct amdgpu_vm *vm, |
1365 | struct amdgpu_bo_va_mapping *mapping, | |
6b777607 | 1366 | uint64_t flags, |
63e0ba40 | 1367 | struct drm_mm_node *nodes, |
f54d1867 | 1368 | struct dma_fence **fence) |
a14faa65 | 1369 | { |
9fc8fc70 | 1370 | unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size; |
570144c6 | 1371 | uint64_t pfn, start = mapping->start; |
a14faa65 CK |
1372 | int r; |
1373 | ||
1374 | /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here | |
1375 | * but in case of something, we filter the flags in first place | |
1376 | */ | |
1377 | if (!(mapping->flags & AMDGPU_PTE_READABLE)) | |
1378 | flags &= ~AMDGPU_PTE_READABLE; | |
1379 | if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) | |
1380 | flags &= ~AMDGPU_PTE_WRITEABLE; | |
1381 | ||
15b31c59 AX |
1382 | flags &= ~AMDGPU_PTE_EXECUTABLE; |
1383 | flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE; | |
1384 | ||
b0fd18b0 AX |
1385 | flags &= ~AMDGPU_PTE_MTYPE_MASK; |
1386 | flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK); | |
1387 | ||
d0766e98 ZJ |
1388 | if ((mapping->flags & AMDGPU_PTE_PRT) && |
1389 | (adev->asic_type >= CHIP_VEGA10)) { | |
1390 | flags |= AMDGPU_PTE_PRT; | |
1391 | flags &= ~AMDGPU_PTE_VALID; | |
1392 | } | |
1393 | ||
a14faa65 CK |
1394 | trace_amdgpu_vm_bo_update(mapping); |
1395 | ||
63e0ba40 CK |
1396 | pfn = mapping->offset >> PAGE_SHIFT; |
1397 | if (nodes) { | |
1398 | while (pfn >= nodes->size) { | |
1399 | pfn -= nodes->size; | |
1400 | ++nodes; | |
1401 | } | |
fa3ab3c7 | 1402 | } |
a14faa65 | 1403 | |
63e0ba40 | 1404 | do { |
9fc8fc70 | 1405 | dma_addr_t *dma_addr = NULL; |
63e0ba40 CK |
1406 | uint64_t max_entries; |
1407 | uint64_t addr, last; | |
a14faa65 | 1408 | |
63e0ba40 CK |
1409 | if (nodes) { |
1410 | addr = nodes->start << PAGE_SHIFT; | |
1411 | max_entries = (nodes->size - pfn) * | |
1412 | (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); | |
1413 | } else { | |
1414 | addr = 0; | |
1415 | max_entries = S64_MAX; | |
1416 | } | |
a14faa65 | 1417 | |
63e0ba40 | 1418 | if (pages_addr) { |
9fc8fc70 CK |
1419 | uint64_t count; |
1420 | ||
457e0fee | 1421 | max_entries = min(max_entries, 16ull * 1024ull); |
9fc8fc70 CK |
1422 | for (count = 1; count < max_entries; ++count) { |
1423 | uint64_t idx = pfn + count; | |
1424 | ||
1425 | if (pages_addr[idx] != | |
1426 | (pages_addr[idx - 1] + PAGE_SIZE)) | |
1427 | break; | |
1428 | } | |
1429 | ||
1430 | if (count < min_linear_pages) { | |
1431 | addr = pfn << PAGE_SHIFT; | |
1432 | dma_addr = pages_addr; | |
1433 | } else { | |
1434 | addr = pages_addr[pfn]; | |
1435 | max_entries = count; | |
1436 | } | |
1437 | ||
63e0ba40 CK |
1438 | } else if (flags & AMDGPU_PTE_VALID) { |
1439 | addr += adev->vm_manager.vram_base_offset; | |
9fc8fc70 | 1440 | addr += pfn << PAGE_SHIFT; |
63e0ba40 | 1441 | } |
63e0ba40 | 1442 | |
a9f87f64 | 1443 | last = min((uint64_t)mapping->last, start + max_entries - 1); |
9fc8fc70 | 1444 | r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm, |
a14faa65 CK |
1445 | start, last, flags, addr, |
1446 | fence); | |
1447 | if (r) | |
1448 | return r; | |
1449 | ||
63e0ba40 CK |
1450 | pfn += last - start + 1; |
1451 | if (nodes && nodes->size == pfn) { | |
1452 | pfn = 0; | |
1453 | ++nodes; | |
1454 | } | |
a14faa65 | 1455 | start = last + 1; |
63e0ba40 | 1456 | |
a9f87f64 | 1457 | } while (unlikely(start != mapping->last + 1)); |
a14faa65 CK |
1458 | |
1459 | return 0; | |
1460 | } | |
1461 | ||
d38ceaf9 AD |
1462 | /** |
1463 | * amdgpu_vm_bo_update - update all BO mappings in the vm page table | |
1464 | * | |
1465 | * @adev: amdgpu_device pointer | |
1466 | * @bo_va: requested BO and VM object | |
99e124f4 | 1467 | * @clear: if true clear the entries |
d38ceaf9 AD |
1468 | * |
1469 | * Fill in the page table entries for @bo_va. | |
1470 | * Returns 0 for success, -EINVAL for failure. | |
d38ceaf9 AD |
1471 | */ |
1472 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, | |
1473 | struct amdgpu_bo_va *bo_va, | |
99e124f4 | 1474 | bool clear) |
d38ceaf9 | 1475 | { |
ec681545 CK |
1476 | struct amdgpu_bo *bo = bo_va->base.bo; |
1477 | struct amdgpu_vm *vm = bo_va->base.vm; | |
d38ceaf9 | 1478 | struct amdgpu_bo_va_mapping *mapping; |
8358dcee | 1479 | dma_addr_t *pages_addr = NULL; |
99e124f4 | 1480 | struct ttm_mem_reg *mem; |
63e0ba40 | 1481 | struct drm_mm_node *nodes; |
4e55eb38 | 1482 | struct dma_fence *exclusive, **last_update; |
457e0fee | 1483 | uint64_t flags; |
d38ceaf9 AD |
1484 | int r; |
1485 | ||
ec681545 | 1486 | if (clear || !bo_va->base.bo) { |
99e124f4 | 1487 | mem = NULL; |
63e0ba40 | 1488 | nodes = NULL; |
99e124f4 CK |
1489 | exclusive = NULL; |
1490 | } else { | |
8358dcee CK |
1491 | struct ttm_dma_tt *ttm; |
1492 | ||
ec681545 | 1493 | mem = &bo_va->base.bo->tbo.mem; |
63e0ba40 CK |
1494 | nodes = mem->mm_node; |
1495 | if (mem->mem_type == TTM_PL_TT) { | |
ec681545 CK |
1496 | ttm = container_of(bo_va->base.bo->tbo.ttm, |
1497 | struct ttm_dma_tt, ttm); | |
8358dcee | 1498 | pages_addr = ttm->dma_address; |
9ab21462 | 1499 | } |
ec681545 | 1500 | exclusive = reservation_object_get_excl(bo->tbo.resv); |
d38ceaf9 AD |
1501 | } |
1502 | ||
457e0fee | 1503 | if (bo) |
ec681545 | 1504 | flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem); |
457e0fee | 1505 | else |
a5f6b5b1 | 1506 | flags = 0x0; |
d38ceaf9 | 1507 | |
4e55eb38 CK |
1508 | if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv)) |
1509 | last_update = &vm->last_update; | |
1510 | else | |
1511 | last_update = &bo_va->last_pt_update; | |
1512 | ||
3d7d4d3a CK |
1513 | if (!clear && bo_va->base.moved) { |
1514 | bo_va->base.moved = false; | |
7fc11959 | 1515 | list_splice_init(&bo_va->valids, &bo_va->invalids); |
3d7d4d3a | 1516 | |
cb7b6ec2 CK |
1517 | } else if (bo_va->cleared != clear) { |
1518 | list_splice_init(&bo_va->valids, &bo_va->invalids); | |
3d7d4d3a | 1519 | } |
7fc11959 CK |
1520 | |
1521 | list_for_each_entry(mapping, &bo_va->invalids, list) { | |
457e0fee | 1522 | r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm, |
63e0ba40 | 1523 | mapping, flags, nodes, |
4e55eb38 | 1524 | last_update); |
d38ceaf9 AD |
1525 | if (r) |
1526 | return r; | |
1527 | } | |
1528 | ||
cb7b6ec2 CK |
1529 | if (vm->use_cpu_for_update) { |
1530 | /* Flush HDP */ | |
1531 | mb(); | |
69882565 | 1532 | amdgpu_asic_flush_hdp(adev, NULL); |
d6c10f6b CK |
1533 | } |
1534 | ||
d38ceaf9 | 1535 | spin_lock(&vm->status_lock); |
ec681545 | 1536 | list_del_init(&bo_va->base.vm_status); |
d38ceaf9 AD |
1537 | spin_unlock(&vm->status_lock); |
1538 | ||
cb7b6ec2 CK |
1539 | list_splice_init(&bo_va->invalids, &bo_va->valids); |
1540 | bo_va->cleared = clear; | |
1541 | ||
1542 | if (trace_amdgpu_vm_bo_mapping_enabled()) { | |
1543 | list_for_each_entry(mapping, &bo_va->valids, list) | |
1544 | trace_amdgpu_vm_bo_mapping(mapping); | |
68c62306 CK |
1545 | } |
1546 | ||
d38ceaf9 AD |
1547 | return 0; |
1548 | } | |
1549 | ||
284710fa CK |
1550 | /** |
1551 | * amdgpu_vm_update_prt_state - update the global PRT state | |
1552 | */ | |
1553 | static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) | |
1554 | { | |
1555 | unsigned long flags; | |
1556 | bool enable; | |
1557 | ||
1558 | spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); | |
451bc8eb | 1559 | enable = !!atomic_read(&adev->vm_manager.num_prt_users); |
132f34e4 | 1560 | adev->gmc.gmc_funcs->set_prt(adev, enable); |
284710fa CK |
1561 | spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); |
1562 | } | |
1563 | ||
451bc8eb | 1564 | /** |
4388fc2a | 1565 | * amdgpu_vm_prt_get - add a PRT user |
451bc8eb CK |
1566 | */ |
1567 | static void amdgpu_vm_prt_get(struct amdgpu_device *adev) | |
1568 | { | |
132f34e4 | 1569 | if (!adev->gmc.gmc_funcs->set_prt) |
4388fc2a CK |
1570 | return; |
1571 | ||
451bc8eb CK |
1572 | if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) |
1573 | amdgpu_vm_update_prt_state(adev); | |
1574 | } | |
1575 | ||
0b15f2fc CK |
1576 | /** |
1577 | * amdgpu_vm_prt_put - drop a PRT user | |
1578 | */ | |
1579 | static void amdgpu_vm_prt_put(struct amdgpu_device *adev) | |
1580 | { | |
451bc8eb | 1581 | if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) |
0b15f2fc CK |
1582 | amdgpu_vm_update_prt_state(adev); |
1583 | } | |
1584 | ||
284710fa | 1585 | /** |
451bc8eb | 1586 | * amdgpu_vm_prt_cb - callback for updating the PRT status |
284710fa CK |
1587 | */ |
1588 | static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) | |
1589 | { | |
1590 | struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); | |
1591 | ||
0b15f2fc | 1592 | amdgpu_vm_prt_put(cb->adev); |
284710fa CK |
1593 | kfree(cb); |
1594 | } | |
1595 | ||
451bc8eb CK |
1596 | /** |
1597 | * amdgpu_vm_add_prt_cb - add callback for updating the PRT status | |
1598 | */ | |
1599 | static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, | |
1600 | struct dma_fence *fence) | |
1601 | { | |
4388fc2a | 1602 | struct amdgpu_prt_cb *cb; |
451bc8eb | 1603 | |
132f34e4 | 1604 | if (!adev->gmc.gmc_funcs->set_prt) |
4388fc2a CK |
1605 | return; |
1606 | ||
1607 | cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); | |
451bc8eb CK |
1608 | if (!cb) { |
1609 | /* Last resort when we are OOM */ | |
1610 | if (fence) | |
1611 | dma_fence_wait(fence, false); | |
1612 | ||
486a68f5 | 1613 | amdgpu_vm_prt_put(adev); |
451bc8eb CK |
1614 | } else { |
1615 | cb->adev = adev; | |
1616 | if (!fence || dma_fence_add_callback(fence, &cb->cb, | |
1617 | amdgpu_vm_prt_cb)) | |
1618 | amdgpu_vm_prt_cb(fence, &cb->cb); | |
1619 | } | |
1620 | } | |
1621 | ||
284710fa CK |
1622 | /** |
1623 | * amdgpu_vm_free_mapping - free a mapping | |
1624 | * | |
1625 | * @adev: amdgpu_device pointer | |
1626 | * @vm: requested vm | |
1627 | * @mapping: mapping to be freed | |
1628 | * @fence: fence of the unmap operation | |
1629 | * | |
1630 | * Free a mapping and make sure we decrease the PRT usage count if applicable. | |
1631 | */ | |
1632 | static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, | |
1633 | struct amdgpu_vm *vm, | |
1634 | struct amdgpu_bo_va_mapping *mapping, | |
1635 | struct dma_fence *fence) | |
1636 | { | |
451bc8eb CK |
1637 | if (mapping->flags & AMDGPU_PTE_PRT) |
1638 | amdgpu_vm_add_prt_cb(adev, fence); | |
1639 | kfree(mapping); | |
1640 | } | |
284710fa | 1641 | |
451bc8eb CK |
1642 | /** |
1643 | * amdgpu_vm_prt_fini - finish all prt mappings | |
1644 | * | |
1645 | * @adev: amdgpu_device pointer | |
1646 | * @vm: requested vm | |
1647 | * | |
1648 | * Register a cleanup callback to disable PRT support after VM dies. | |
1649 | */ | |
1650 | static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
1651 | { | |
3f3333f8 | 1652 | struct reservation_object *resv = vm->root.base.bo->tbo.resv; |
451bc8eb CK |
1653 | struct dma_fence *excl, **shared; |
1654 | unsigned i, shared_count; | |
1655 | int r; | |
0b15f2fc | 1656 | |
451bc8eb CK |
1657 | r = reservation_object_get_fences_rcu(resv, &excl, |
1658 | &shared_count, &shared); | |
1659 | if (r) { | |
1660 | /* Not enough memory to grab the fence list, as last resort | |
1661 | * block for all the fences to complete. | |
1662 | */ | |
1663 | reservation_object_wait_timeout_rcu(resv, true, false, | |
1664 | MAX_SCHEDULE_TIMEOUT); | |
1665 | return; | |
284710fa | 1666 | } |
451bc8eb CK |
1667 | |
1668 | /* Add a callback for each fence in the reservation object */ | |
1669 | amdgpu_vm_prt_get(adev); | |
1670 | amdgpu_vm_add_prt_cb(adev, excl); | |
1671 | ||
1672 | for (i = 0; i < shared_count; ++i) { | |
1673 | amdgpu_vm_prt_get(adev); | |
1674 | amdgpu_vm_add_prt_cb(adev, shared[i]); | |
1675 | } | |
1676 | ||
1677 | kfree(shared); | |
284710fa CK |
1678 | } |
1679 | ||
d38ceaf9 AD |
1680 | /** |
1681 | * amdgpu_vm_clear_freed - clear freed BOs in the PT | |
1682 | * | |
1683 | * @adev: amdgpu_device pointer | |
1684 | * @vm: requested vm | |
f3467818 NH |
1685 | * @fence: optional resulting fence (unchanged if no work needed to be done |
1686 | * or if an error occurred) | |
d38ceaf9 AD |
1687 | * |
1688 | * Make sure all freed BOs are cleared in the PT. | |
1689 | * Returns 0 for success. | |
1690 | * | |
1691 | * PTs have to be reserved and mutex must be locked! | |
1692 | */ | |
1693 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, | |
f3467818 NH |
1694 | struct amdgpu_vm *vm, |
1695 | struct dma_fence **fence) | |
d38ceaf9 AD |
1696 | { |
1697 | struct amdgpu_bo_va_mapping *mapping; | |
4584312d | 1698 | uint64_t init_pte_value = 0; |
f3467818 | 1699 | struct dma_fence *f = NULL; |
d38ceaf9 AD |
1700 | int r; |
1701 | ||
1702 | while (!list_empty(&vm->freed)) { | |
1703 | mapping = list_first_entry(&vm->freed, | |
1704 | struct amdgpu_bo_va_mapping, list); | |
1705 | list_del(&mapping->list); | |
e17841b9 | 1706 | |
4584312d | 1707 | if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START) |
6d16dac8 | 1708 | init_pte_value = AMDGPU_PTE_DEFAULT_ATC; |
51ac7eec | 1709 | |
570144c6 | 1710 | r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm, |
fc6aa33d | 1711 | mapping->start, mapping->last, |
51ac7eec | 1712 | init_pte_value, 0, &f); |
f3467818 | 1713 | amdgpu_vm_free_mapping(adev, vm, mapping, f); |
284710fa | 1714 | if (r) { |
f3467818 | 1715 | dma_fence_put(f); |
d38ceaf9 | 1716 | return r; |
284710fa | 1717 | } |
f3467818 | 1718 | } |
d38ceaf9 | 1719 | |
f3467818 NH |
1720 | if (fence && f) { |
1721 | dma_fence_put(*fence); | |
1722 | *fence = f; | |
1723 | } else { | |
1724 | dma_fence_put(f); | |
d38ceaf9 | 1725 | } |
f3467818 | 1726 | |
d38ceaf9 AD |
1727 | return 0; |
1728 | ||
1729 | } | |
1730 | ||
1731 | /** | |
73fb16e7 | 1732 | * amdgpu_vm_handle_moved - handle moved BOs in the PT |
d38ceaf9 AD |
1733 | * |
1734 | * @adev: amdgpu_device pointer | |
1735 | * @vm: requested vm | |
73fb16e7 | 1736 | * @sync: sync object to add fences to |
d38ceaf9 | 1737 | * |
73fb16e7 | 1738 | * Make sure all BOs which are moved are updated in the PTs. |
d38ceaf9 AD |
1739 | * Returns 0 for success. |
1740 | * | |
73fb16e7 | 1741 | * PTs have to be reserved! |
d38ceaf9 | 1742 | */ |
73fb16e7 | 1743 | int amdgpu_vm_handle_moved(struct amdgpu_device *adev, |
4e55eb38 | 1744 | struct amdgpu_vm *vm) |
d38ceaf9 | 1745 | { |
73fb16e7 | 1746 | bool clear; |
91e1a520 | 1747 | int r = 0; |
d38ceaf9 AD |
1748 | |
1749 | spin_lock(&vm->status_lock); | |
27c7b9ae | 1750 | while (!list_empty(&vm->moved)) { |
4e55eb38 | 1751 | struct amdgpu_bo_va *bo_va; |
ec363e0d | 1752 | struct reservation_object *resv; |
4e55eb38 | 1753 | |
27c7b9ae | 1754 | bo_va = list_first_entry(&vm->moved, |
ec681545 | 1755 | struct amdgpu_bo_va, base.vm_status); |
d38ceaf9 | 1756 | spin_unlock(&vm->status_lock); |
32b41ac2 | 1757 | |
ec363e0d CK |
1758 | resv = bo_va->base.bo->tbo.resv; |
1759 | ||
73fb16e7 | 1760 | /* Per VM BOs never need to bo cleared in the page tables */ |
ec363e0d CK |
1761 | if (resv == vm->root.base.bo->tbo.resv) |
1762 | clear = false; | |
1763 | /* Try to reserve the BO to avoid clearing its ptes */ | |
9b8cad20 | 1764 | else if (!amdgpu_vm_debug && reservation_object_trylock(resv)) |
ec363e0d CK |
1765 | clear = false; |
1766 | /* Somebody else is using the BO right now */ | |
1767 | else | |
1768 | clear = true; | |
73fb16e7 CK |
1769 | |
1770 | r = amdgpu_vm_bo_update(adev, bo_va, clear); | |
d38ceaf9 AD |
1771 | if (r) |
1772 | return r; | |
1773 | ||
ec363e0d CK |
1774 | if (!clear && resv != vm->root.base.bo->tbo.resv) |
1775 | reservation_object_unlock(resv); | |
1776 | ||
d38ceaf9 AD |
1777 | spin_lock(&vm->status_lock); |
1778 | } | |
1779 | spin_unlock(&vm->status_lock); | |
1780 | ||
91e1a520 | 1781 | return r; |
d38ceaf9 AD |
1782 | } |
1783 | ||
1784 | /** | |
1785 | * amdgpu_vm_bo_add - add a bo to a specific vm | |
1786 | * | |
1787 | * @adev: amdgpu_device pointer | |
1788 | * @vm: requested vm | |
1789 | * @bo: amdgpu buffer object | |
1790 | * | |
8843dbbb | 1791 | * Add @bo into the requested vm. |
d38ceaf9 AD |
1792 | * Add @bo to the list of bos associated with the vm |
1793 | * Returns newly added bo_va or NULL for failure | |
1794 | * | |
1795 | * Object has to be reserved! | |
1796 | */ | |
1797 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, | |
1798 | struct amdgpu_vm *vm, | |
1799 | struct amdgpu_bo *bo) | |
1800 | { | |
1801 | struct amdgpu_bo_va *bo_va; | |
1802 | ||
1803 | bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); | |
1804 | if (bo_va == NULL) { | |
1805 | return NULL; | |
1806 | } | |
ec681545 CK |
1807 | bo_va->base.vm = vm; |
1808 | bo_va->base.bo = bo; | |
1809 | INIT_LIST_HEAD(&bo_va->base.bo_list); | |
1810 | INIT_LIST_HEAD(&bo_va->base.vm_status); | |
1811 | ||
d38ceaf9 | 1812 | bo_va->ref_count = 1; |
7fc11959 CK |
1813 | INIT_LIST_HEAD(&bo_va->valids); |
1814 | INIT_LIST_HEAD(&bo_va->invalids); | |
32b41ac2 | 1815 | |
727ffdf2 CK |
1816 | if (!bo) |
1817 | return bo_va; | |
1818 | ||
1819 | list_add_tail(&bo_va->base.bo_list, &bo->va); | |
1820 | ||
1821 | if (bo->tbo.resv != vm->root.base.bo->tbo.resv) | |
1822 | return bo_va; | |
1823 | ||
1824 | if (bo->preferred_domains & | |
1825 | amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)) | |
1826 | return bo_va; | |
1827 | ||
1828 | /* | |
1829 | * We checked all the prerequisites, but it looks like this per VM BO | |
1830 | * is currently evicted. add the BO to the evicted list to make sure it | |
1831 | * is validated on next VM use to avoid fault. | |
1832 | * */ | |
1833 | spin_lock(&vm->status_lock); | |
1834 | list_move_tail(&bo_va->base.vm_status, &vm->evicted); | |
1835 | spin_unlock(&vm->status_lock); | |
d38ceaf9 AD |
1836 | |
1837 | return bo_va; | |
1838 | } | |
1839 | ||
73fb16e7 CK |
1840 | |
1841 | /** | |
1842 | * amdgpu_vm_bo_insert_mapping - insert a new mapping | |
1843 | * | |
1844 | * @adev: amdgpu_device pointer | |
1845 | * @bo_va: bo_va to store the address | |
1846 | * @mapping: the mapping to insert | |
1847 | * | |
1848 | * Insert a new mapping into all structures. | |
1849 | */ | |
1850 | static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, | |
1851 | struct amdgpu_bo_va *bo_va, | |
1852 | struct amdgpu_bo_va_mapping *mapping) | |
1853 | { | |
1854 | struct amdgpu_vm *vm = bo_va->base.vm; | |
1855 | struct amdgpu_bo *bo = bo_va->base.bo; | |
1856 | ||
aebc5e6f | 1857 | mapping->bo_va = bo_va; |
73fb16e7 CK |
1858 | list_add(&mapping->list, &bo_va->invalids); |
1859 | amdgpu_vm_it_insert(mapping, &vm->va); | |
1860 | ||
1861 | if (mapping->flags & AMDGPU_PTE_PRT) | |
1862 | amdgpu_vm_prt_get(adev); | |
1863 | ||
1864 | if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) { | |
1865 | spin_lock(&vm->status_lock); | |
481c2e94 CK |
1866 | if (list_empty(&bo_va->base.vm_status)) |
1867 | list_add(&bo_va->base.vm_status, &vm->moved); | |
73fb16e7 CK |
1868 | spin_unlock(&vm->status_lock); |
1869 | } | |
1870 | trace_amdgpu_vm_bo_map(bo_va, mapping); | |
1871 | } | |
1872 | ||
d38ceaf9 AD |
1873 | /** |
1874 | * amdgpu_vm_bo_map - map bo inside a vm | |
1875 | * | |
1876 | * @adev: amdgpu_device pointer | |
1877 | * @bo_va: bo_va to store the address | |
1878 | * @saddr: where to map the BO | |
1879 | * @offset: requested offset in the BO | |
1880 | * @flags: attributes of pages (read/write/valid/etc.) | |
1881 | * | |
1882 | * Add a mapping of the BO at the specefied addr into the VM. | |
1883 | * Returns 0 for success, error for failure. | |
1884 | * | |
49b02b18 | 1885 | * Object has to be reserved and unreserved outside! |
d38ceaf9 AD |
1886 | */ |
1887 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, | |
1888 | struct amdgpu_bo_va *bo_va, | |
1889 | uint64_t saddr, uint64_t offset, | |
268c3001 | 1890 | uint64_t size, uint64_t flags) |
d38ceaf9 | 1891 | { |
a9f87f64 | 1892 | struct amdgpu_bo_va_mapping *mapping, *tmp; |
ec681545 CK |
1893 | struct amdgpu_bo *bo = bo_va->base.bo; |
1894 | struct amdgpu_vm *vm = bo_va->base.vm; | |
d38ceaf9 | 1895 | uint64_t eaddr; |
d38ceaf9 | 1896 | |
0be52de9 CK |
1897 | /* validate the parameters */ |
1898 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || | |
49b02b18 | 1899 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
0be52de9 | 1900 | return -EINVAL; |
0be52de9 | 1901 | |
d38ceaf9 | 1902 | /* make sure object fit at this offset */ |
005ae95e | 1903 | eaddr = saddr + size - 1; |
a5f6b5b1 | 1904 | if (saddr >= eaddr || |
ec681545 | 1905 | (bo && offset + size > amdgpu_bo_size(bo))) |
d38ceaf9 | 1906 | return -EINVAL; |
d38ceaf9 | 1907 | |
d38ceaf9 AD |
1908 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
1909 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
1910 | ||
a9f87f64 CK |
1911 | tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); |
1912 | if (tmp) { | |
d38ceaf9 AD |
1913 | /* bo and tmp overlap, invalid addr */ |
1914 | dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " | |
ec681545 | 1915 | "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr, |
a9f87f64 | 1916 | tmp->start, tmp->last + 1); |
663e4577 | 1917 | return -EINVAL; |
d38ceaf9 AD |
1918 | } |
1919 | ||
1920 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); | |
663e4577 CK |
1921 | if (!mapping) |
1922 | return -ENOMEM; | |
d38ceaf9 | 1923 | |
a9f87f64 CK |
1924 | mapping->start = saddr; |
1925 | mapping->last = eaddr; | |
d38ceaf9 AD |
1926 | mapping->offset = offset; |
1927 | mapping->flags = flags; | |
1928 | ||
73fb16e7 | 1929 | amdgpu_vm_bo_insert_map(adev, bo_va, mapping); |
80f95c57 CK |
1930 | |
1931 | return 0; | |
1932 | } | |
1933 | ||
1934 | /** | |
1935 | * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings | |
1936 | * | |
1937 | * @adev: amdgpu_device pointer | |
1938 | * @bo_va: bo_va to store the address | |
1939 | * @saddr: where to map the BO | |
1940 | * @offset: requested offset in the BO | |
1941 | * @flags: attributes of pages (read/write/valid/etc.) | |
1942 | * | |
1943 | * Add a mapping of the BO at the specefied addr into the VM. Replace existing | |
1944 | * mappings as we do so. | |
1945 | * Returns 0 for success, error for failure. | |
1946 | * | |
1947 | * Object has to be reserved and unreserved outside! | |
1948 | */ | |
1949 | int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, | |
1950 | struct amdgpu_bo_va *bo_va, | |
1951 | uint64_t saddr, uint64_t offset, | |
1952 | uint64_t size, uint64_t flags) | |
1953 | { | |
1954 | struct amdgpu_bo_va_mapping *mapping; | |
ec681545 | 1955 | struct amdgpu_bo *bo = bo_va->base.bo; |
80f95c57 CK |
1956 | uint64_t eaddr; |
1957 | int r; | |
1958 | ||
1959 | /* validate the parameters */ | |
1960 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || | |
1961 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) | |
1962 | return -EINVAL; | |
1963 | ||
1964 | /* make sure object fit at this offset */ | |
1965 | eaddr = saddr + size - 1; | |
1966 | if (saddr >= eaddr || | |
ec681545 | 1967 | (bo && offset + size > amdgpu_bo_size(bo))) |
80f95c57 CK |
1968 | return -EINVAL; |
1969 | ||
1970 | /* Allocate all the needed memory */ | |
1971 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); | |
1972 | if (!mapping) | |
1973 | return -ENOMEM; | |
1974 | ||
ec681545 | 1975 | r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size); |
80f95c57 CK |
1976 | if (r) { |
1977 | kfree(mapping); | |
1978 | return r; | |
1979 | } | |
1980 | ||
1981 | saddr /= AMDGPU_GPU_PAGE_SIZE; | |
1982 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
1983 | ||
a9f87f64 CK |
1984 | mapping->start = saddr; |
1985 | mapping->last = eaddr; | |
80f95c57 CK |
1986 | mapping->offset = offset; |
1987 | mapping->flags = flags; | |
1988 | ||
73fb16e7 | 1989 | amdgpu_vm_bo_insert_map(adev, bo_va, mapping); |
4388fc2a | 1990 | |
d38ceaf9 | 1991 | return 0; |
d38ceaf9 AD |
1992 | } |
1993 | ||
1994 | /** | |
1995 | * amdgpu_vm_bo_unmap - remove bo mapping from vm | |
1996 | * | |
1997 | * @adev: amdgpu_device pointer | |
1998 | * @bo_va: bo_va to remove the address from | |
1999 | * @saddr: where to the BO is mapped | |
2000 | * | |
2001 | * Remove a mapping of the BO at the specefied addr from the VM. | |
2002 | * Returns 0 for success, error for failure. | |
2003 | * | |
49b02b18 | 2004 | * Object has to be reserved and unreserved outside! |
d38ceaf9 AD |
2005 | */ |
2006 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, | |
2007 | struct amdgpu_bo_va *bo_va, | |
2008 | uint64_t saddr) | |
2009 | { | |
2010 | struct amdgpu_bo_va_mapping *mapping; | |
ec681545 | 2011 | struct amdgpu_vm *vm = bo_va->base.vm; |
7fc11959 | 2012 | bool valid = true; |
d38ceaf9 | 2013 | |
6c7fc503 | 2014 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
32b41ac2 | 2015 | |
7fc11959 | 2016 | list_for_each_entry(mapping, &bo_va->valids, list) { |
a9f87f64 | 2017 | if (mapping->start == saddr) |
d38ceaf9 AD |
2018 | break; |
2019 | } | |
2020 | ||
7fc11959 CK |
2021 | if (&mapping->list == &bo_va->valids) { |
2022 | valid = false; | |
2023 | ||
2024 | list_for_each_entry(mapping, &bo_va->invalids, list) { | |
a9f87f64 | 2025 | if (mapping->start == saddr) |
7fc11959 CK |
2026 | break; |
2027 | } | |
2028 | ||
32b41ac2 | 2029 | if (&mapping->list == &bo_va->invalids) |
7fc11959 | 2030 | return -ENOENT; |
d38ceaf9 | 2031 | } |
32b41ac2 | 2032 | |
d38ceaf9 | 2033 | list_del(&mapping->list); |
a9f87f64 | 2034 | amdgpu_vm_it_remove(mapping, &vm->va); |
aebc5e6f | 2035 | mapping->bo_va = NULL; |
93e3e438 | 2036 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
d38ceaf9 | 2037 | |
e17841b9 | 2038 | if (valid) |
d38ceaf9 | 2039 | list_add(&mapping->list, &vm->freed); |
e17841b9 | 2040 | else |
284710fa CK |
2041 | amdgpu_vm_free_mapping(adev, vm, mapping, |
2042 | bo_va->last_pt_update); | |
d38ceaf9 AD |
2043 | |
2044 | return 0; | |
2045 | } | |
2046 | ||
dc54d3d1 CK |
2047 | /** |
2048 | * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range | |
2049 | * | |
2050 | * @adev: amdgpu_device pointer | |
2051 | * @vm: VM structure to use | |
2052 | * @saddr: start of the range | |
2053 | * @size: size of the range | |
2054 | * | |
2055 | * Remove all mappings in a range, split them as appropriate. | |
2056 | * Returns 0 for success, error for failure. | |
2057 | */ | |
2058 | int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, | |
2059 | struct amdgpu_vm *vm, | |
2060 | uint64_t saddr, uint64_t size) | |
2061 | { | |
2062 | struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; | |
dc54d3d1 CK |
2063 | LIST_HEAD(removed); |
2064 | uint64_t eaddr; | |
2065 | ||
2066 | eaddr = saddr + size - 1; | |
2067 | saddr /= AMDGPU_GPU_PAGE_SIZE; | |
2068 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
2069 | ||
2070 | /* Allocate all the needed memory */ | |
2071 | before = kzalloc(sizeof(*before), GFP_KERNEL); | |
2072 | if (!before) | |
2073 | return -ENOMEM; | |
27f6d610 | 2074 | INIT_LIST_HEAD(&before->list); |
dc54d3d1 CK |
2075 | |
2076 | after = kzalloc(sizeof(*after), GFP_KERNEL); | |
2077 | if (!after) { | |
2078 | kfree(before); | |
2079 | return -ENOMEM; | |
2080 | } | |
27f6d610 | 2081 | INIT_LIST_HEAD(&after->list); |
dc54d3d1 CK |
2082 | |
2083 | /* Now gather all removed mappings */ | |
a9f87f64 CK |
2084 | tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); |
2085 | while (tmp) { | |
dc54d3d1 | 2086 | /* Remember mapping split at the start */ |
a9f87f64 CK |
2087 | if (tmp->start < saddr) { |
2088 | before->start = tmp->start; | |
2089 | before->last = saddr - 1; | |
dc54d3d1 CK |
2090 | before->offset = tmp->offset; |
2091 | before->flags = tmp->flags; | |
2092 | list_add(&before->list, &tmp->list); | |
2093 | } | |
2094 | ||
2095 | /* Remember mapping split at the end */ | |
a9f87f64 CK |
2096 | if (tmp->last > eaddr) { |
2097 | after->start = eaddr + 1; | |
2098 | after->last = tmp->last; | |
dc54d3d1 | 2099 | after->offset = tmp->offset; |
a9f87f64 | 2100 | after->offset += after->start - tmp->start; |
dc54d3d1 CK |
2101 | after->flags = tmp->flags; |
2102 | list_add(&after->list, &tmp->list); | |
2103 | } | |
2104 | ||
2105 | list_del(&tmp->list); | |
2106 | list_add(&tmp->list, &removed); | |
a9f87f64 CK |
2107 | |
2108 | tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr); | |
dc54d3d1 CK |
2109 | } |
2110 | ||
2111 | /* And free them up */ | |
2112 | list_for_each_entry_safe(tmp, next, &removed, list) { | |
a9f87f64 | 2113 | amdgpu_vm_it_remove(tmp, &vm->va); |
dc54d3d1 CK |
2114 | list_del(&tmp->list); |
2115 | ||
a9f87f64 CK |
2116 | if (tmp->start < saddr) |
2117 | tmp->start = saddr; | |
2118 | if (tmp->last > eaddr) | |
2119 | tmp->last = eaddr; | |
dc54d3d1 | 2120 | |
aebc5e6f | 2121 | tmp->bo_va = NULL; |
dc54d3d1 CK |
2122 | list_add(&tmp->list, &vm->freed); |
2123 | trace_amdgpu_vm_bo_unmap(NULL, tmp); | |
2124 | } | |
2125 | ||
27f6d610 JZ |
2126 | /* Insert partial mapping before the range */ |
2127 | if (!list_empty(&before->list)) { | |
a9f87f64 | 2128 | amdgpu_vm_it_insert(before, &vm->va); |
dc54d3d1 CK |
2129 | if (before->flags & AMDGPU_PTE_PRT) |
2130 | amdgpu_vm_prt_get(adev); | |
2131 | } else { | |
2132 | kfree(before); | |
2133 | } | |
2134 | ||
2135 | /* Insert partial mapping after the range */ | |
27f6d610 | 2136 | if (!list_empty(&after->list)) { |
a9f87f64 | 2137 | amdgpu_vm_it_insert(after, &vm->va); |
dc54d3d1 CK |
2138 | if (after->flags & AMDGPU_PTE_PRT) |
2139 | amdgpu_vm_prt_get(adev); | |
2140 | } else { | |
2141 | kfree(after); | |
2142 | } | |
2143 | ||
2144 | return 0; | |
2145 | } | |
2146 | ||
aebc5e6f CK |
2147 | /** |
2148 | * amdgpu_vm_bo_lookup_mapping - find mapping by address | |
2149 | * | |
2150 | * @vm: the requested VM | |
2151 | * | |
2152 | * Find a mapping by it's address. | |
2153 | */ | |
2154 | struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm, | |
2155 | uint64_t addr) | |
2156 | { | |
2157 | return amdgpu_vm_it_iter_first(&vm->va, addr, addr); | |
2158 | } | |
2159 | ||
d38ceaf9 AD |
2160 | /** |
2161 | * amdgpu_vm_bo_rmv - remove a bo to a specific vm | |
2162 | * | |
2163 | * @adev: amdgpu_device pointer | |
2164 | * @bo_va: requested bo_va | |
2165 | * | |
8843dbbb | 2166 | * Remove @bo_va->bo from the requested vm. |
d38ceaf9 AD |
2167 | * |
2168 | * Object have to be reserved! | |
2169 | */ | |
2170 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, | |
2171 | struct amdgpu_bo_va *bo_va) | |
2172 | { | |
2173 | struct amdgpu_bo_va_mapping *mapping, *next; | |
ec681545 | 2174 | struct amdgpu_vm *vm = bo_va->base.vm; |
d38ceaf9 | 2175 | |
ec681545 | 2176 | list_del(&bo_va->base.bo_list); |
d38ceaf9 | 2177 | |
d38ceaf9 | 2178 | spin_lock(&vm->status_lock); |
ec681545 | 2179 | list_del(&bo_va->base.vm_status); |
d38ceaf9 AD |
2180 | spin_unlock(&vm->status_lock); |
2181 | ||
7fc11959 | 2182 | list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { |
d38ceaf9 | 2183 | list_del(&mapping->list); |
a9f87f64 | 2184 | amdgpu_vm_it_remove(mapping, &vm->va); |
aebc5e6f | 2185 | mapping->bo_va = NULL; |
93e3e438 | 2186 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
7fc11959 CK |
2187 | list_add(&mapping->list, &vm->freed); |
2188 | } | |
2189 | list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { | |
2190 | list_del(&mapping->list); | |
a9f87f64 | 2191 | amdgpu_vm_it_remove(mapping, &vm->va); |
284710fa CK |
2192 | amdgpu_vm_free_mapping(adev, vm, mapping, |
2193 | bo_va->last_pt_update); | |
d38ceaf9 | 2194 | } |
32b41ac2 | 2195 | |
f54d1867 | 2196 | dma_fence_put(bo_va->last_pt_update); |
d38ceaf9 | 2197 | kfree(bo_va); |
d38ceaf9 AD |
2198 | } |
2199 | ||
2200 | /** | |
2201 | * amdgpu_vm_bo_invalidate - mark the bo as invalid | |
2202 | * | |
2203 | * @adev: amdgpu_device pointer | |
2204 | * @vm: requested vm | |
2205 | * @bo: amdgpu buffer object | |
2206 | * | |
8843dbbb | 2207 | * Mark @bo as invalid. |
d38ceaf9 AD |
2208 | */ |
2209 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, | |
3f3333f8 | 2210 | struct amdgpu_bo *bo, bool evicted) |
d38ceaf9 | 2211 | { |
ec681545 CK |
2212 | struct amdgpu_vm_bo_base *bo_base; |
2213 | ||
2214 | list_for_each_entry(bo_base, &bo->va, bo_list) { | |
3f3333f8 CK |
2215 | struct amdgpu_vm *vm = bo_base->vm; |
2216 | ||
3d7d4d3a | 2217 | bo_base->moved = true; |
3f3333f8 CK |
2218 | if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) { |
2219 | spin_lock(&bo_base->vm->status_lock); | |
73fb16e7 CK |
2220 | if (bo->tbo.type == ttm_bo_type_kernel) |
2221 | list_move(&bo_base->vm_status, &vm->evicted); | |
2222 | else | |
2223 | list_move_tail(&bo_base->vm_status, | |
2224 | &vm->evicted); | |
3f3333f8 CK |
2225 | spin_unlock(&bo_base->vm->status_lock); |
2226 | continue; | |
2227 | } | |
2228 | ||
ea09729c CK |
2229 | if (bo->tbo.type == ttm_bo_type_kernel) { |
2230 | spin_lock(&bo_base->vm->status_lock); | |
2231 | if (list_empty(&bo_base->vm_status)) | |
2232 | list_add(&bo_base->vm_status, &vm->relocated); | |
2233 | spin_unlock(&bo_base->vm->status_lock); | |
3f3333f8 | 2234 | continue; |
ea09729c | 2235 | } |
3f3333f8 | 2236 | |
ec681545 CK |
2237 | spin_lock(&bo_base->vm->status_lock); |
2238 | if (list_empty(&bo_base->vm_status)) | |
481c2e94 | 2239 | list_add(&bo_base->vm_status, &vm->moved); |
ec681545 | 2240 | spin_unlock(&bo_base->vm->status_lock); |
d38ceaf9 AD |
2241 | } |
2242 | } | |
2243 | ||
bab4fee7 JZ |
2244 | static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) |
2245 | { | |
2246 | /* Total bits covered by PD + PTs */ | |
2247 | unsigned bits = ilog2(vm_size) + 18; | |
2248 | ||
2249 | /* Make sure the PD is 4K in size up to 8GB address space. | |
2250 | Above that split equal between PD and PTs */ | |
2251 | if (vm_size <= 8) | |
2252 | return (bits - 9); | |
2253 | else | |
2254 | return ((bits + 3) / 2); | |
2255 | } | |
2256 | ||
d07f14be RH |
2257 | /** |
2258 | * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size | |
bab4fee7 JZ |
2259 | * |
2260 | * @adev: amdgpu_device pointer | |
2261 | * @vm_size: the default vm size if it's set auto | |
2262 | */ | |
fdd5faaa | 2263 | void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size, |
f3368128 CK |
2264 | uint32_t fragment_size_default, unsigned max_level, |
2265 | unsigned max_bits) | |
bab4fee7 | 2266 | { |
36539dce CK |
2267 | uint64_t tmp; |
2268 | ||
2269 | /* adjust vm size first */ | |
f3368128 CK |
2270 | if (amdgpu_vm_size != -1) { |
2271 | unsigned max_size = 1 << (max_bits - 30); | |
2272 | ||
fdd5faaa | 2273 | vm_size = amdgpu_vm_size; |
f3368128 CK |
2274 | if (vm_size > max_size) { |
2275 | dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n", | |
2276 | amdgpu_vm_size, max_size); | |
2277 | vm_size = max_size; | |
2278 | } | |
2279 | } | |
fdd5faaa CK |
2280 | |
2281 | adev->vm_manager.max_pfn = (uint64_t)vm_size << 18; | |
36539dce CK |
2282 | |
2283 | tmp = roundup_pow_of_two(adev->vm_manager.max_pfn); | |
97489129 CK |
2284 | if (amdgpu_vm_block_size != -1) |
2285 | tmp >>= amdgpu_vm_block_size - 9; | |
36539dce CK |
2286 | tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1; |
2287 | adev->vm_manager.num_level = min(max_level, (unsigned)tmp); | |
196f7489 CZ |
2288 | switch (adev->vm_manager.num_level) { |
2289 | case 3: | |
2290 | adev->vm_manager.root_level = AMDGPU_VM_PDB2; | |
2291 | break; | |
2292 | case 2: | |
2293 | adev->vm_manager.root_level = AMDGPU_VM_PDB1; | |
2294 | break; | |
2295 | case 1: | |
2296 | adev->vm_manager.root_level = AMDGPU_VM_PDB0; | |
2297 | break; | |
2298 | default: | |
2299 | dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n"); | |
2300 | } | |
b38f41eb | 2301 | /* block size depends on vm size and hw setup*/ |
97489129 | 2302 | if (amdgpu_vm_block_size != -1) |
bab4fee7 | 2303 | adev->vm_manager.block_size = |
97489129 CK |
2304 | min((unsigned)amdgpu_vm_block_size, max_bits |
2305 | - AMDGPU_GPU_PAGE_SHIFT | |
2306 | - 9 * adev->vm_manager.num_level); | |
2307 | else if (adev->vm_manager.num_level > 1) | |
2308 | adev->vm_manager.block_size = 9; | |
bab4fee7 | 2309 | else |
97489129 | 2310 | adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp); |
bab4fee7 | 2311 | |
b38f41eb CK |
2312 | if (amdgpu_vm_fragment_size == -1) |
2313 | adev->vm_manager.fragment_size = fragment_size_default; | |
2314 | else | |
2315 | adev->vm_manager.fragment_size = amdgpu_vm_fragment_size; | |
d07f14be | 2316 | |
36539dce CK |
2317 | DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n", |
2318 | vm_size, adev->vm_manager.num_level + 1, | |
2319 | adev->vm_manager.block_size, | |
fdd5faaa | 2320 | adev->vm_manager.fragment_size); |
bab4fee7 JZ |
2321 | } |
2322 | ||
d38ceaf9 AD |
2323 | /** |
2324 | * amdgpu_vm_init - initialize a vm instance | |
2325 | * | |
2326 | * @adev: amdgpu_device pointer | |
2327 | * @vm: requested vm | |
9a4b7d4c | 2328 | * @vm_context: Indicates if it GFX or Compute context |
d38ceaf9 | 2329 | * |
8843dbbb | 2330 | * Init @vm fields. |
d38ceaf9 | 2331 | */ |
9a4b7d4c | 2332 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
02208441 | 2333 | int vm_context, unsigned int pasid) |
d38ceaf9 AD |
2334 | { |
2335 | const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE, | |
36b32a68 | 2336 | AMDGPU_VM_PTE_COUNT(adev) * 8); |
2d55e45a CK |
2337 | unsigned ring_instance; |
2338 | struct amdgpu_ring *ring; | |
1b1f42d8 | 2339 | struct drm_sched_rq *rq; |
d3aab672 | 2340 | unsigned long size; |
13307f7e | 2341 | uint64_t flags; |
36bbf3bf | 2342 | int r, i; |
d38ceaf9 | 2343 | |
f808c13f | 2344 | vm->va = RB_ROOT_CACHED; |
36bbf3bf CZ |
2345 | for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) |
2346 | vm->reserved_vmid[i] = NULL; | |
d38ceaf9 | 2347 | spin_lock_init(&vm->status_lock); |
3f3333f8 | 2348 | INIT_LIST_HEAD(&vm->evicted); |
ea09729c | 2349 | INIT_LIST_HEAD(&vm->relocated); |
27c7b9ae | 2350 | INIT_LIST_HEAD(&vm->moved); |
d38ceaf9 | 2351 | INIT_LIST_HEAD(&vm->freed); |
20250215 | 2352 | |
2bd9ccfa | 2353 | /* create scheduler entity for page table updates */ |
2d55e45a CK |
2354 | |
2355 | ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring); | |
2356 | ring_instance %= adev->vm_manager.vm_pte_num_rings; | |
2357 | ring = adev->vm_manager.vm_pte_rings[ring_instance]; | |
1b1f42d8 LS |
2358 | rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL]; |
2359 | r = drm_sched_entity_init(&ring->sched, &vm->entity, | |
b3eebe3d | 2360 | rq, amdgpu_sched_jobs, NULL); |
2bd9ccfa | 2361 | if (r) |
f566ceb1 | 2362 | return r; |
2bd9ccfa | 2363 | |
51ac7eec YZ |
2364 | vm->pte_support_ats = false; |
2365 | ||
2366 | if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) { | |
9a4b7d4c HK |
2367 | vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & |
2368 | AMDGPU_VM_USE_CPU_FOR_COMPUTE); | |
51ac7eec | 2369 | |
13307f7e | 2370 | if (adev->asic_type == CHIP_RAVEN) |
51ac7eec | 2371 | vm->pte_support_ats = true; |
13307f7e | 2372 | } else { |
9a4b7d4c HK |
2373 | vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & |
2374 | AMDGPU_VM_USE_CPU_FOR_GFX); | |
13307f7e | 2375 | } |
9a4b7d4c HK |
2376 | DRM_DEBUG_DRIVER("VM update mode is %s\n", |
2377 | vm->use_cpu_for_update ? "CPU" : "SDMA"); | |
2378 | WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)), | |
2379 | "CPU update of VM recommended only for large BAR system\n"); | |
d5884513 | 2380 | vm->last_update = NULL; |
05906dec | 2381 | |
13307f7e | 2382 | flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; |
3c824172 HK |
2383 | if (vm->use_cpu_for_update) |
2384 | flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; | |
2385 | else | |
2386 | flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS | | |
2387 | AMDGPU_GEM_CREATE_SHADOW); | |
2388 | ||
d3aab672 CK |
2389 | size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level); |
2390 | r = amdgpu_bo_create(adev, size, align, true, AMDGPU_GEM_DOMAIN_VRAM, | |
8febe617 | 2391 | flags, NULL, NULL, &vm->root.base.bo); |
d38ceaf9 | 2392 | if (r) |
2bd9ccfa CK |
2393 | goto error_free_sched_entity; |
2394 | ||
d3aab672 CK |
2395 | r = amdgpu_bo_reserve(vm->root.base.bo, true); |
2396 | if (r) | |
2397 | goto error_free_root; | |
2398 | ||
13307f7e | 2399 | r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo, |
4584312d CK |
2400 | adev->vm_manager.root_level, |
2401 | vm->pte_support_ats); | |
13307f7e CK |
2402 | if (r) |
2403 | goto error_unreserve; | |
2404 | ||
3f3333f8 CK |
2405 | vm->root.base.vm = vm; |
2406 | list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va); | |
d3aab672 CK |
2407 | list_add_tail(&vm->root.base.vm_status, &vm->evicted); |
2408 | amdgpu_bo_unreserve(vm->root.base.bo); | |
d38ceaf9 | 2409 | |
02208441 FK |
2410 | if (pasid) { |
2411 | unsigned long flags; | |
2412 | ||
2413 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); | |
2414 | r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1, | |
2415 | GFP_ATOMIC); | |
2416 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); | |
2417 | if (r < 0) | |
2418 | goto error_free_root; | |
2419 | ||
2420 | vm->pasid = pasid; | |
0a096fb6 CK |
2421 | } |
2422 | ||
a2f14820 | 2423 | INIT_KFIFO(vm->faults); |
c98171cc | 2424 | vm->fault_credit = 16; |
d38ceaf9 AD |
2425 | |
2426 | return 0; | |
2bd9ccfa | 2427 | |
13307f7e CK |
2428 | error_unreserve: |
2429 | amdgpu_bo_unreserve(vm->root.base.bo); | |
2430 | ||
67003a15 | 2431 | error_free_root: |
3f3333f8 CK |
2432 | amdgpu_bo_unref(&vm->root.base.bo->shadow); |
2433 | amdgpu_bo_unref(&vm->root.base.bo); | |
2434 | vm->root.base.bo = NULL; | |
2bd9ccfa CK |
2435 | |
2436 | error_free_sched_entity: | |
1b1f42d8 | 2437 | drm_sched_entity_fini(&ring->sched, &vm->entity); |
2bd9ccfa CK |
2438 | |
2439 | return r; | |
d38ceaf9 AD |
2440 | } |
2441 | ||
f566ceb1 CK |
2442 | /** |
2443 | * amdgpu_vm_free_levels - free PD/PT levels | |
2444 | * | |
8f19cd78 CK |
2445 | * @adev: amdgpu device structure |
2446 | * @parent: PD/PT starting level to free | |
2447 | * @level: level of parent structure | |
f566ceb1 CK |
2448 | * |
2449 | * Free the page directory or page table level and all sub levels. | |
2450 | */ | |
8f19cd78 CK |
2451 | static void amdgpu_vm_free_levels(struct amdgpu_device *adev, |
2452 | struct amdgpu_vm_pt *parent, | |
2453 | unsigned level) | |
f566ceb1 | 2454 | { |
8f19cd78 | 2455 | unsigned i, num_entries = amdgpu_vm_num_entries(adev, level); |
f566ceb1 | 2456 | |
8f19cd78 CK |
2457 | if (parent->base.bo) { |
2458 | list_del(&parent->base.bo_list); | |
2459 | list_del(&parent->base.vm_status); | |
2460 | amdgpu_bo_unref(&parent->base.bo->shadow); | |
2461 | amdgpu_bo_unref(&parent->base.bo); | |
f566ceb1 CK |
2462 | } |
2463 | ||
8f19cd78 CK |
2464 | if (parent->entries) |
2465 | for (i = 0; i < num_entries; i++) | |
2466 | amdgpu_vm_free_levels(adev, &parent->entries[i], | |
2467 | level + 1); | |
f566ceb1 | 2468 | |
8f19cd78 | 2469 | kvfree(parent->entries); |
f566ceb1 CK |
2470 | } |
2471 | ||
d38ceaf9 AD |
2472 | /** |
2473 | * amdgpu_vm_fini - tear down a vm instance | |
2474 | * | |
2475 | * @adev: amdgpu_device pointer | |
2476 | * @vm: requested vm | |
2477 | * | |
8843dbbb | 2478 | * Tear down @vm. |
d38ceaf9 AD |
2479 | * Unbind the VM and remove all bos from the vm bo list |
2480 | */ | |
2481 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
2482 | { | |
2483 | struct amdgpu_bo_va_mapping *mapping, *tmp; | |
132f34e4 | 2484 | bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt; |
2642cf11 | 2485 | struct amdgpu_bo *root; |
a2f14820 | 2486 | u64 fault; |
2642cf11 | 2487 | int i, r; |
d38ceaf9 | 2488 | |
a2f14820 FK |
2489 | /* Clear pending page faults from IH when the VM is destroyed */ |
2490 | while (kfifo_get(&vm->faults, &fault)) | |
2491 | amdgpu_ih_clear_fault(adev, fault); | |
2492 | ||
02208441 FK |
2493 | if (vm->pasid) { |
2494 | unsigned long flags; | |
2495 | ||
2496 | spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); | |
2497 | idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); | |
2498 | spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); | |
2499 | } | |
2500 | ||
1b1f42d8 | 2501 | drm_sched_entity_fini(vm->entity.sched, &vm->entity); |
2bd9ccfa | 2502 | |
f808c13f | 2503 | if (!RB_EMPTY_ROOT(&vm->va.rb_root)) { |
d38ceaf9 AD |
2504 | dev_err(adev->dev, "still active bo inside vm\n"); |
2505 | } | |
f808c13f DB |
2506 | rbtree_postorder_for_each_entry_safe(mapping, tmp, |
2507 | &vm->va.rb_root, rb) { | |
d38ceaf9 | 2508 | list_del(&mapping->list); |
a9f87f64 | 2509 | amdgpu_vm_it_remove(mapping, &vm->va); |
d38ceaf9 AD |
2510 | kfree(mapping); |
2511 | } | |
2512 | list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { | |
4388fc2a | 2513 | if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { |
451bc8eb | 2514 | amdgpu_vm_prt_fini(adev, vm); |
4388fc2a | 2515 | prt_fini_needed = false; |
451bc8eb | 2516 | } |
284710fa | 2517 | |
d38ceaf9 | 2518 | list_del(&mapping->list); |
451bc8eb | 2519 | amdgpu_vm_free_mapping(adev, vm, mapping, NULL); |
d38ceaf9 AD |
2520 | } |
2521 | ||
2642cf11 CK |
2522 | root = amdgpu_bo_ref(vm->root.base.bo); |
2523 | r = amdgpu_bo_reserve(root, true); | |
2524 | if (r) { | |
2525 | dev_err(adev->dev, "Leaking page tables because BO reservation failed\n"); | |
2526 | } else { | |
196f7489 CZ |
2527 | amdgpu_vm_free_levels(adev, &vm->root, |
2528 | adev->vm_manager.root_level); | |
2642cf11 CK |
2529 | amdgpu_bo_unreserve(root); |
2530 | } | |
2531 | amdgpu_bo_unref(&root); | |
d5884513 | 2532 | dma_fence_put(vm->last_update); |
1e9ef26f | 2533 | for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) |
620f774f | 2534 | amdgpu_vmid_free_reserved(adev, vm, i); |
d38ceaf9 | 2535 | } |
ea89f8c9 | 2536 | |
c98171cc FK |
2537 | /** |
2538 | * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID | |
2539 | * | |
2540 | * @adev: amdgpu_device pointer | |
2541 | * @pasid: PASID do identify the VM | |
2542 | * | |
2543 | * This function is expected to be called in interrupt context. Returns | |
2544 | * true if there was fault credit, false otherwise | |
2545 | */ | |
2546 | bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev, | |
2547 | unsigned int pasid) | |
2548 | { | |
2549 | struct amdgpu_vm *vm; | |
2550 | ||
2551 | spin_lock(&adev->vm_manager.pasid_lock); | |
2552 | vm = idr_find(&adev->vm_manager.pasid_idr, pasid); | |
d958939a | 2553 | if (!vm) { |
c98171cc | 2554 | /* VM not found, can't track fault credit */ |
d958939a | 2555 | spin_unlock(&adev->vm_manager.pasid_lock); |
c98171cc | 2556 | return true; |
d958939a | 2557 | } |
c98171cc FK |
2558 | |
2559 | /* No lock needed. only accessed by IRQ handler */ | |
d958939a | 2560 | if (!vm->fault_credit) { |
c98171cc | 2561 | /* Too many faults in this VM */ |
d958939a | 2562 | spin_unlock(&adev->vm_manager.pasid_lock); |
c98171cc | 2563 | return false; |
d958939a | 2564 | } |
c98171cc FK |
2565 | |
2566 | vm->fault_credit--; | |
d958939a | 2567 | spin_unlock(&adev->vm_manager.pasid_lock); |
c98171cc FK |
2568 | return true; |
2569 | } | |
2570 | ||
a9a78b32 CK |
2571 | /** |
2572 | * amdgpu_vm_manager_init - init the VM manager | |
2573 | * | |
2574 | * @adev: amdgpu_device pointer | |
2575 | * | |
2576 | * Initialize the VM manager structures | |
2577 | */ | |
2578 | void amdgpu_vm_manager_init(struct amdgpu_device *adev) | |
2579 | { | |
620f774f | 2580 | unsigned i; |
a9a78b32 | 2581 | |
620f774f | 2582 | amdgpu_vmid_mgr_init(adev); |
2d55e45a | 2583 | |
f54d1867 CW |
2584 | adev->vm_manager.fence_context = |
2585 | dma_fence_context_alloc(AMDGPU_MAX_RINGS); | |
1fbb2e92 CK |
2586 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
2587 | adev->vm_manager.seqno[i] = 0; | |
2588 | ||
2d55e45a | 2589 | atomic_set(&adev->vm_manager.vm_pte_next_ring, 0); |
284710fa | 2590 | spin_lock_init(&adev->vm_manager.prt_lock); |
451bc8eb | 2591 | atomic_set(&adev->vm_manager.num_prt_users, 0); |
9a4b7d4c HK |
2592 | |
2593 | /* If not overridden by the user, by default, only in large BAR systems | |
2594 | * Compute VM tables will be updated by CPU | |
2595 | */ | |
2596 | #ifdef CONFIG_X86_64 | |
2597 | if (amdgpu_vm_update_mode == -1) { | |
2598 | if (amdgpu_vm_is_large_bar(adev)) | |
2599 | adev->vm_manager.vm_update_mode = | |
2600 | AMDGPU_VM_USE_CPU_FOR_COMPUTE; | |
2601 | else | |
2602 | adev->vm_manager.vm_update_mode = 0; | |
2603 | } else | |
2604 | adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode; | |
2605 | #else | |
2606 | adev->vm_manager.vm_update_mode = 0; | |
2607 | #endif | |
2608 | ||
02208441 FK |
2609 | idr_init(&adev->vm_manager.pasid_idr); |
2610 | spin_lock_init(&adev->vm_manager.pasid_lock); | |
a9a78b32 CK |
2611 | } |
2612 | ||
ea89f8c9 CK |
2613 | /** |
2614 | * amdgpu_vm_manager_fini - cleanup VM manager | |
2615 | * | |
2616 | * @adev: amdgpu_device pointer | |
2617 | * | |
2618 | * Cleanup the VM manager and free resources. | |
2619 | */ | |
2620 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev) | |
2621 | { | |
02208441 FK |
2622 | WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr)); |
2623 | idr_destroy(&adev->vm_manager.pasid_idr); | |
2624 | ||
620f774f | 2625 | amdgpu_vmid_mgr_fini(adev); |
ea89f8c9 | 2626 | } |
cfbcacf4 CZ |
2627 | |
2628 | int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) | |
2629 | { | |
2630 | union drm_amdgpu_vm *args = data; | |
1e9ef26f CZ |
2631 | struct amdgpu_device *adev = dev->dev_private; |
2632 | struct amdgpu_fpriv *fpriv = filp->driver_priv; | |
2633 | int r; | |
cfbcacf4 CZ |
2634 | |
2635 | switch (args->in.op) { | |
2636 | case AMDGPU_VM_OP_RESERVE_VMID: | |
1e9ef26f | 2637 | /* current, we only have requirement to reserve vmid from gfxhub */ |
620f774f | 2638 | r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB); |
1e9ef26f CZ |
2639 | if (r) |
2640 | return r; | |
2641 | break; | |
cfbcacf4 | 2642 | case AMDGPU_VM_OP_UNRESERVE_VMID: |
620f774f | 2643 | amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB); |
cfbcacf4 CZ |
2644 | break; |
2645 | default: | |
2646 | return -EINVAL; | |
2647 | } | |
2648 | ||
2649 | return 0; | |
2650 | } |