]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
drm/amdgpu: adjust HDP write queue flushing for tlb invalidation
[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_asic_flush_hdp(adev);
860                 amdgpu_gart_flush_gpu_tlb(adev, 0);
861         } else if (params.ib->length_dw == 0) {
862                 amdgpu_job_free(job);
863         } else {
864                 struct amdgpu_bo *root = vm->root.base.bo;
865                 struct amdgpu_ring *ring;
866                 struct dma_fence *fence;
867
868                 ring = container_of(vm->entity.sched, struct amdgpu_ring,
869                                     sched);
870
871                 amdgpu_ring_pad_ib(ring, params.ib);
872                 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
873                                  AMDGPU_FENCE_OWNER_VM, false);
874                 if (root->shadow)
875                         amdgpu_sync_resv(adev, &job->sync,
876                                          root->shadow->tbo.resv,
877                                          AMDGPU_FENCE_OWNER_VM, false);
878
879                 WARN_ON(params.ib->length_dw > ndw);
880                 r = amdgpu_job_submit(job, ring, &vm->entity,
881                                       AMDGPU_FENCE_OWNER_VM, &fence);
882                 if (r)
883                         goto error;
884
885                 amdgpu_bo_fence(root, fence, true);
886                 dma_fence_put(vm->last_update);
887                 vm->last_update = fence;
888         }
889
890         if (!list_empty(&vm->relocated))
891                 goto restart;
892
893         return 0;
894
895 error:
896         amdgpu_vm_invalidate_level(adev, vm, &vm->root,
897                                    adev->vm_manager.root_level);
898         amdgpu_job_free(job);
899         return r;
900 }
901
902 /**
903  * amdgpu_vm_find_entry - find the entry for an address
904  *
905  * @p: see amdgpu_pte_update_params definition
906  * @addr: virtual address in question
907  * @entry: resulting entry or NULL
908  * @parent: parent entry
909  *
910  * Find the vm_pt entry and it's parent for the given address.
911  */
912 void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
913                          struct amdgpu_vm_pt **entry,
914                          struct amdgpu_vm_pt **parent)
915 {
916         unsigned level = p->adev->vm_manager.root_level;
917
918         *parent = NULL;
919         *entry = &p->vm->root;
920         while ((*entry)->entries) {
921                 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
922
923                 *parent = *entry;
924                 *entry = &(*entry)->entries[addr >> shift];
925                 addr &= (1ULL << shift) - 1;
926         }
927
928         if (level != AMDGPU_VM_PTB)
929                 *entry = NULL;
930 }
931
932 /**
933  * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
934  *
935  * @p: see amdgpu_pte_update_params definition
936  * @entry: vm_pt entry to check
937  * @parent: parent entry
938  * @nptes: number of PTEs updated with this operation
939  * @dst: destination address where the PTEs should point to
940  * @flags: access flags fro the PTEs
941  *
942  * Check if we can update the PD with a huge page.
943  */
944 static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
945                                         struct amdgpu_vm_pt *entry,
946                                         struct amdgpu_vm_pt *parent,
947                                         unsigned nptes, uint64_t dst,
948                                         uint64_t flags)
949 {
950         uint64_t pd_addr, pde;
951
952         /* In the case of a mixed PT the PDE must point to it*/
953         if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
954             nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
955                 /* Set the huge page flag to stop scanning at this PDE */
956                 flags |= AMDGPU_PDE_PTE;
957         }
958
959         if (!(flags & AMDGPU_PDE_PTE)) {
960                 if (entry->huge) {
961                         /* Add the entry to the relocated list to update it. */
962                         entry->huge = false;
963                         spin_lock(&p->vm->status_lock);
964                         list_move(&entry->base.vm_status, &p->vm->relocated);
965                         spin_unlock(&p->vm->status_lock);
966                 }
967                 return;
968         }
969
970         entry->huge = true;
971         amdgpu_gart_get_vm_pde(p->adev, AMDGPU_VM_PDB0,
972                                &dst, &flags);
973
974         if (p->func == amdgpu_vm_cpu_set_ptes) {
975                 pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
976         } else {
977                 if (parent->base.bo->shadow) {
978                         pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
979                         pde = pd_addr + (entry - parent->entries) * 8;
980                         p->func(p, pde, dst, 1, 0, flags);
981                 }
982                 pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
983         }
984         pde = pd_addr + (entry - parent->entries) * 8;
985         p->func(p, pde, dst, 1, 0, flags);
986 }
987
988 /**
989  * amdgpu_vm_update_ptes - make sure that page tables are valid
990  *
991  * @params: see amdgpu_pte_update_params definition
992  * @vm: requested vm
993  * @start: start of GPU address range
994  * @end: end of GPU address range
995  * @dst: destination address to map to, the next dst inside the function
996  * @flags: mapping flags
997  *
998  * Update the page tables in the range @start - @end.
999  * Returns 0 for success, -EINVAL for failure.
1000  */
1001 static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
1002                                   uint64_t start, uint64_t end,
1003                                   uint64_t dst, uint64_t flags)
1004 {
1005         struct amdgpu_device *adev = params->adev;
1006         const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
1007
1008         uint64_t addr, pe_start;
1009         struct amdgpu_bo *pt;
1010         unsigned nptes;
1011         bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
1012
1013         /* walk over the address space and update the page tables */
1014         for (addr = start; addr < end; addr += nptes,
1015              dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1016                 struct amdgpu_vm_pt *entry, *parent;
1017
1018                 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1019                 if (!entry)
1020                         return -ENOENT;
1021
1022                 if ((addr & ~mask) == (end & ~mask))
1023                         nptes = end - addr;
1024                 else
1025                         nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
1026
1027                 amdgpu_vm_handle_huge_pages(params, entry, parent,
1028                                             nptes, dst, flags);
1029                 /* We don't need to update PTEs for huge pages */
1030                 if (entry->huge)
1031                         continue;
1032
1033                 pt = entry->base.bo;
1034                 if (use_cpu_update) {
1035                         pe_start = (unsigned long)amdgpu_bo_kptr(pt);
1036                 } else {
1037                         if (pt->shadow) {
1038                                 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1039                                 pe_start += (addr & mask) * 8;
1040                                 params->func(params, pe_start, dst, nptes,
1041                                              AMDGPU_GPU_PAGE_SIZE, flags);
1042                         }
1043                         pe_start = amdgpu_bo_gpu_offset(pt);
1044                 }
1045
1046                 pe_start += (addr & mask) * 8;
1047                 params->func(params, pe_start, dst, nptes,
1048                              AMDGPU_GPU_PAGE_SIZE, flags);
1049         }
1050
1051         return 0;
1052 }
1053
1054 /*
1055  * amdgpu_vm_frag_ptes - add fragment information to PTEs
1056  *
1057  * @params: see amdgpu_pte_update_params definition
1058  * @vm: requested vm
1059  * @start: first PTE to handle
1060  * @end: last PTE to handle
1061  * @dst: addr those PTEs should point to
1062  * @flags: hw mapping flags
1063  * Returns 0 for success, -EINVAL for failure.
1064  */
1065 static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params  *params,
1066                                 uint64_t start, uint64_t end,
1067                                 uint64_t dst, uint64_t flags)
1068 {
1069         /**
1070          * The MC L1 TLB supports variable sized pages, based on a fragment
1071          * field in the PTE. When this field is set to a non-zero value, page
1072          * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1073          * flags are considered valid for all PTEs within the fragment range
1074          * and corresponding mappings are assumed to be physically contiguous.
1075          *
1076          * The L1 TLB can store a single PTE for the whole fragment,
1077          * significantly increasing the space available for translation
1078          * caching. This leads to large improvements in throughput when the
1079          * TLB is under pressure.
1080          *
1081          * The L2 TLB distributes small and large fragments into two
1082          * asymmetric partitions. The large fragment cache is significantly
1083          * larger. Thus, we try to use large fragments wherever possible.
1084          * Userspace can support this by aligning virtual base address and
1085          * allocation size to the fragment size.
1086          */
1087         unsigned max_frag = params->adev->vm_manager.fragment_size;
1088         int r;
1089
1090         /* system pages are non continuously */
1091         if (params->src || !(flags & AMDGPU_PTE_VALID))
1092                 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
1093
1094         while (start != end) {
1095                 uint64_t frag_flags, frag_end;
1096                 unsigned frag;
1097
1098                 /* This intentionally wraps around if no bit is set */
1099                 frag = min((unsigned)ffs(start) - 1,
1100                            (unsigned)fls64(end - start) - 1);
1101                 if (frag >= max_frag) {
1102                         frag_flags = AMDGPU_PTE_FRAG(max_frag);
1103                         frag_end = end & ~((1ULL << max_frag) - 1);
1104                 } else {
1105                         frag_flags = AMDGPU_PTE_FRAG(frag);
1106                         frag_end = start + (1 << frag);
1107                 }
1108
1109                 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1110                                           flags | frag_flags);
1111                 if (r)
1112                         return r;
1113
1114                 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1115                 start = frag_end;
1116         }
1117
1118         return 0;
1119 }
1120
1121 /**
1122  * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1123  *
1124  * @adev: amdgpu_device pointer
1125  * @exclusive: fence we need to sync to
1126  * @pages_addr: DMA addresses to use for mapping
1127  * @vm: requested vm
1128  * @start: start of mapped range
1129  * @last: last mapped entry
1130  * @flags: flags for the entries
1131  * @addr: addr to set the area to
1132  * @fence: optional resulting fence
1133  *
1134  * Fill in the page table entries between @start and @last.
1135  * Returns 0 for success, -EINVAL for failure.
1136  */
1137 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
1138                                        struct dma_fence *exclusive,
1139                                        dma_addr_t *pages_addr,
1140                                        struct amdgpu_vm *vm,
1141                                        uint64_t start, uint64_t last,
1142                                        uint64_t flags, uint64_t addr,
1143                                        struct dma_fence **fence)
1144 {
1145         struct amdgpu_ring *ring;
1146         void *owner = AMDGPU_FENCE_OWNER_VM;
1147         unsigned nptes, ncmds, ndw;
1148         struct amdgpu_job *job;
1149         struct amdgpu_pte_update_params params;
1150         struct dma_fence *f = NULL;
1151         int r;
1152
1153         memset(&params, 0, sizeof(params));
1154         params.adev = adev;
1155         params.vm = vm;
1156
1157         /* sync to everything on unmapping */
1158         if (!(flags & AMDGPU_PTE_VALID))
1159                 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1160
1161         if (vm->use_cpu_for_update) {
1162                 /* params.src is used as flag to indicate system Memory */
1163                 if (pages_addr)
1164                         params.src = ~0;
1165
1166                 /* Wait for PT BOs to be free. PTs share the same resv. object
1167                  * as the root PD BO
1168                  */
1169                 r = amdgpu_vm_wait_pd(adev, vm, owner);
1170                 if (unlikely(r))
1171                         return r;
1172
1173                 params.func = amdgpu_vm_cpu_set_ptes;
1174                 params.pages_addr = pages_addr;
1175                 return amdgpu_vm_frag_ptes(&params, start, last + 1,
1176                                            addr, flags);
1177         }
1178
1179         ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
1180
1181         nptes = last - start + 1;
1182
1183         /*
1184          * reserve space for two commands every (1 << BLOCK_SIZE)
1185          *  entries or 2k dwords (whatever is smaller)
1186          *
1187          * The second command is for the shadow pagetables.
1188          */
1189         if (vm->root.base.bo->shadow)
1190                 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1191         else
1192                 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
1193
1194         /* padding, etc. */
1195         ndw = 64;
1196
1197         if (pages_addr) {
1198                 /* copy commands needed */
1199                 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
1200
1201                 /* and also PTEs */
1202                 ndw += nptes * 2;
1203
1204                 params.func = amdgpu_vm_do_copy_ptes;
1205
1206         } else {
1207                 /* set page commands needed */
1208                 ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
1209
1210                 /* extra commands for begin/end fragments */
1211                 ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
1212                                 * adev->vm_manager.fragment_size;
1213
1214                 params.func = amdgpu_vm_do_set_ptes;
1215         }
1216
1217         r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1218         if (r)
1219                 return r;
1220
1221         params.ib = &job->ibs[0];
1222
1223         if (pages_addr) {
1224                 uint64_t *pte;
1225                 unsigned i;
1226
1227                 /* Put the PTEs at the end of the IB. */
1228                 i = ndw - nptes * 2;
1229                 pte= (uint64_t *)&(job->ibs->ptr[i]);
1230                 params.src = job->ibs->gpu_addr + i * 4;
1231
1232                 for (i = 0; i < nptes; ++i) {
1233                         pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1234                                                     AMDGPU_GPU_PAGE_SIZE);
1235                         pte[i] |= flags;
1236                 }
1237                 addr = 0;
1238         }
1239
1240         r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
1241         if (r)
1242                 goto error_free;
1243
1244         r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
1245                              owner, false);
1246         if (r)
1247                 goto error_free;
1248
1249         r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
1250         if (r)
1251                 goto error_free;
1252
1253         r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
1254         if (r)
1255                 goto error_free;
1256
1257         amdgpu_ring_pad_ib(ring, params.ib);
1258         WARN_ON(params.ib->length_dw > ndw);
1259         r = amdgpu_job_submit(job, ring, &vm->entity,
1260                               AMDGPU_FENCE_OWNER_VM, &f);
1261         if (r)
1262                 goto error_free;
1263
1264         amdgpu_bo_fence(vm->root.base.bo, f, true);
1265         dma_fence_put(*fence);
1266         *fence = f;
1267         return 0;
1268
1269 error_free:
1270         amdgpu_job_free(job);
1271         return r;
1272 }
1273
1274 /**
1275  * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1276  *
1277  * @adev: amdgpu_device pointer
1278  * @exclusive: fence we need to sync to
1279  * @pages_addr: DMA addresses to use for mapping
1280  * @vm: requested vm
1281  * @mapping: mapped range and flags to use for the update
1282  * @flags: HW flags for the mapping
1283  * @nodes: array of drm_mm_nodes with the MC addresses
1284  * @fence: optional resulting fence
1285  *
1286  * Split the mapping into smaller chunks so that each update fits
1287  * into a SDMA IB.
1288  * Returns 0 for success, -EINVAL for failure.
1289  */
1290 static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
1291                                       struct dma_fence *exclusive,
1292                                       dma_addr_t *pages_addr,
1293                                       struct amdgpu_vm *vm,
1294                                       struct amdgpu_bo_va_mapping *mapping,
1295                                       uint64_t flags,
1296                                       struct drm_mm_node *nodes,
1297                                       struct dma_fence **fence)
1298 {
1299         unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
1300         uint64_t pfn, start = mapping->start;
1301         int r;
1302
1303         /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1304          * but in case of something, we filter the flags in first place
1305          */
1306         if (!(mapping->flags & AMDGPU_PTE_READABLE))
1307                 flags &= ~AMDGPU_PTE_READABLE;
1308         if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1309                 flags &= ~AMDGPU_PTE_WRITEABLE;
1310
1311         flags &= ~AMDGPU_PTE_EXECUTABLE;
1312         flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1313
1314         flags &= ~AMDGPU_PTE_MTYPE_MASK;
1315         flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1316
1317         if ((mapping->flags & AMDGPU_PTE_PRT) &&
1318             (adev->asic_type >= CHIP_VEGA10)) {
1319                 flags |= AMDGPU_PTE_PRT;
1320                 flags &= ~AMDGPU_PTE_VALID;
1321         }
1322
1323         trace_amdgpu_vm_bo_update(mapping);
1324
1325         pfn = mapping->offset >> PAGE_SHIFT;
1326         if (nodes) {
1327                 while (pfn >= nodes->size) {
1328                         pfn -= nodes->size;
1329                         ++nodes;
1330                 }
1331         }
1332
1333         do {
1334                 dma_addr_t *dma_addr = NULL;
1335                 uint64_t max_entries;
1336                 uint64_t addr, last;
1337
1338                 if (nodes) {
1339                         addr = nodes->start << PAGE_SHIFT;
1340                         max_entries = (nodes->size - pfn) *
1341                                 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1342                 } else {
1343                         addr = 0;
1344                         max_entries = S64_MAX;
1345                 }
1346
1347                 if (pages_addr) {
1348                         uint64_t count;
1349
1350                         max_entries = min(max_entries, 16ull * 1024ull);
1351                         for (count = 1; count < max_entries; ++count) {
1352                                 uint64_t idx = pfn + count;
1353
1354                                 if (pages_addr[idx] !=
1355                                     (pages_addr[idx - 1] + PAGE_SIZE))
1356                                         break;
1357                         }
1358
1359                         if (count < min_linear_pages) {
1360                                 addr = pfn << PAGE_SHIFT;
1361                                 dma_addr = pages_addr;
1362                         } else {
1363                                 addr = pages_addr[pfn];
1364                                 max_entries = count;
1365                         }
1366
1367                 } else if (flags & AMDGPU_PTE_VALID) {
1368                         addr += adev->vm_manager.vram_base_offset;
1369                         addr += pfn << PAGE_SHIFT;
1370                 }
1371
1372                 last = min((uint64_t)mapping->last, start + max_entries - 1);
1373                 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
1374                                                 start, last, flags, addr,
1375                                                 fence);
1376                 if (r)
1377                         return r;
1378
1379                 pfn += last - start + 1;
1380                 if (nodes && nodes->size == pfn) {
1381                         pfn = 0;
1382                         ++nodes;
1383                 }
1384                 start = last + 1;
1385
1386         } while (unlikely(start != mapping->last + 1));
1387
1388         return 0;
1389 }
1390
1391 /**
1392  * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1393  *
1394  * @adev: amdgpu_device pointer
1395  * @bo_va: requested BO and VM object
1396  * @clear: if true clear the entries
1397  *
1398  * Fill in the page table entries for @bo_va.
1399  * Returns 0 for success, -EINVAL for failure.
1400  */
1401 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1402                         struct amdgpu_bo_va *bo_va,
1403                         bool clear)
1404 {
1405         struct amdgpu_bo *bo = bo_va->base.bo;
1406         struct amdgpu_vm *vm = bo_va->base.vm;
1407         struct amdgpu_bo_va_mapping *mapping;
1408         dma_addr_t *pages_addr = NULL;
1409         struct ttm_mem_reg *mem;
1410         struct drm_mm_node *nodes;
1411         struct dma_fence *exclusive, **last_update;
1412         uint64_t flags;
1413         int r;
1414
1415         if (clear || !bo_va->base.bo) {
1416                 mem = NULL;
1417                 nodes = NULL;
1418                 exclusive = NULL;
1419         } else {
1420                 struct ttm_dma_tt *ttm;
1421
1422                 mem = &bo_va->base.bo->tbo.mem;
1423                 nodes = mem->mm_node;
1424                 if (mem->mem_type == TTM_PL_TT) {
1425                         ttm = container_of(bo_va->base.bo->tbo.ttm,
1426                                            struct ttm_dma_tt, ttm);
1427                         pages_addr = ttm->dma_address;
1428                 }
1429                 exclusive = reservation_object_get_excl(bo->tbo.resv);
1430         }
1431
1432         if (bo)
1433                 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
1434         else
1435                 flags = 0x0;
1436
1437         if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1438                 last_update = &vm->last_update;
1439         else
1440                 last_update = &bo_va->last_pt_update;
1441
1442         if (!clear && bo_va->base.moved) {
1443                 bo_va->base.moved = false;
1444                 list_splice_init(&bo_va->valids, &bo_va->invalids);
1445
1446         } else if (bo_va->cleared != clear) {
1447                 list_splice_init(&bo_va->valids, &bo_va->invalids);
1448         }
1449
1450         list_for_each_entry(mapping, &bo_va->invalids, list) {
1451                 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
1452                                                mapping, flags, nodes,
1453                                                last_update);
1454                 if (r)
1455                         return r;
1456         }
1457
1458         if (vm->use_cpu_for_update) {
1459                 /* Flush HDP */
1460                 mb();
1461                 amdgpu_asic_flush_hdp(adev);
1462                 amdgpu_gart_flush_gpu_tlb(adev, 0);
1463         }
1464
1465         spin_lock(&vm->status_lock);
1466         list_del_init(&bo_va->base.vm_status);
1467         spin_unlock(&vm->status_lock);
1468
1469         list_splice_init(&bo_va->invalids, &bo_va->valids);
1470         bo_va->cleared = clear;
1471
1472         if (trace_amdgpu_vm_bo_mapping_enabled()) {
1473                 list_for_each_entry(mapping, &bo_va->valids, list)
1474                         trace_amdgpu_vm_bo_mapping(mapping);
1475         }
1476
1477         return 0;
1478 }
1479
1480 /**
1481  * amdgpu_vm_update_prt_state - update the global PRT state
1482  */
1483 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1484 {
1485         unsigned long flags;
1486         bool enable;
1487
1488         spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1489         enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1490         adev->gart.gart_funcs->set_prt(adev, enable);
1491         spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1492 }
1493
1494 /**
1495  * amdgpu_vm_prt_get - add a PRT user
1496  */
1497 static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1498 {
1499         if (!adev->gart.gart_funcs->set_prt)
1500                 return;
1501
1502         if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1503                 amdgpu_vm_update_prt_state(adev);
1504 }
1505
1506 /**
1507  * amdgpu_vm_prt_put - drop a PRT user
1508  */
1509 static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1510 {
1511         if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1512                 amdgpu_vm_update_prt_state(adev);
1513 }
1514
1515 /**
1516  * amdgpu_vm_prt_cb - callback for updating the PRT status
1517  */
1518 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1519 {
1520         struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1521
1522         amdgpu_vm_prt_put(cb->adev);
1523         kfree(cb);
1524 }
1525
1526 /**
1527  * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1528  */
1529 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1530                                  struct dma_fence *fence)
1531 {
1532         struct amdgpu_prt_cb *cb;
1533
1534         if (!adev->gart.gart_funcs->set_prt)
1535                 return;
1536
1537         cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
1538         if (!cb) {
1539                 /* Last resort when we are OOM */
1540                 if (fence)
1541                         dma_fence_wait(fence, false);
1542
1543                 amdgpu_vm_prt_put(adev);
1544         } else {
1545                 cb->adev = adev;
1546                 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1547                                                      amdgpu_vm_prt_cb))
1548                         amdgpu_vm_prt_cb(fence, &cb->cb);
1549         }
1550 }
1551
1552 /**
1553  * amdgpu_vm_free_mapping - free a mapping
1554  *
1555  * @adev: amdgpu_device pointer
1556  * @vm: requested vm
1557  * @mapping: mapping to be freed
1558  * @fence: fence of the unmap operation
1559  *
1560  * Free a mapping and make sure we decrease the PRT usage count if applicable.
1561  */
1562 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1563                                    struct amdgpu_vm *vm,
1564                                    struct amdgpu_bo_va_mapping *mapping,
1565                                    struct dma_fence *fence)
1566 {
1567         if (mapping->flags & AMDGPU_PTE_PRT)
1568                 amdgpu_vm_add_prt_cb(adev, fence);
1569         kfree(mapping);
1570 }
1571
1572 /**
1573  * amdgpu_vm_prt_fini - finish all prt mappings
1574  *
1575  * @adev: amdgpu_device pointer
1576  * @vm: requested vm
1577  *
1578  * Register a cleanup callback to disable PRT support after VM dies.
1579  */
1580 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1581 {
1582         struct reservation_object *resv = vm->root.base.bo->tbo.resv;
1583         struct dma_fence *excl, **shared;
1584         unsigned i, shared_count;
1585         int r;
1586
1587         r = reservation_object_get_fences_rcu(resv, &excl,
1588                                               &shared_count, &shared);
1589         if (r) {
1590                 /* Not enough memory to grab the fence list, as last resort
1591                  * block for all the fences to complete.
1592                  */
1593                 reservation_object_wait_timeout_rcu(resv, true, false,
1594                                                     MAX_SCHEDULE_TIMEOUT);
1595                 return;
1596         }
1597
1598         /* Add a callback for each fence in the reservation object */
1599         amdgpu_vm_prt_get(adev);
1600         amdgpu_vm_add_prt_cb(adev, excl);
1601
1602         for (i = 0; i < shared_count; ++i) {
1603                 amdgpu_vm_prt_get(adev);
1604                 amdgpu_vm_add_prt_cb(adev, shared[i]);
1605         }
1606
1607         kfree(shared);
1608 }
1609
1610 /**
1611  * amdgpu_vm_clear_freed - clear freed BOs in the PT
1612  *
1613  * @adev: amdgpu_device pointer
1614  * @vm: requested vm
1615  * @fence: optional resulting fence (unchanged if no work needed to be done
1616  * or if an error occurred)
1617  *
1618  * Make sure all freed BOs are cleared in the PT.
1619  * Returns 0 for success.
1620  *
1621  * PTs have to be reserved and mutex must be locked!
1622  */
1623 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1624                           struct amdgpu_vm *vm,
1625                           struct dma_fence **fence)
1626 {
1627         struct amdgpu_bo_va_mapping *mapping;
1628         struct dma_fence *f = NULL;
1629         int r;
1630         uint64_t init_pte_value = 0;
1631
1632         while (!list_empty(&vm->freed)) {
1633                 mapping = list_first_entry(&vm->freed,
1634                         struct amdgpu_bo_va_mapping, list);
1635                 list_del(&mapping->list);
1636
1637                 if (vm->pte_support_ats)
1638                         init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
1639
1640                 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
1641                                                 mapping->start, mapping->last,
1642                                                 init_pte_value, 0, &f);
1643                 amdgpu_vm_free_mapping(adev, vm, mapping, f);
1644                 if (r) {
1645                         dma_fence_put(f);
1646                         return r;
1647                 }
1648         }
1649
1650         if (fence && f) {
1651                 dma_fence_put(*fence);
1652                 *fence = f;
1653         } else {
1654                 dma_fence_put(f);
1655         }
1656
1657         return 0;
1658
1659 }
1660
1661 /**
1662  * amdgpu_vm_handle_moved - handle moved BOs in the PT
1663  *
1664  * @adev: amdgpu_device pointer
1665  * @vm: requested vm
1666  * @sync: sync object to add fences to
1667  *
1668  * Make sure all BOs which are moved are updated in the PTs.
1669  * Returns 0 for success.
1670  *
1671  * PTs have to be reserved!
1672  */
1673 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
1674                            struct amdgpu_vm *vm)
1675 {
1676         bool clear;
1677         int r = 0;
1678
1679         spin_lock(&vm->status_lock);
1680         while (!list_empty(&vm->moved)) {
1681                 struct amdgpu_bo_va *bo_va;
1682                 struct reservation_object *resv;
1683
1684                 bo_va = list_first_entry(&vm->moved,
1685                         struct amdgpu_bo_va, base.vm_status);
1686                 spin_unlock(&vm->status_lock);
1687
1688                 resv = bo_va->base.bo->tbo.resv;
1689
1690                 /* Per VM BOs never need to bo cleared in the page tables */
1691                 if (resv == vm->root.base.bo->tbo.resv)
1692                         clear = false;
1693                 /* Try to reserve the BO to avoid clearing its ptes */
1694                 else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
1695                         clear = false;
1696                 /* Somebody else is using the BO right now */
1697                 else
1698                         clear = true;
1699
1700                 r = amdgpu_vm_bo_update(adev, bo_va, clear);
1701                 if (r)
1702                         return r;
1703
1704                 if (!clear && resv != vm->root.base.bo->tbo.resv)
1705                         reservation_object_unlock(resv);
1706
1707                 spin_lock(&vm->status_lock);
1708         }
1709         spin_unlock(&vm->status_lock);
1710
1711         return r;
1712 }
1713
1714 /**
1715  * amdgpu_vm_bo_add - add a bo to a specific vm
1716  *
1717  * @adev: amdgpu_device pointer
1718  * @vm: requested vm
1719  * @bo: amdgpu buffer object
1720  *
1721  * Add @bo into the requested vm.
1722  * Add @bo to the list of bos associated with the vm
1723  * Returns newly added bo_va or NULL for failure
1724  *
1725  * Object has to be reserved!
1726  */
1727 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1728                                       struct amdgpu_vm *vm,
1729                                       struct amdgpu_bo *bo)
1730 {
1731         struct amdgpu_bo_va *bo_va;
1732
1733         bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1734         if (bo_va == NULL) {
1735                 return NULL;
1736         }
1737         bo_va->base.vm = vm;
1738         bo_va->base.bo = bo;
1739         INIT_LIST_HEAD(&bo_va->base.bo_list);
1740         INIT_LIST_HEAD(&bo_va->base.vm_status);
1741
1742         bo_va->ref_count = 1;
1743         INIT_LIST_HEAD(&bo_va->valids);
1744         INIT_LIST_HEAD(&bo_va->invalids);
1745
1746         if (!bo)
1747                 return bo_va;
1748
1749         list_add_tail(&bo_va->base.bo_list, &bo->va);
1750
1751         if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
1752                 return bo_va;
1753
1754         if (bo->preferred_domains &
1755             amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
1756                 return bo_va;
1757
1758         /*
1759          * We checked all the prerequisites, but it looks like this per VM BO
1760          * is currently evicted. add the BO to the evicted list to make sure it
1761          * is validated on next VM use to avoid fault.
1762          * */
1763         spin_lock(&vm->status_lock);
1764         list_move_tail(&bo_va->base.vm_status, &vm->evicted);
1765         spin_unlock(&vm->status_lock);
1766
1767         return bo_va;
1768 }
1769
1770
1771 /**
1772  * amdgpu_vm_bo_insert_mapping - insert a new mapping
1773  *
1774  * @adev: amdgpu_device pointer
1775  * @bo_va: bo_va to store the address
1776  * @mapping: the mapping to insert
1777  *
1778  * Insert a new mapping into all structures.
1779  */
1780 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1781                                     struct amdgpu_bo_va *bo_va,
1782                                     struct amdgpu_bo_va_mapping *mapping)
1783 {
1784         struct amdgpu_vm *vm = bo_va->base.vm;
1785         struct amdgpu_bo *bo = bo_va->base.bo;
1786
1787         mapping->bo_va = bo_va;
1788         list_add(&mapping->list, &bo_va->invalids);
1789         amdgpu_vm_it_insert(mapping, &vm->va);
1790
1791         if (mapping->flags & AMDGPU_PTE_PRT)
1792                 amdgpu_vm_prt_get(adev);
1793
1794         if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1795                 spin_lock(&vm->status_lock);
1796                 if (list_empty(&bo_va->base.vm_status))
1797                         list_add(&bo_va->base.vm_status, &vm->moved);
1798                 spin_unlock(&vm->status_lock);
1799         }
1800         trace_amdgpu_vm_bo_map(bo_va, mapping);
1801 }
1802
1803 /**
1804  * amdgpu_vm_bo_map - map bo inside a vm
1805  *
1806  * @adev: amdgpu_device pointer
1807  * @bo_va: bo_va to store the address
1808  * @saddr: where to map the BO
1809  * @offset: requested offset in the BO
1810  * @flags: attributes of pages (read/write/valid/etc.)
1811  *
1812  * Add a mapping of the BO at the specefied addr into the VM.
1813  * Returns 0 for success, error for failure.
1814  *
1815  * Object has to be reserved and unreserved outside!
1816  */
1817 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1818                      struct amdgpu_bo_va *bo_va,
1819                      uint64_t saddr, uint64_t offset,
1820                      uint64_t size, uint64_t flags)
1821 {
1822         struct amdgpu_bo_va_mapping *mapping, *tmp;
1823         struct amdgpu_bo *bo = bo_va->base.bo;
1824         struct amdgpu_vm *vm = bo_va->base.vm;
1825         uint64_t eaddr;
1826
1827         /* validate the parameters */
1828         if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1829             size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1830                 return -EINVAL;
1831
1832         /* make sure object fit at this offset */
1833         eaddr = saddr + size - 1;
1834         if (saddr >= eaddr ||
1835             (bo && offset + size > amdgpu_bo_size(bo)))
1836                 return -EINVAL;
1837
1838         saddr /= AMDGPU_GPU_PAGE_SIZE;
1839         eaddr /= AMDGPU_GPU_PAGE_SIZE;
1840
1841         tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1842         if (tmp) {
1843                 /* bo and tmp overlap, invalid addr */
1844                 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1845                         "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
1846                         tmp->start, tmp->last + 1);
1847                 return -EINVAL;
1848         }
1849
1850         mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1851         if (!mapping)
1852                 return -ENOMEM;
1853
1854         mapping->start = saddr;
1855         mapping->last = eaddr;
1856         mapping->offset = offset;
1857         mapping->flags = flags;
1858
1859         amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1860
1861         return 0;
1862 }
1863
1864 /**
1865  * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1866  *
1867  * @adev: amdgpu_device pointer
1868  * @bo_va: bo_va to store the address
1869  * @saddr: where to map the BO
1870  * @offset: requested offset in the BO
1871  * @flags: attributes of pages (read/write/valid/etc.)
1872  *
1873  * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1874  * mappings as we do so.
1875  * Returns 0 for success, error for failure.
1876  *
1877  * Object has to be reserved and unreserved outside!
1878  */
1879 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1880                              struct amdgpu_bo_va *bo_va,
1881                              uint64_t saddr, uint64_t offset,
1882                              uint64_t size, uint64_t flags)
1883 {
1884         struct amdgpu_bo_va_mapping *mapping;
1885         struct amdgpu_bo *bo = bo_va->base.bo;
1886         uint64_t eaddr;
1887         int r;
1888
1889         /* validate the parameters */
1890         if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1891             size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1892                 return -EINVAL;
1893
1894         /* make sure object fit at this offset */
1895         eaddr = saddr + size - 1;
1896         if (saddr >= eaddr ||
1897             (bo && offset + size > amdgpu_bo_size(bo)))
1898                 return -EINVAL;
1899
1900         /* Allocate all the needed memory */
1901         mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1902         if (!mapping)
1903                 return -ENOMEM;
1904
1905         r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
1906         if (r) {
1907                 kfree(mapping);
1908                 return r;
1909         }
1910
1911         saddr /= AMDGPU_GPU_PAGE_SIZE;
1912         eaddr /= AMDGPU_GPU_PAGE_SIZE;
1913
1914         mapping->start = saddr;
1915         mapping->last = eaddr;
1916         mapping->offset = offset;
1917         mapping->flags = flags;
1918
1919         amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1920
1921         return 0;
1922 }
1923
1924 /**
1925  * amdgpu_vm_bo_unmap - remove bo mapping from vm
1926  *
1927  * @adev: amdgpu_device pointer
1928  * @bo_va: bo_va to remove the address from
1929  * @saddr: where to the BO is mapped
1930  *
1931  * Remove a mapping of the BO at the specefied addr from the VM.
1932  * Returns 0 for success, error for failure.
1933  *
1934  * Object has to be reserved and unreserved outside!
1935  */
1936 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1937                        struct amdgpu_bo_va *bo_va,
1938                        uint64_t saddr)
1939 {
1940         struct amdgpu_bo_va_mapping *mapping;
1941         struct amdgpu_vm *vm = bo_va->base.vm;
1942         bool valid = true;
1943
1944         saddr /= AMDGPU_GPU_PAGE_SIZE;
1945
1946         list_for_each_entry(mapping, &bo_va->valids, list) {
1947                 if (mapping->start == saddr)
1948                         break;
1949         }
1950
1951         if (&mapping->list == &bo_va->valids) {
1952                 valid = false;
1953
1954                 list_for_each_entry(mapping, &bo_va->invalids, list) {
1955                         if (mapping->start == saddr)
1956                                 break;
1957                 }
1958
1959                 if (&mapping->list == &bo_va->invalids)
1960                         return -ENOENT;
1961         }
1962
1963         list_del(&mapping->list);
1964         amdgpu_vm_it_remove(mapping, &vm->va);
1965         mapping->bo_va = NULL;
1966         trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1967
1968         if (valid)
1969                 list_add(&mapping->list, &vm->freed);
1970         else
1971                 amdgpu_vm_free_mapping(adev, vm, mapping,
1972                                        bo_va->last_pt_update);
1973
1974         return 0;
1975 }
1976
1977 /**
1978  * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1979  *
1980  * @adev: amdgpu_device pointer
1981  * @vm: VM structure to use
1982  * @saddr: start of the range
1983  * @size: size of the range
1984  *
1985  * Remove all mappings in a range, split them as appropriate.
1986  * Returns 0 for success, error for failure.
1987  */
1988 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1989                                 struct amdgpu_vm *vm,
1990                                 uint64_t saddr, uint64_t size)
1991 {
1992         struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1993         LIST_HEAD(removed);
1994         uint64_t eaddr;
1995
1996         eaddr = saddr + size - 1;
1997         saddr /= AMDGPU_GPU_PAGE_SIZE;
1998         eaddr /= AMDGPU_GPU_PAGE_SIZE;
1999
2000         /* Allocate all the needed memory */
2001         before = kzalloc(sizeof(*before), GFP_KERNEL);
2002         if (!before)
2003                 return -ENOMEM;
2004         INIT_LIST_HEAD(&before->list);
2005
2006         after = kzalloc(sizeof(*after), GFP_KERNEL);
2007         if (!after) {
2008                 kfree(before);
2009                 return -ENOMEM;
2010         }
2011         INIT_LIST_HEAD(&after->list);
2012
2013         /* Now gather all removed mappings */
2014         tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2015         while (tmp) {
2016                 /* Remember mapping split at the start */
2017                 if (tmp->start < saddr) {
2018                         before->start = tmp->start;
2019                         before->last = saddr - 1;
2020                         before->offset = tmp->offset;
2021                         before->flags = tmp->flags;
2022                         list_add(&before->list, &tmp->list);
2023                 }
2024
2025                 /* Remember mapping split at the end */
2026                 if (tmp->last > eaddr) {
2027                         after->start = eaddr + 1;
2028                         after->last = tmp->last;
2029                         after->offset = tmp->offset;
2030                         after->offset += after->start - tmp->start;
2031                         after->flags = tmp->flags;
2032                         list_add(&after->list, &tmp->list);
2033                 }
2034
2035                 list_del(&tmp->list);
2036                 list_add(&tmp->list, &removed);
2037
2038                 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
2039         }
2040
2041         /* And free them up */
2042         list_for_each_entry_safe(tmp, next, &removed, list) {
2043                 amdgpu_vm_it_remove(tmp, &vm->va);
2044                 list_del(&tmp->list);
2045
2046                 if (tmp->start < saddr)
2047                     tmp->start = saddr;
2048                 if (tmp->last > eaddr)
2049                     tmp->last = eaddr;
2050
2051                 tmp->bo_va = NULL;
2052                 list_add(&tmp->list, &vm->freed);
2053                 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2054         }
2055
2056         /* Insert partial mapping before the range */
2057         if (!list_empty(&before->list)) {
2058                 amdgpu_vm_it_insert(before, &vm->va);
2059                 if (before->flags & AMDGPU_PTE_PRT)
2060                         amdgpu_vm_prt_get(adev);
2061         } else {
2062                 kfree(before);
2063         }
2064
2065         /* Insert partial mapping after the range */
2066         if (!list_empty(&after->list)) {
2067                 amdgpu_vm_it_insert(after, &vm->va);
2068                 if (after->flags & AMDGPU_PTE_PRT)
2069                         amdgpu_vm_prt_get(adev);
2070         } else {
2071                 kfree(after);
2072         }
2073
2074         return 0;
2075 }
2076
2077 /**
2078  * amdgpu_vm_bo_lookup_mapping - find mapping by address
2079  *
2080  * @vm: the requested VM
2081  *
2082  * Find a mapping by it's address.
2083  */
2084 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2085                                                          uint64_t addr)
2086 {
2087         return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2088 }
2089
2090 /**
2091  * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2092  *
2093  * @adev: amdgpu_device pointer
2094  * @bo_va: requested bo_va
2095  *
2096  * Remove @bo_va->bo from the requested vm.
2097  *
2098  * Object have to be reserved!
2099  */
2100 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2101                       struct amdgpu_bo_va *bo_va)
2102 {
2103         struct amdgpu_bo_va_mapping *mapping, *next;
2104         struct amdgpu_vm *vm = bo_va->base.vm;
2105
2106         list_del(&bo_va->base.bo_list);
2107
2108         spin_lock(&vm->status_lock);
2109         list_del(&bo_va->base.vm_status);
2110         spin_unlock(&vm->status_lock);
2111
2112         list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
2113                 list_del(&mapping->list);
2114                 amdgpu_vm_it_remove(mapping, &vm->va);
2115                 mapping->bo_va = NULL;
2116                 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2117                 list_add(&mapping->list, &vm->freed);
2118         }
2119         list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2120                 list_del(&mapping->list);
2121                 amdgpu_vm_it_remove(mapping, &vm->va);
2122                 amdgpu_vm_free_mapping(adev, vm, mapping,
2123                                        bo_va->last_pt_update);
2124         }
2125
2126         dma_fence_put(bo_va->last_pt_update);
2127         kfree(bo_va);
2128 }
2129
2130 /**
2131  * amdgpu_vm_bo_invalidate - mark the bo as invalid
2132  *
2133  * @adev: amdgpu_device pointer
2134  * @vm: requested vm
2135  * @bo: amdgpu buffer object
2136  *
2137  * Mark @bo as invalid.
2138  */
2139 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
2140                              struct amdgpu_bo *bo, bool evicted)
2141 {
2142         struct amdgpu_vm_bo_base *bo_base;
2143
2144         list_for_each_entry(bo_base, &bo->va, bo_list) {
2145                 struct amdgpu_vm *vm = bo_base->vm;
2146
2147                 bo_base->moved = true;
2148                 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2149                         spin_lock(&bo_base->vm->status_lock);
2150                         if (bo->tbo.type == ttm_bo_type_kernel)
2151                                 list_move(&bo_base->vm_status, &vm->evicted);
2152                         else
2153                                 list_move_tail(&bo_base->vm_status,
2154                                                &vm->evicted);
2155                         spin_unlock(&bo_base->vm->status_lock);
2156                         continue;
2157                 }
2158
2159                 if (bo->tbo.type == ttm_bo_type_kernel) {
2160                         spin_lock(&bo_base->vm->status_lock);
2161                         if (list_empty(&bo_base->vm_status))
2162                                 list_add(&bo_base->vm_status, &vm->relocated);
2163                         spin_unlock(&bo_base->vm->status_lock);
2164                         continue;
2165                 }
2166
2167                 spin_lock(&bo_base->vm->status_lock);
2168                 if (list_empty(&bo_base->vm_status))
2169                         list_add(&bo_base->vm_status, &vm->moved);
2170                 spin_unlock(&bo_base->vm->status_lock);
2171         }
2172 }
2173
2174 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2175 {
2176         /* Total bits covered by PD + PTs */
2177         unsigned bits = ilog2(vm_size) + 18;
2178
2179         /* Make sure the PD is 4K in size up to 8GB address space.
2180            Above that split equal between PD and PTs */
2181         if (vm_size <= 8)
2182                 return (bits - 9);
2183         else
2184                 return ((bits + 3) / 2);
2185 }
2186
2187 /**
2188  * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
2189  *
2190  * @adev: amdgpu_device pointer
2191  * @vm_size: the default vm size if it's set auto
2192  */
2193 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
2194                            uint32_t fragment_size_default, unsigned max_level,
2195                            unsigned max_bits)
2196 {
2197         uint64_t tmp;
2198
2199         /* adjust vm size first */
2200         if (amdgpu_vm_size != -1) {
2201                 unsigned max_size = 1 << (max_bits - 30);
2202
2203                 vm_size = amdgpu_vm_size;
2204                 if (vm_size > max_size) {
2205                         dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2206                                  amdgpu_vm_size, max_size);
2207                         vm_size = max_size;
2208                 }
2209         }
2210
2211         adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
2212
2213         tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
2214         if (amdgpu_vm_block_size != -1)
2215                 tmp >>= amdgpu_vm_block_size - 9;
2216         tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2217         adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
2218         switch (adev->vm_manager.num_level) {
2219         case 3:
2220                 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2221                 break;
2222         case 2:
2223                 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2224                 break;
2225         case 1:
2226                 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2227                 break;
2228         default:
2229                 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2230         }
2231         /* block size depends on vm size and hw setup*/
2232         if (amdgpu_vm_block_size != -1)
2233                 adev->vm_manager.block_size =
2234                         min((unsigned)amdgpu_vm_block_size, max_bits
2235                             - AMDGPU_GPU_PAGE_SHIFT
2236                             - 9 * adev->vm_manager.num_level);
2237         else if (adev->vm_manager.num_level > 1)
2238                 adev->vm_manager.block_size = 9;
2239         else
2240                 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
2241
2242         if (amdgpu_vm_fragment_size == -1)
2243                 adev->vm_manager.fragment_size = fragment_size_default;
2244         else
2245                 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2246
2247         DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2248                  vm_size, adev->vm_manager.num_level + 1,
2249                  adev->vm_manager.block_size,
2250                  adev->vm_manager.fragment_size);
2251 }
2252
2253 /**
2254  * amdgpu_vm_init - initialize a vm instance
2255  *
2256  * @adev: amdgpu_device pointer
2257  * @vm: requested vm
2258  * @vm_context: Indicates if it GFX or Compute context
2259  *
2260  * Init @vm fields.
2261  */
2262 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2263                    int vm_context, unsigned int pasid)
2264 {
2265         const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
2266                 AMDGPU_VM_PTE_COUNT(adev) * 8);
2267         uint64_t init_pde_value = 0, flags;
2268         unsigned ring_instance;
2269         struct amdgpu_ring *ring;
2270         struct drm_sched_rq *rq;
2271         unsigned long size;
2272         int r, i;
2273
2274         vm->va = RB_ROOT_CACHED;
2275         for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2276                 vm->reserved_vmid[i] = NULL;
2277         spin_lock_init(&vm->status_lock);
2278         INIT_LIST_HEAD(&vm->evicted);
2279         INIT_LIST_HEAD(&vm->relocated);
2280         INIT_LIST_HEAD(&vm->moved);
2281         INIT_LIST_HEAD(&vm->freed);
2282
2283         /* create scheduler entity for page table updates */
2284
2285         ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2286         ring_instance %= adev->vm_manager.vm_pte_num_rings;
2287         ring = adev->vm_manager.vm_pte_rings[ring_instance];
2288         rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2289         r = drm_sched_entity_init(&ring->sched, &vm->entity,
2290                                   rq, amdgpu_sched_jobs, NULL);
2291         if (r)
2292                 return r;
2293
2294         vm->pte_support_ats = false;
2295
2296         if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
2297                 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2298                                                 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2299
2300                 if (adev->asic_type == CHIP_RAVEN) {
2301                         vm->pte_support_ats = true;
2302                         init_pde_value = AMDGPU_PTE_DEFAULT_ATC
2303                                         | AMDGPU_PDE_PTE;
2304
2305                 }
2306         } else
2307                 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2308                                                 AMDGPU_VM_USE_CPU_FOR_GFX);
2309         DRM_DEBUG_DRIVER("VM update mode is %s\n",
2310                          vm->use_cpu_for_update ? "CPU" : "SDMA");
2311         WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2312                   "CPU update of VM recommended only for large BAR system\n");
2313         vm->last_update = NULL;
2314
2315         flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2316                         AMDGPU_GEM_CREATE_VRAM_CLEARED;
2317         if (vm->use_cpu_for_update)
2318                 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2319         else
2320                 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2321                                 AMDGPU_GEM_CREATE_SHADOW);
2322
2323         size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
2324         r = amdgpu_bo_create(adev, size, align, true, AMDGPU_GEM_DOMAIN_VRAM,
2325                              flags, NULL, NULL, init_pde_value,
2326                              &vm->root.base.bo);
2327         if (r)
2328                 goto error_free_sched_entity;
2329
2330         r = amdgpu_bo_reserve(vm->root.base.bo, true);
2331         if (r)
2332                 goto error_free_root;
2333
2334         vm->root.base.vm = vm;
2335         list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
2336         list_add_tail(&vm->root.base.vm_status, &vm->evicted);
2337         amdgpu_bo_unreserve(vm->root.base.bo);
2338
2339         if (pasid) {
2340                 unsigned long flags;
2341
2342                 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2343                 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2344                               GFP_ATOMIC);
2345                 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2346                 if (r < 0)
2347                         goto error_free_root;
2348
2349                 vm->pasid = pasid;
2350         }
2351
2352         INIT_KFIFO(vm->faults);
2353         vm->fault_credit = 16;
2354
2355         return 0;
2356
2357 error_free_root:
2358         amdgpu_bo_unref(&vm->root.base.bo->shadow);
2359         amdgpu_bo_unref(&vm->root.base.bo);
2360         vm->root.base.bo = NULL;
2361
2362 error_free_sched_entity:
2363         drm_sched_entity_fini(&ring->sched, &vm->entity);
2364
2365         return r;
2366 }
2367
2368 /**
2369  * amdgpu_vm_free_levels - free PD/PT levels
2370  *
2371  * @adev: amdgpu device structure
2372  * @parent: PD/PT starting level to free
2373  * @level: level of parent structure
2374  *
2375  * Free the page directory or page table level and all sub levels.
2376  */
2377 static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2378                                   struct amdgpu_vm_pt *parent,
2379                                   unsigned level)
2380 {
2381         unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
2382
2383         if (parent->base.bo) {
2384                 list_del(&parent->base.bo_list);
2385                 list_del(&parent->base.vm_status);
2386                 amdgpu_bo_unref(&parent->base.bo->shadow);
2387                 amdgpu_bo_unref(&parent->base.bo);
2388         }
2389
2390         if (parent->entries)
2391                 for (i = 0; i < num_entries; i++)
2392                         amdgpu_vm_free_levels(adev, &parent->entries[i],
2393                                               level + 1);
2394
2395         kvfree(parent->entries);
2396 }
2397
2398 /**
2399  * amdgpu_vm_fini - tear down a vm instance
2400  *
2401  * @adev: amdgpu_device pointer
2402  * @vm: requested vm
2403  *
2404  * Tear down @vm.
2405  * Unbind the VM and remove all bos from the vm bo list
2406  */
2407 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2408 {
2409         struct amdgpu_bo_va_mapping *mapping, *tmp;
2410         bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
2411         struct amdgpu_bo *root;
2412         u64 fault;
2413         int i, r;
2414
2415         /* Clear pending page faults from IH when the VM is destroyed */
2416         while (kfifo_get(&vm->faults, &fault))
2417                 amdgpu_ih_clear_fault(adev, fault);
2418
2419         if (vm->pasid) {
2420                 unsigned long flags;
2421
2422                 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2423                 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2424                 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2425         }
2426
2427         drm_sched_entity_fini(vm->entity.sched, &vm->entity);
2428
2429         if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
2430                 dev_err(adev->dev, "still active bo inside vm\n");
2431         }
2432         rbtree_postorder_for_each_entry_safe(mapping, tmp,
2433                                              &vm->va.rb_root, rb) {
2434                 list_del(&mapping->list);
2435                 amdgpu_vm_it_remove(mapping, &vm->va);
2436                 kfree(mapping);
2437         }
2438         list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2439                 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
2440                         amdgpu_vm_prt_fini(adev, vm);
2441                         prt_fini_needed = false;
2442                 }
2443
2444                 list_del(&mapping->list);
2445                 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2446         }
2447
2448         root = amdgpu_bo_ref(vm->root.base.bo);
2449         r = amdgpu_bo_reserve(root, true);
2450         if (r) {
2451                 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2452         } else {
2453                 amdgpu_vm_free_levels(adev, &vm->root,
2454                                       adev->vm_manager.root_level);
2455                 amdgpu_bo_unreserve(root);
2456         }
2457         amdgpu_bo_unref(&root);
2458         dma_fence_put(vm->last_update);
2459         for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2460                 amdgpu_vmid_free_reserved(adev, vm, i);
2461 }
2462
2463 /**
2464  * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2465  *
2466  * @adev: amdgpu_device pointer
2467  * @pasid: PASID do identify the VM
2468  *
2469  * This function is expected to be called in interrupt context. Returns
2470  * true if there was fault credit, false otherwise
2471  */
2472 bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2473                                   unsigned int pasid)
2474 {
2475         struct amdgpu_vm *vm;
2476
2477         spin_lock(&adev->vm_manager.pasid_lock);
2478         vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
2479         if (!vm) {
2480                 /* VM not found, can't track fault credit */
2481                 spin_unlock(&adev->vm_manager.pasid_lock);
2482                 return true;
2483         }
2484
2485         /* No lock needed. only accessed by IRQ handler */
2486         if (!vm->fault_credit) {
2487                 /* Too many faults in this VM */
2488                 spin_unlock(&adev->vm_manager.pasid_lock);
2489                 return false;
2490         }
2491
2492         vm->fault_credit--;
2493         spin_unlock(&adev->vm_manager.pasid_lock);
2494         return true;
2495 }
2496
2497 /**
2498  * amdgpu_vm_manager_init - init the VM manager
2499  *
2500  * @adev: amdgpu_device pointer
2501  *
2502  * Initialize the VM manager structures
2503  */
2504 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2505 {
2506         unsigned i;
2507
2508         amdgpu_vmid_mgr_init(adev);
2509
2510         adev->vm_manager.fence_context =
2511                 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2512         for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2513                 adev->vm_manager.seqno[i] = 0;
2514
2515         atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
2516         spin_lock_init(&adev->vm_manager.prt_lock);
2517         atomic_set(&adev->vm_manager.num_prt_users, 0);
2518
2519         /* If not overridden by the user, by default, only in large BAR systems
2520          * Compute VM tables will be updated by CPU
2521          */
2522 #ifdef CONFIG_X86_64
2523         if (amdgpu_vm_update_mode == -1) {
2524                 if (amdgpu_vm_is_large_bar(adev))
2525                         adev->vm_manager.vm_update_mode =
2526                                 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2527                 else
2528                         adev->vm_manager.vm_update_mode = 0;
2529         } else
2530                 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2531 #else
2532         adev->vm_manager.vm_update_mode = 0;
2533 #endif
2534
2535         idr_init(&adev->vm_manager.pasid_idr);
2536         spin_lock_init(&adev->vm_manager.pasid_lock);
2537 }
2538
2539 /**
2540  * amdgpu_vm_manager_fini - cleanup VM manager
2541  *
2542  * @adev: amdgpu_device pointer
2543  *
2544  * Cleanup the VM manager and free resources.
2545  */
2546 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2547 {
2548         WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2549         idr_destroy(&adev->vm_manager.pasid_idr);
2550
2551         amdgpu_vmid_mgr_fini(adev);
2552 }
2553
2554 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2555 {
2556         union drm_amdgpu_vm *args = data;
2557         struct amdgpu_device *adev = dev->dev_private;
2558         struct amdgpu_fpriv *fpriv = filp->driver_priv;
2559         int r;
2560
2561         switch (args->in.op) {
2562         case AMDGPU_VM_OP_RESERVE_VMID:
2563                 /* current, we only have requirement to reserve vmid from gfxhub */
2564                 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
2565                 if (r)
2566                         return r;
2567                 break;
2568         case AMDGPU_VM_OP_UNRESERVE_VMID:
2569                 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
2570                 break;
2571         default:
2572                 return -EINVAL;
2573         }
2574
2575         return 0;
2576 }
This page took 0.189076 seconds and 4 git commands to generate.