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