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