Merge tag 'amd-drm-next-5.19-2022-04-15' of https://gitlab.freedesktop.org/agd5f...
authorDave Airlie <airlied@redhat.com>
Thu, 28 Apr 2022 04:33:20 +0000 (14:33 +1000)
committerDave Airlie <airlied@redhat.com>
Thu, 28 Apr 2022 04:33:20 +0000 (14:33 +1000)
amd-drm-next-5.19-2022-04-15:

amdgpu:
- USB-C updates
- GPUVM updates
- TMZ fixes for RV
- DCN 3.1 pstate fixes
- Display z state fixes
- RAS fixes
- Misc code cleanups and spelling fixes
- More DC FP rework
- GPUVM TLB handling rework
- Power management sysfs code cleanup
- Add RAS support for VCN
- Backlight fix
- Add unique id support for more asics
- Misc display updates
- SR-IOV fixes
- Extend CG and PG flags to 64 bits
- Enable VCN clk sysfs nodes for navi12

amdkfd:
- Fix IO link cleanup during device removal
- RAS fixes
- Retry fault fixes
- Asynchronously free events
- SVM fixes

radeon:
- Drop some dead code
- Misc code cleanups

From: Alex Deucher <alexander.deucher@amd.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20220415135144.5700-1-alexander.deucher@amd.com
Signed-off-by: Dave Airlie <airlied@redhat.com>
15 files changed:
1  2 
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
drivers/gpu/drm/amd/amdgpu/amdgpu_display.c
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c
drivers/gpu/drm/amd/amdkfd/kfd_priv.h
drivers/gpu/drm/amd/amdkfd/kfd_svm.c
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
include/drm/dp/drm_dp_helper.h

index 3dc5ab2764ff84f61b2a4c3f0b4c97406503e596,8b14c55a079319d19f20fd0324d57b69a23a8edd..80b6b8e432fd0a404ec872793cba1569a19d72ad
@@@ -253,18 -253,53 +253,18 @@@ void amdgpu_amdkfd_release_notify(struc
  static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo,
                                        struct amdgpu_amdkfd_fence *ef)
  {
 -      struct dma_resv *resv = bo->tbo.base.resv;
 -      struct dma_resv_list *old, *new;
 -      unsigned int i, j, k;
 +      struct dma_fence *replacement;
  
        if (!ef)
                return -EINVAL;
  
 -      old = dma_resv_shared_list(resv);
 -      if (!old)
 -              return 0;
 -
 -      new = kmalloc(struct_size(new, shared, old->shared_max), GFP_KERNEL);
 -      if (!new)
 -              return -ENOMEM;
 -
 -      /* Go through all the shared fences in the resevation object and sort
 -       * the interesting ones to the end of the list.
 +      /* TODO: Instead of block before we should use the fence of the page
 +       * table update and TLB flush here directly.
         */
 -      for (i = 0, j = old->shared_count, k = 0; i < old->shared_count; ++i) {
 -              struct dma_fence *f;
 -
 -              f = rcu_dereference_protected(old->shared[i],
 -                                            dma_resv_held(resv));
 -
 -              if (f->context == ef->base.context)
 -                      RCU_INIT_POINTER(new->shared[--j], f);
 -              else
 -                      RCU_INIT_POINTER(new->shared[k++], f);
 -      }
 -      new->shared_max = old->shared_max;
 -      new->shared_count = k;
 -
 -      /* Install the new fence list, seqcount provides the barriers */
 -      write_seqcount_begin(&resv->seq);
 -      RCU_INIT_POINTER(resv->fence, new);
 -      write_seqcount_end(&resv->seq);
 -
 -      /* Drop the references to the removed fences or move them to ef_list */
 -      for (i = j; i < old->shared_count; ++i) {
 -              struct dma_fence *f;
 -
 -              f = rcu_dereference_protected(new->shared[i],
 -                                            dma_resv_held(resv));
 -              dma_fence_put(f);
 -      }
 -      kfree_rcu(old, rcu);
 -
 +      replacement = dma_fence_get_stub();
 +      dma_resv_replace_fences(bo->tbo.base.resv, ef->base.context,
 +                              replacement, DMA_RESV_USAGE_READ);
 +      dma_fence_put(replacement);
        return 0;
  }
  
@@@ -1058,8 -1093,7 +1058,7 @@@ static void unmap_bo_from_gpuvm(struct 
  
  static int update_gpuvm_pte(struct kgd_mem *mem,
                            struct kfd_mem_attachment *entry,
-                           struct amdgpu_sync *sync,
-                           bool *table_freed)
+                           struct amdgpu_sync *sync)
  {
        struct amdgpu_bo_va *bo_va = entry->bo_va;
        struct amdgpu_device *adev = entry->adev;
                return ret;
  
        /* Update the page tables  */
-       ret = amdgpu_vm_bo_update(adev, bo_va, false, table_freed);
+       ret = amdgpu_vm_bo_update(adev, bo_va, false);
        if (ret) {
                pr_err("amdgpu_vm_bo_update failed\n");
                return ret;
  static int map_bo_to_gpuvm(struct kgd_mem *mem,
                           struct kfd_mem_attachment *entry,
                           struct amdgpu_sync *sync,
-                          bool no_update_pte,
-                          bool *table_freed)
+                          bool no_update_pte)
  {
        int ret;
  
        if (no_update_pte)
                return 0;
  
-       ret = update_gpuvm_pte(mem, entry, sync, table_freed);
+       ret = update_gpuvm_pte(mem, entry, sync);
        if (ret) {
                pr_err("update_gpuvm_pte() failed\n");
                goto update_gpuvm_pte_failed;
@@@ -1233,7 -1266,7 +1231,7 @@@ static int init_kfd_vm(struct amdgpu_v
                                  AMDGPU_FENCE_OWNER_KFD, false);
        if (ret)
                goto wait_pd_fail;
 -      ret = dma_resv_reserve_shared(vm->root.bo->tbo.base.resv, 1);
 +      ret = dma_resv_reserve_fences(vm->root.bo->tbo.base.resv, 1);
        if (ret)
                goto reserve_shared_fail;
        amdgpu_bo_fence(vm->root.bo,
@@@ -1710,7 -1743,7 +1708,7 @@@ int amdgpu_amdkfd_gpuvm_free_memory_of_
  
  int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
                struct amdgpu_device *adev, struct kgd_mem *mem,
-               void *drm_priv, bool *table_freed)
+               void *drm_priv)
  {
        struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv);
        int ret;
                         entry->va, entry->va + bo_size, entry);
  
                ret = map_bo_to_gpuvm(mem, entry, ctx.sync,
-                                     is_invalid_userptr, table_freed);
+                                     is_invalid_userptr);
                if (ret) {
                        pr_err("Failed to map bo to gpuvm\n");
                        goto out_unreserve;
@@@ -2265,7 -2298,7 +2263,7 @@@ static int validate_invalid_user_pages(
                                continue;
  
                        kfd_mem_dmaunmap_attachment(mem, attachment);
-                       ret = update_gpuvm_pte(mem, attachment, &sync, NULL);
+                       ret = update_gpuvm_pte(mem, attachment, &sync);
                        if (ret) {
                                pr_err("%s: update PTE failed\n", __func__);
                                /* make sure this gets validated again */
@@@ -2447,8 -2480,6 +2445,8 @@@ int amdgpu_amdkfd_gpuvm_restore_process
                struct amdgpu_bo *bo = mem->bo;
                uint32_t domain = mem->domain;
                struct kfd_mem_attachment *attachment;
 +              struct dma_resv_iter cursor;
 +              struct dma_fence *fence;
  
                total_size += amdgpu_bo_size(bo);
  
                                goto validate_map_fail;
                        }
                }
 -              ret = amdgpu_sync_fence(&sync_obj, bo->tbo.moving);
 -              if (ret) {
 -                      pr_debug("Memory eviction: Sync BO fence failed. Try again\n");
 -                      goto validate_map_fail;
 +              dma_resv_for_each_fence(&cursor, bo->tbo.base.resv,
 +                                      DMA_RESV_USAGE_KERNEL, fence) {
 +                      ret = amdgpu_sync_fence(&sync_obj, fence);
 +                      if (ret) {
 +                              pr_debug("Memory eviction: Sync BO fence failed. Try again\n");
 +                              goto validate_map_fail;
 +                      }
                }
                list_for_each_entry(attachment, &mem->attachments, list) {
                        if (!attachment->is_mapped)
                                continue;
  
                        kfd_mem_dmaunmap_attachment(mem, attachment);
-                       ret = update_gpuvm_pte(mem, attachment, &sync_obj, NULL);
+                       ret = update_gpuvm_pte(mem, attachment, &sync_obj);
                        if (ret) {
                                pr_debug("Memory eviction: update PTE failed. Try again\n");
                                goto validate_map_fail;
@@@ -2576,7 -2604,7 +2574,7 @@@ int amdgpu_amdkfd_add_gws_to_process(vo
         * Add process eviction fence to bo so they can
         * evict each other.
         */
 -      ret = dma_resv_reserve_shared(gws_bo->tbo.base.resv, 1);
 +      ret = dma_resv_reserve_fences(gws_bo->tbo.base.resv, 1);
        if (ret)
                goto reserve_shared_fail;
        amdgpu_bo_fence(gws_bo, &process_info->eviction_fence->base, true);
index 8de28399776936031227c1e562649803701c1be0,12bad207bb0f64441e94b0c376d3b5ceade0c1b9..71633a1fedfddd2dce4a9e67fdbe1199e2835ec9
@@@ -55,8 -55,8 +55,8 @@@ static int amdgpu_cs_user_fence_chunk(s
        bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
        p->uf_entry.priority = 0;
        p->uf_entry.tv.bo = &bo->tbo;
 -      /* One for TTM and one for the CS job */
 -      p->uf_entry.tv.num_shared = 2;
 +      /* One for TTM and two for the CS job */
 +      p->uf_entry.tv.num_shared = 3;
  
        drm_gem_object_put(gobj);
  
@@@ -574,6 -574,14 +574,6 @@@ static int amdgpu_cs_parser_bos(struct 
                struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
  
                e->bo_va = amdgpu_vm_bo_find(vm, bo);
 -
 -              if (bo->tbo.base.dma_buf && !amdgpu_bo_explicit_sync(bo)) {
 -                      e->chain = dma_fence_chain_alloc();
 -                      if (!e->chain) {
 -                              r = -ENOMEM;
 -                              goto error_validate;
 -                      }
 -              }
        }
  
        /* Move fence waiting after getting reservation lock of
        }
  
  error_validate:
 -      if (r) {
 -              amdgpu_bo_list_for_each_entry(e, p->bo_list) {
 -                      dma_fence_chain_free(e->chain);
 -                      e->chain = NULL;
 -              }
 +      if (r)
                ttm_eu_backoff_reservation(&p->ticket, &p->validated);
 -      }
  out:
        return r;
  }
@@@ -675,9 -688,17 +675,9 @@@ static void amdgpu_cs_parser_fini(struc
  {
        unsigned i;
  
 -      if (error && backoff) {
 -              struct amdgpu_bo_list_entry *e;
 -
 -              amdgpu_bo_list_for_each_entry(e, parser->bo_list) {
 -                      dma_fence_chain_free(e->chain);
 -                      e->chain = NULL;
 -              }
 -
 +      if (error && backoff)
                ttm_eu_backoff_reservation(&parser->ticket,
                                           &parser->validated);
 -      }
  
        for (i = 0; i < parser->num_post_deps; i++) {
                drm_syncobj_put(parser->post_deps[i].syncobj);
@@@ -785,22 -806,22 +785,22 @@@ static int amdgpu_cs_vm_handling(struc
        if (r)
                return r;
  
-       r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false, NULL);
+       r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
        if (r)
                return r;
  
-       r = amdgpu_sync_vm_fence(&p->job->sync, fpriv->prt_va->last_pt_update);
+       r = amdgpu_sync_fence(&p->job->sync, fpriv->prt_va->last_pt_update);
        if (r)
                return r;
  
        if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
                bo_va = fpriv->csa_va;
                BUG_ON(!bo_va);
-               r = amdgpu_vm_bo_update(adev, bo_va, false, NULL);
+               r = amdgpu_vm_bo_update(adev, bo_va, false);
                if (r)
                        return r;
  
-               r = amdgpu_sync_vm_fence(&p->job->sync, bo_va->last_pt_update);
+               r = amdgpu_sync_fence(&p->job->sync, bo_va->last_pt_update);
                if (r)
                        return r;
        }
                if (bo_va == NULL)
                        continue;
  
-               r = amdgpu_vm_bo_update(adev, bo_va, false, NULL);
+               r = amdgpu_vm_bo_update(adev, bo_va, false);
                if (r)
                        return r;
  
-               r = amdgpu_sync_vm_fence(&p->job->sync, bo_va->last_pt_update);
+               r = amdgpu_sync_fence(&p->job->sync, bo_va->last_pt_update);
                if (r)
                        return r;
        }
        if (r)
                return r;
  
-       r = amdgpu_sync_vm_fence(&p->job->sync, vm->last_update);
+       r = amdgpu_sync_fence(&p->job->sync, vm->last_update);
        if (r)
                return r;
  
@@@ -1251,9 -1272,24 +1251,9 @@@ static int amdgpu_cs_submit(struct amdg
  
        amdgpu_vm_move_to_lru_tail(p->adev, &fpriv->vm);
  
 -      amdgpu_bo_list_for_each_entry(e, p->bo_list) {
 -              struct dma_resv *resv = e->tv.bo->base.resv;
 -              struct dma_fence_chain *chain = e->chain;
 -
 -              if (!chain)
 -                      continue;
 -
 -              /*
 -               * Work around dma_resv shortcomings by wrapping up the
 -               * submission in a dma_fence_chain and add it as exclusive
 -               * fence.
 -               */
 -              dma_fence_chain_init(chain, dma_resv_excl_fence(resv),
 -                                   dma_fence_get(p->fence), 1);
 -
 -              rcu_assign_pointer(resv->fence_excl, &chain->base);
 -              e->chain = NULL;
 -      }
 +      /* Make sure all BOs are remembered as writers */
 +      amdgpu_bo_list_for_each_entry(e, p->bo_list)
 +              e->tv.num_shared = 0;
  
        ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
        mutex_unlock(&p->adev->notifier_lock);
index 7a6908d7182023a940e5e240cd5991ddb39f0342,787724166952472bcdf816f536a7b516ca674419..17c9bbe0cbc59fadbc64d303e5872f7766230397
  #include <drm/drm_fourcc.h>
  #include <drm/drm_vblank.h>
  
+ static int amdgpu_display_framebuffer_init(struct drm_device *dev,
+                                          struct amdgpu_framebuffer *rfb,
+                                          const struct drm_mode_fb_cmd2 *mode_cmd,
+                                          struct drm_gem_object *obj);
  static void amdgpu_display_flip_callback(struct dma_fence *f,
                                         struct dma_fence_cb *cb)
  {
@@@ -113,8 -118,9 +118,9 @@@ static void amdgpu_display_flip_work_fu
        spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  
  
-       DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_SUBMITTED, work: %p,\n",
-                                        amdgpu_crtc->crtc_id, amdgpu_crtc, work);
+       drm_dbg_vbl(adev_to_drm(adev),
+                   "crtc:%d[%p], pflip_stat:AMDGPU_FLIP_SUBMITTED, work: %p,\n",
+                   amdgpu_crtc->crtc_id, amdgpu_crtc, work);
  
  }
  
@@@ -200,7 -206,8 +206,7 @@@ int amdgpu_display_crtc_page_flip_targe
                goto unpin;
        }
  
 -      /* TODO: Unify this with other drivers */
 -      r = dma_resv_get_fences(new_abo->tbo.base.resv, true,
 +      r = dma_resv_get_fences(new_abo->tbo.base.resv, DMA_RESV_USAGE_WRITE,
                                &work->shared_count,
                                &work->shared);
        if (unlikely(r != 0)) {
@@@ -1038,35 -1045,11 +1044,11 @@@ static int amdgpu_display_get_fb_info(c
        return r;
  }
  
- int amdgpu_display_gem_fb_init(struct drm_device *dev,
-                              struct amdgpu_framebuffer *rfb,
-                              const struct drm_mode_fb_cmd2 *mode_cmd,
-                              struct drm_gem_object *obj)
- {
-       int ret;
-       rfb->base.obj[0] = obj;
-       drm_helper_mode_fill_fb_struct(dev, &rfb->base, mode_cmd);
-       ret = amdgpu_display_framebuffer_init(dev, rfb, mode_cmd, obj);
-       if (ret)
-               goto err;
-       ret = drm_framebuffer_init(dev, &rfb->base, &amdgpu_fb_funcs);
-       if (ret)
-               goto err;
-       return 0;
- err:
-       drm_dbg_kms(dev, "Failed to init gem fb: %d\n", ret);
-       rfb->base.obj[0] = NULL;
-       return ret;
- }
- int amdgpu_display_gem_fb_verify_and_init(
-       struct drm_device *dev, struct amdgpu_framebuffer *rfb,
-       struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd,
-       struct drm_gem_object *obj)
+ static int amdgpu_display_gem_fb_verify_and_init(struct drm_device *dev,
+                                                struct amdgpu_framebuffer *rfb,
+                                                struct drm_file *file_priv,
+                                                const struct drm_mode_fb_cmd2 *mode_cmd,
+                                                struct drm_gem_object *obj)
  {
        int ret;
  
        return ret;
  }
  
- int amdgpu_display_framebuffer_init(struct drm_device *dev,
-                                   struct amdgpu_framebuffer *rfb,
-                                   const struct drm_mode_fb_cmd2 *mode_cmd,
-                                   struct drm_gem_object *obj)
static int amdgpu_display_framebuffer_init(struct drm_device *dev,
+                                          struct amdgpu_framebuffer *rfb,
+                                          const struct drm_mode_fb_cmd2 *mode_cmd,
+                                          struct drm_gem_object *obj)
  {
        struct amdgpu_device *adev = drm_to_adev(dev);
        int ret, i;
index 84a53758e18e85fc9b9539ebcf599c75fa42ddd1,89c6d6f1d4faad322228de4d880eae0a9a7040bd..652571267077694eb57971e5664a1678bd168756
@@@ -526,8 -526,7 +526,8 @@@ int amdgpu_gem_wait_idle_ioctl(struct d
                return -ENOENT;
        }
        robj = gem_to_amdgpu_bo(gobj);
 -      ret = dma_resv_wait_timeout(robj->tbo.base.resv, true, true, timeout);
 +      ret = dma_resv_wait_timeout(robj->tbo.base.resv, DMA_RESV_USAGE_READ,
 +                                  true, timeout);
  
        /* ret == 0 means not signaled,
         * ret > 0 means signaled
@@@ -613,7 -612,7 +613,7 @@@ static void amdgpu_gem_va_update_vm(str
  
        if (operation == AMDGPU_VA_OP_MAP ||
            operation == AMDGPU_VA_OP_REPLACE) {
-               r = amdgpu_vm_bo_update(adev, bo_va, false, NULL);
+               r = amdgpu_vm_bo_update(adev, bo_va, false);
                if (r)
                        goto error;
        }
index 4ba4b54092f19cc9432b8aaf5e2f20b1bb1df69a,558f90e11d7861a7b21aeb0bf71a65a634def982..03d115d2b5edd25a65fb1b5dcfec0306bde382f3
@@@ -107,19 -107,36 +107,19 @@@ static void amdgpu_pasid_free_cb(struc
  void amdgpu_pasid_free_delayed(struct dma_resv *resv,
                               u32 pasid)
  {
 -      struct dma_fence *fence, **fences;
        struct amdgpu_pasid_cb *cb;
 -      unsigned count;
 +      struct dma_fence *fence;
        int r;
  
 -      r = dma_resv_get_fences(resv, true, &count, &fences);
 +      r = dma_resv_get_singleton(resv, DMA_RESV_USAGE_BOOKKEEP, &fence);
        if (r)
                goto fallback;
  
 -      if (count == 0) {
 +      if (!fence) {
                amdgpu_pasid_free(pasid);
                return;
        }
  
 -      if (count == 1) {
 -              fence = fences[0];
 -              kfree(fences);
 -      } else {
 -              uint64_t context = dma_fence_context_alloc(1);
 -              struct dma_fence_array *array;
 -
 -              array = dma_fence_array_create(count, fences, context,
 -                                             1, false);
 -              if (!array) {
 -                      kfree(fences);
 -                      goto fallback;
 -              }
 -              fence = &array->base;
 -      }
 -
        cb = kmalloc(sizeof(*cb), GFP_KERNEL);
        if (!cb) {
                /* Last resort when we are OOM */
@@@ -139,8 -156,7 +139,8 @@@ fallback
        /* Not enough memory for the delayed delete, as last resort
         * block for all the fences to complete.
         */
 -      dma_resv_wait_timeout(resv, true, false, MAX_SCHEDULE_TIMEOUT);
 +      dma_resv_wait_timeout(resv, DMA_RESV_USAGE_BOOKKEEP,
 +                            false, MAX_SCHEDULE_TIMEOUT);
        amdgpu_pasid_free(pasid);
  }
  
@@@ -260,19 -276,15 +260,15 @@@ static int amdgpu_vmid_grab_reserved(st
        struct amdgpu_device *adev = ring->adev;
        unsigned vmhub = ring->funcs->vmhub;
        uint64_t fence_context = adev->fence_context + ring->idx;
-       struct dma_fence *updates = sync->last_vm_update;
        bool needs_flush = vm->use_cpu_for_update;
-       int r = 0;
+       uint64_t updates = amdgpu_vm_tlb_seq(vm);
+       int r;
  
        *id = vm->reserved_vmid[vmhub];
-       if (updates && (*id)->flushed_updates &&
-           updates->context == (*id)->flushed_updates->context &&
-           !dma_fence_is_later(updates, (*id)->flushed_updates))
-               updates = NULL;
        if ((*id)->owner != vm->immediate.fence_context ||
-           job->vm_pd_addr != (*id)->pd_gpu_addr ||
-           updates || !(*id)->last_flush ||
+           (*id)->pd_gpu_addr != job->vm_pd_addr ||
+           (*id)->flushed_updates < updates ||
+           !(*id)->last_flush ||
            ((*id)->last_flush->context != fence_context &&
             !dma_fence_is_signaled((*id)->last_flush))) {
                struct dma_fence *tmp;
                tmp = amdgpu_sync_peek_fence(&(*id)->active, ring);
                if (tmp) {
                        *id = NULL;
-                       r = amdgpu_sync_fence(sync, tmp);
-                       return r;
+                       return amdgpu_sync_fence(sync, tmp);
                }
                needs_flush = true;
        }
        if (r)
                return r;
  
-       if (updates) {
-               dma_fence_put((*id)->flushed_updates);
-               (*id)->flushed_updates = dma_fence_get(updates);
-       }
+       (*id)->flushed_updates = updates;
        job->vm_needs_flush = needs_flush;
        return 0;
  }
@@@ -330,7 -338,7 +322,7 @@@ static int amdgpu_vmid_grab_used(struc
        unsigned vmhub = ring->funcs->vmhub;
        struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
        uint64_t fence_context = adev->fence_context + ring->idx;
-       struct dma_fence *updates = sync->last_vm_update;
+       uint64_t updates = amdgpu_vm_tlb_seq(vm);
        int r;
  
        job->vm_needs_flush = vm->use_cpu_for_update;
        /* Check if we can use a VMID already assigned to this VM */
        list_for_each_entry_reverse((*id), &id_mgr->ids_lru, list) {
                bool needs_flush = vm->use_cpu_for_update;
-               struct dma_fence *flushed;
  
                /* Check all the prerequisites to using this VMID */
                if ((*id)->owner != vm->immediate.fence_context)
                     !dma_fence_is_signaled((*id)->last_flush)))
                        needs_flush = true;
  
-               flushed  = (*id)->flushed_updates;
-               if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
+               if ((*id)->flushed_updates < updates)
                        needs_flush = true;
  
                if (needs_flush && !adev->vm_manager.concurrent_flush)
                if (r)
                        return r;
  
-               if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
-                       dma_fence_put((*id)->flushed_updates);
-                       (*id)->flushed_updates = dma_fence_get(updates);
-               }
+               (*id)->flushed_updates = updates;
                job->vm_needs_flush |= needs_flush;
                return 0;
        }
@@@ -416,8 -418,6 +402,6 @@@ int amdgpu_vmid_grab(struct amdgpu_vm *
                        goto error;
  
                if (!id) {
-                       struct dma_fence *updates = sync->last_vm_update;
                        /* Still no ID to use? Then use the idle one found earlier */
                        id = idle;
  
                        if (r)
                                goto error;
  
-                       dma_fence_put(id->flushed_updates);
-                       id->flushed_updates = dma_fence_get(updates);
+                       id->flushed_updates = amdgpu_vm_tlb_seq(vm);
                        job->vm_needs_flush = true;
                }
  
@@@ -594,7 -593,6 +577,6 @@@ void amdgpu_vmid_mgr_fini(struct amdgpu
                        struct amdgpu_vmid *id = &id_mgr->ids[j];
  
                        amdgpu_sync_free(&id->active);
-                       dma_fence_put(id->flushed_updates);
                        dma_fence_put(id->last_flush);
                        dma_fence_put(id->pasid_mapping);
                }
index e92ecabfa7bda4125e8e2feeb099d1b4f6ca60ab,a00022b6ee5b67aa600a8cd571e2f4ace672f614..5444515c1476e1f5f093497b156c0c62e3587a0a
@@@ -472,7 -472,7 +472,7 @@@ static bool amdgpu_bo_validate_size(str
  
  fail:
        DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
-                 man->size << PAGE_SHIFT);
+                 man->size);
        return false;
  }
  
@@@ -612,8 -612,9 +612,8 @@@ int amdgpu_bo_create(struct amdgpu_devi
                if (unlikely(r))
                        goto fail_unreserve;
  
 -              amdgpu_bo_fence(bo, fence, false);
 -              dma_fence_put(bo->tbo.moving);
 -              bo->tbo.moving = dma_fence_get(fence);
 +              dma_resv_add_fence(bo->tbo.base.resv, fence,
 +                                 DMA_RESV_USAGE_KERNEL);
                dma_fence_put(fence);
        }
        if (!bp->resv)
@@@ -760,11 -761,6 +760,11 @@@ int amdgpu_bo_kmap(struct amdgpu_bo *bo
        if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
                return -EPERM;
  
 +      r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_KERNEL,
 +                                false, MAX_SCHEDULE_TIMEOUT);
 +      if (r < 0)
 +              return r;
 +
        kptr = amdgpu_bo_kptr(bo);
        if (kptr) {
                if (ptr)
                return 0;
        }
  
 -      r = dma_resv_wait_timeout(bo->tbo.base.resv, false, false,
 -                                MAX_SCHEDULE_TIMEOUT);
 -      if (r < 0)
 -              return r;
 -
        r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.resource->num_pages, &bo->kmap);
        if (r)
                return r;
@@@ -1389,17 -1390,11 +1389,17 @@@ void amdgpu_bo_fence(struct amdgpu_bo *
                     bool shared)
  {
        struct dma_resv *resv = bo->tbo.base.resv;
 +      int r;
  
 -      if (shared)
 -              dma_resv_add_shared_fence(resv, fence);
 -      else
 -              dma_resv_add_excl_fence(resv, fence);
 +      r = dma_resv_reserve_fences(resv, 1);
 +      if (r) {
 +              /* As last resort on OOM we block for the fence */
 +              dma_fence_wait(fence, false);
 +              return;
 +      }
 +
 +      dma_resv_add_fence(resv, fence, shared ? DMA_RESV_USAGE_READ :
 +                         DMA_RESV_USAGE_WRITE);
  }
  
  /**
index 11c46b3e4c60b4d36f22252dcd9524773bd12404,edd49dd27422c7fee81ab3c072eb2d08bedc9264..504af1b93bfa84e728127b7e1ee6afc035fe4156
@@@ -51,7 -51,6 +51,6 @@@ static struct kmem_cache *amdgpu_sync_s
  void amdgpu_sync_create(struct amdgpu_sync *sync)
  {
        hash_init(sync->fences);
-       sync->last_vm_update = NULL;
  }
  
  /**
@@@ -171,23 -170,6 +170,6 @@@ int amdgpu_sync_fence(struct amdgpu_syn
        return 0;
  }
  
- /**
-  * amdgpu_sync_vm_fence - remember to sync to this VM fence
-  *
-  * @sync: sync object to add fence to
-  * @fence: the VM fence to add
-  *
-  * Add the fence to the sync object and remember it as VM update.
-  */
- int amdgpu_sync_vm_fence(struct amdgpu_sync *sync, struct dma_fence *fence)
- {
-       if (!fence)
-               return 0;
-       amdgpu_sync_keep_later(&sync->last_vm_update, fence);
-       return amdgpu_sync_fence(sync, fence);
- }
  /* Determine based on the owner and mode if we should sync to a fence or not */
  static bool amdgpu_sync_test_fence(struct amdgpu_device *adev,
                                   enum amdgpu_sync_mode mode,
@@@ -259,8 -241,7 +241,8 @@@ int amdgpu_sync_resv(struct amdgpu_devi
        if (resv == NULL)
                return -EINVAL;
  
 -      dma_resv_for_each_fence(&cursor, resv, true, f) {
 +      /* TODO: Use DMA_RESV_USAGE_READ here */
 +      dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, f) {
                dma_fence_chain_for_each(f, f) {
                        struct dma_fence *tmp = dma_fence_chain_contained(f);
  
@@@ -377,9 -358,6 +359,6 @@@ int amdgpu_sync_clone(struct amdgpu_syn
                }
        }
  
-       dma_fence_put(clone->last_vm_update);
-       clone->last_vm_update = dma_fence_get(source->last_vm_update);
        return 0;
  }
  
@@@ -420,8 -398,6 +399,6 @@@ void amdgpu_sync_free(struct amdgpu_syn
                dma_fence_put(e->fence);
                kmem_cache_free(amdgpu_sync_slab, e);
        }
-       dma_fence_put(sync->last_vm_update);
  }
  
  /**
index 5277c10d901dcad2269dc7e39ce6edf83c64f8bf,4736174f5e4d631b444988d688e3ebf1ba9f4f93..f9479e23de1855649807a7f6bb19e78c79fb08f4
@@@ -88,6 -88,21 +88,21 @@@ struct amdgpu_prt_cb 
        struct dma_fence_cb cb;
  };
  
+ /**
+  * struct amdgpu_vm_tlb_seq_cb - Helper to increment the TLB flush sequence
+  */
+ struct amdgpu_vm_tlb_seq_cb {
+       /**
+        * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
+        */
+       struct amdgpu_vm *vm;
+       /**
+        * @cb: callback
+        */
+       struct dma_fence_cb cb;
+ };
  /**
   * amdgpu_vm_set_pasid - manage pasid and vm ptr mapping
   *
@@@ -154,108 -169,6 +169,6 @@@ static inline void amdgpu_vm_eviction_u
        mutex_unlock(&vm->eviction_lock);
  }
  
- /**
-  * amdgpu_vm_level_shift - return the addr shift for each level
-  *
-  * @adev: amdgpu_device pointer
-  * @level: VMPT level
-  *
-  * Returns:
-  * The number of bits the pfn needs to be right shifted for a level.
-  */
- static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
-                                     unsigned level)
- {
-       switch (level) {
-       case AMDGPU_VM_PDB2:
-       case AMDGPU_VM_PDB1:
-       case AMDGPU_VM_PDB0:
-               return 9 * (AMDGPU_VM_PDB0 - level) +
-                       adev->vm_manager.block_size;
-       case AMDGPU_VM_PTB:
-               return 0;
-       default:
-               return ~0;
-       }
- }
- /**
-  * amdgpu_vm_num_entries - return the number of entries in a PD/PT
-  *
-  * @adev: amdgpu_device pointer
-  * @level: VMPT level
-  *
-  * Returns:
-  * The number of entries in a page directory or page table.
-  */
- static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
-                                     unsigned level)
- {
-       unsigned shift = amdgpu_vm_level_shift(adev,
-                                              adev->vm_manager.root_level);
-       if (level == adev->vm_manager.root_level)
-               /* For the root directory */
-               return round_up(adev->vm_manager.max_pfn, 1ULL << shift)
-                       >> shift;
-       else if (level != AMDGPU_VM_PTB)
-               /* Everything in between */
-               return 512;
-       else
-               /* For the page tables on the leaves */
-               return AMDGPU_VM_PTE_COUNT(adev);
- }
- /**
-  * amdgpu_vm_num_ats_entries - return the number of ATS entries in the root PD
-  *
-  * @adev: amdgpu_device pointer
-  *
-  * Returns:
-  * The number of entries in the root page directory which needs the ATS setting.
-  */
- static unsigned amdgpu_vm_num_ats_entries(struct amdgpu_device *adev)
- {
-       unsigned shift;
-       shift = amdgpu_vm_level_shift(adev, adev->vm_manager.root_level);
-       return AMDGPU_GMC_HOLE_START >> (shift + AMDGPU_GPU_PAGE_SHIFT);
- }
- /**
-  * amdgpu_vm_entries_mask - the mask to get the entry number of a PD/PT
-  *
-  * @adev: amdgpu_device pointer
-  * @level: VMPT level
-  *
-  * Returns:
-  * The mask to extract the entry number of a PD/PT from an address.
-  */
- static uint32_t amdgpu_vm_entries_mask(struct amdgpu_device *adev,
-                                      unsigned int level)
- {
-       if (level <= adev->vm_manager.root_level)
-               return 0xffffffff;
-       else if (level != AMDGPU_VM_PTB)
-               return 0x1ff;
-       else
-               return AMDGPU_VM_PTE_COUNT(adev) - 1;
- }
- /**
-  * amdgpu_vm_bo_size - returns the size of the BOs in bytes
-  *
-  * @adev: amdgpu_device pointer
-  * @level: VMPT level
-  *
-  * Returns:
-  * The size of the BO for a page directory or page table in bytes.
-  */
- static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
- {
-       return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
- }
  /**
   * amdgpu_vm_bo_evicted - vm_bo is evicted
   *
@@@ -358,9 -271,8 +271,8 @@@ static void amdgpu_vm_bo_done(struct am
   * Initialize a bo_va_base structure and add it to the appropriate lists
   *
   */
- static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
-                                  struct amdgpu_vm *vm,
-                                  struct amdgpu_bo *bo)
+ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
+                           struct amdgpu_vm *vm, struct amdgpu_bo *bo)
  {
        base->vm = vm;
        base->bo = bo;
  
        dma_resv_assert_held(vm->root.bo->tbo.base.resv);
  
 -      vm->bulk_moveable = false;
 +      ttm_bo_set_bulk_move(&bo->tbo, &vm->lru_bulk_move);
        if (bo->tbo.type == ttm_bo_type_kernel && bo->parent)
                amdgpu_vm_bo_relocated(base);
        else
        amdgpu_vm_bo_evicted(base);
  }
  
- /**
-  * amdgpu_vm_pt_parent - get the parent page directory
-  *
-  * @pt: child page table
-  *
-  * Helper to get the parent entry for the child page table. NULL if we are at
-  * the root page directory.
-  */
- static struct amdgpu_vm_bo_base *amdgpu_vm_pt_parent(struct amdgpu_vm_bo_base *pt)
- {
-       struct amdgpu_bo *parent = pt->bo->parent;
-       if (!parent)
-               return NULL;
-       return parent->vm_bo;
- }
- /*
-  * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt
-  */
- struct amdgpu_vm_pt_cursor {
-       uint64_t pfn;
-       struct amdgpu_vm_bo_base *parent;
-       struct amdgpu_vm_bo_base *entry;
-       unsigned level;
- };
- /**
-  * amdgpu_vm_pt_start - start PD/PT walk
-  *
-  * @adev: amdgpu_device pointer
-  * @vm: amdgpu_vm structure
-  * @start: start address of the walk
-  * @cursor: state to initialize
-  *
-  * Initialize a amdgpu_vm_pt_cursor to start a walk.
-  */
- static void amdgpu_vm_pt_start(struct amdgpu_device *adev,
-                              struct amdgpu_vm *vm, uint64_t start,
-                              struct amdgpu_vm_pt_cursor *cursor)
- {
-       cursor->pfn = start;
-       cursor->parent = NULL;
-       cursor->entry = &vm->root;
-       cursor->level = adev->vm_manager.root_level;
- }
- /**
-  * amdgpu_vm_pt_descendant - go to child node
-  *
-  * @adev: amdgpu_device pointer
-  * @cursor: current state
-  *
-  * Walk to the child node of the current node.
-  * Returns:
-  * True if the walk was possible, false otherwise.
-  */
- static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev,
-                                   struct amdgpu_vm_pt_cursor *cursor)
- {
-       unsigned mask, shift, idx;
-       if ((cursor->level == AMDGPU_VM_PTB) || !cursor->entry ||
-           !cursor->entry->bo)
-               return false;
-       mask = amdgpu_vm_entries_mask(adev, cursor->level);
-       shift = amdgpu_vm_level_shift(adev, cursor->level);
-       ++cursor->level;
-       idx = (cursor->pfn >> shift) & mask;
-       cursor->parent = cursor->entry;
-       cursor->entry = &to_amdgpu_bo_vm(cursor->entry->bo)->entries[idx];
-       return true;
- }
- /**
-  * amdgpu_vm_pt_sibling - go to sibling node
-  *
-  * @adev: amdgpu_device pointer
-  * @cursor: current state
-  *
-  * Walk to the sibling node of the current node.
-  * Returns:
-  * True if the walk was possible, false otherwise.
-  */
- static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev,
-                                struct amdgpu_vm_pt_cursor *cursor)
- {
-       unsigned shift, num_entries;
-       /* Root doesn't have a sibling */
-       if (!cursor->parent)
-               return false;
-       /* Go to our parents and see if we got a sibling */
-       shift = amdgpu_vm_level_shift(adev, cursor->level - 1);
-       num_entries = amdgpu_vm_num_entries(adev, cursor->level - 1);
-       if (cursor->entry == &to_amdgpu_bo_vm(cursor->parent->bo)->entries[num_entries - 1])
-               return false;
-       cursor->pfn += 1ULL << shift;
-       cursor->pfn &= ~((1ULL << shift) - 1);
-       ++cursor->entry;
-       return true;
- }
- /**
-  * amdgpu_vm_pt_ancestor - go to parent node
-  *
-  * @cursor: current state
-  *
-  * Walk to the parent node of the current node.
-  * Returns:
-  * True if the walk was possible, false otherwise.
-  */
- static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor)
- {
-       if (!cursor->parent)
-               return false;
-       --cursor->level;
-       cursor->entry = cursor->parent;
-       cursor->parent = amdgpu_vm_pt_parent(cursor->parent);
-       return true;
- }
- /**
-  * amdgpu_vm_pt_next - get next PD/PT in hieratchy
-  *
-  * @adev: amdgpu_device pointer
-  * @cursor: current state
-  *
-  * Walk the PD/PT tree to the next node.
-  */
- static void amdgpu_vm_pt_next(struct amdgpu_device *adev,
-                             struct amdgpu_vm_pt_cursor *cursor)
- {
-       /* First try a newborn child */
-       if (amdgpu_vm_pt_descendant(adev, cursor))
-               return;
-       /* If that didn't worked try to find a sibling */
-       while (!amdgpu_vm_pt_sibling(adev, cursor)) {
-               /* No sibling, go to our parents and grandparents */
-               if (!amdgpu_vm_pt_ancestor(cursor)) {
-                       cursor->pfn = ~0ll;
-                       return;
-               }
-       }
- }
- /**
-  * amdgpu_vm_pt_first_dfs - start a deep first search
-  *
-  * @adev: amdgpu_device structure
-  * @vm: amdgpu_vm structure
-  * @start: optional cursor to start with
-  * @cursor: state to initialize
-  *
-  * Starts a deep first traversal of the PD/PT tree.
-  */
- static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev,
-                                  struct amdgpu_vm *vm,
-                                  struct amdgpu_vm_pt_cursor *start,
-                                  struct amdgpu_vm_pt_cursor *cursor)
- {
-       if (start)
-               *cursor = *start;
-       else
-               amdgpu_vm_pt_start(adev, vm, 0, cursor);
-       while (amdgpu_vm_pt_descendant(adev, cursor));
- }
- /**
-  * amdgpu_vm_pt_continue_dfs - check if the deep first search should continue
-  *
-  * @start: starting point for the search
-  * @entry: current entry
-  *
-  * Returns:
-  * True when the search should continue, false otherwise.
-  */
- static bool amdgpu_vm_pt_continue_dfs(struct amdgpu_vm_pt_cursor *start,
-                                     struct amdgpu_vm_bo_base *entry)
- {
-       return entry && (!start || entry != start->entry);
- }
- /**
-  * amdgpu_vm_pt_next_dfs - get the next node for a deep first search
-  *
-  * @adev: amdgpu_device structure
-  * @cursor: current state
-  *
-  * Move the cursor to the next node in a deep first search.
-  */
- static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev,
-                                 struct amdgpu_vm_pt_cursor *cursor)
- {
-       if (!cursor->entry)
-               return;
-       if (!cursor->parent)
-               cursor->entry = NULL;
-       else if (amdgpu_vm_pt_sibling(adev, cursor))
-               while (amdgpu_vm_pt_descendant(adev, cursor));
-       else
-               amdgpu_vm_pt_ancestor(cursor);
- }
- /*
-  * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs
-  */
- #define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)                \
-       for (amdgpu_vm_pt_first_dfs((adev), (vm), (start), &(cursor)),          \
-            (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\
-            amdgpu_vm_pt_continue_dfs((start), (entry));                       \
-            (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor)))
  /**
   * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
   *
@@@ -639,6 -329,36 +329,6 @@@ void amdgpu_vm_get_pd_bo(struct amdgpu_
        list_add(&entry->tv.head, validated);
  }
  
 -/**
 - * amdgpu_vm_del_from_lru_notify - update bulk_moveable flag
 - *
 - * @bo: BO which was removed from the LRU
 - *
 - * Make sure the bulk_moveable flag is updated when a BO is removed from the
 - * LRU.
 - */
 -void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo)
 -{
 -      struct amdgpu_bo *abo;
 -      struct amdgpu_vm_bo_base *bo_base;
 -
 -      if (!amdgpu_bo_is_amdgpu_bo(bo))
 -              return;
 -
 -      if (bo->pin_count)
 -              return;
 -
 -      abo = ttm_to_amdgpu_bo(bo);
 -      if (!abo->parent)
 -              return;
 -      for (bo_base = abo->vm_bo; bo_base; bo_base = bo_base->next) {
 -              struct amdgpu_vm *vm = bo_base->vm;
 -
 -              if (abo->tbo.base.resv == vm->root.bo->tbo.base.resv)
 -                      vm->bulk_moveable = false;
 -      }
 -
 -}
  /**
   * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU
   *
  void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
                                struct amdgpu_vm *vm)
  {
 -      struct amdgpu_vm_bo_base *bo_base;
 -
 -      if (vm->bulk_moveable) {
 -              spin_lock(&adev->mman.bdev.lru_lock);
 -              ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
 -              spin_unlock(&adev->mman.bdev.lru_lock);
 -              return;
 -      }
 -
 -      memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
 -
        spin_lock(&adev->mman.bdev.lru_lock);
 -      list_for_each_entry(bo_base, &vm->idle, vm_status) {
 -              struct amdgpu_bo *bo = bo_base->bo;
 -              struct amdgpu_bo *shadow = amdgpu_bo_shadowed(bo);
 -
 -              if (!bo->parent)
 -                      continue;
 -
 -              ttm_bo_move_to_lru_tail(&bo->tbo, bo->tbo.resource,
 -                                      &vm->lru_bulk_move);
 -              if (shadow)
 -                      ttm_bo_move_to_lru_tail(&shadow->tbo,
 -                                              shadow->tbo.resource,
 -                                              &vm->lru_bulk_move);
 -      }
 +      ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
        spin_unlock(&adev->mman.bdev.lru_lock);
 -
 -      vm->bulk_moveable = true;
  }
  
  /**
@@@ -676,6 -422,8 +366,6 @@@ int amdgpu_vm_validate_pt_bos(struct am
        struct amdgpu_vm_bo_base *bo_base, *tmp;
        int r;
  
 -      vm->bulk_moveable &= list_empty(&vm->evicted);
 -
        list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
                struct amdgpu_bo *bo = bo_base->bo;
                struct amdgpu_bo *shadow = amdgpu_bo_shadowed(bo);
@@@ -725,316 -473,6 +415,6 @@@ bool amdgpu_vm_ready(struct amdgpu_vm *
        return ret && list_empty(&vm->evicted);
  }
  
- /**
-  * amdgpu_vm_clear_bo - initially clear the PDs/PTs
-  *
-  * @adev: amdgpu_device pointer
-  * @vm: VM to clear BO from
-  * @vmbo: BO to clear
-  * @immediate: use an immediate update
-  *
-  * Root PD needs to be reserved when calling this.
-  *
-  * Returns:
-  * 0 on success, errno otherwise.
-  */
- static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
-                             struct amdgpu_vm *vm,
-                             struct amdgpu_bo_vm *vmbo,
-                             bool immediate)
- {
-       struct ttm_operation_ctx ctx = { true, false };
-       unsigned level = adev->vm_manager.root_level;
-       struct amdgpu_vm_update_params params;
-       struct amdgpu_bo *ancestor = &vmbo->bo;
-       struct amdgpu_bo *bo = &vmbo->bo;
-       unsigned entries, ats_entries;
-       uint64_t addr;
-       int r, idx;
-       /* Figure out our place in the hierarchy */
-       if (ancestor->parent) {
-               ++level;
-               while (ancestor->parent->parent) {
-                       ++level;
-                       ancestor = ancestor->parent;
-               }
-       }
-       entries = amdgpu_bo_size(bo) / 8;
-       if (!vm->pte_support_ats) {
-               ats_entries = 0;
-       } else if (!bo->parent) {
-               ats_entries = amdgpu_vm_num_ats_entries(adev);
-               ats_entries = min(ats_entries, entries);
-               entries -= ats_entries;
-       } else {
-               struct amdgpu_vm_bo_base *pt;
-               pt = ancestor->vm_bo;
-               ats_entries = amdgpu_vm_num_ats_entries(adev);
-               if ((pt - to_amdgpu_bo_vm(vm->root.bo)->entries) >= ats_entries) {
-                       ats_entries = 0;
-               } else {
-                       ats_entries = entries;
-                       entries = 0;
-               }
-       }
-       r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
-       if (r)
-               return r;
-       if (vmbo->shadow) {
-               struct amdgpu_bo *shadow = vmbo->shadow;
-               r = ttm_bo_validate(&shadow->tbo, &shadow->placement, &ctx);
-               if (r)
-                       return r;
-       }
-       if (!drm_dev_enter(adev_to_drm(adev), &idx))
-               return -ENODEV;
-       r = vm->update_funcs->map_table(vmbo);
-       if (r)
-               goto exit;
-       memset(&params, 0, sizeof(params));
-       params.adev = adev;
-       params.vm = vm;
-       params.immediate = immediate;
-       r = vm->update_funcs->prepare(&params, NULL, AMDGPU_SYNC_EXPLICIT);
-       if (r)
-               goto exit;
-       addr = 0;
-       if (ats_entries) {
-               uint64_t value = 0, flags;
-               flags = AMDGPU_PTE_DEFAULT_ATC;
-               if (level != AMDGPU_VM_PTB) {
-                       /* Handle leaf PDEs as PTEs */
-                       flags |= AMDGPU_PDE_PTE;
-                       amdgpu_gmc_get_vm_pde(adev, level, &value, &flags);
-               }
-               r = vm->update_funcs->update(&params, vmbo, addr, 0, ats_entries,
-                                            value, flags);
-               if (r)
-                       goto exit;
-               addr += ats_entries * 8;
-       }
-       if (entries) {
-               uint64_t value = 0, flags = 0;
-               if (adev->asic_type >= CHIP_VEGA10) {
-                       if (level != AMDGPU_VM_PTB) {
-                               /* Handle leaf PDEs as PTEs */
-                               flags |= AMDGPU_PDE_PTE;
-                               amdgpu_gmc_get_vm_pde(adev, level,
-                                                     &value, &flags);
-                       } else {
-                               /* Workaround for fault priority problem on GMC9 */
-                               flags = AMDGPU_PTE_EXECUTABLE;
-                       }
-               }
-               r = vm->update_funcs->update(&params, vmbo, addr, 0, entries,
-                                            value, flags);
-               if (r)
-                       goto exit;
-       }
-       r = vm->update_funcs->commit(&params, NULL);
- exit:
-       drm_dev_exit(idx);
-       return r;
- }
- /**
-  * amdgpu_vm_pt_create - create bo for PD/PT
-  *
-  * @adev: amdgpu_device pointer
-  * @vm: requesting vm
-  * @level: the page table level
-  * @immediate: use a immediate update
-  * @vmbo: pointer to the buffer object pointer
-  */
- static int amdgpu_vm_pt_create(struct amdgpu_device *adev,
-                              struct amdgpu_vm *vm,
-                              int level, bool immediate,
-                              struct amdgpu_bo_vm **vmbo)
- {
-       struct amdgpu_bo_param bp;
-       struct amdgpu_bo *bo;
-       struct dma_resv *resv;
-       unsigned int num_entries;
-       int r;
-       memset(&bp, 0, sizeof(bp));
-       bp.size = amdgpu_vm_bo_size(adev, level);
-       bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
-       bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
-       bp.domain = amdgpu_bo_get_preferred_domain(adev, bp.domain);
-       bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
-               AMDGPU_GEM_CREATE_CPU_GTT_USWC;
-       if (level < AMDGPU_VM_PTB)
-               num_entries = amdgpu_vm_num_entries(adev, level);
-       else
-               num_entries = 0;
-       bp.bo_ptr_size = struct_size((*vmbo), entries, num_entries);
-       if (vm->use_cpu_for_update)
-               bp.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
-       bp.type = ttm_bo_type_kernel;
-       bp.no_wait_gpu = immediate;
-       if (vm->root.bo)
-               bp.resv = vm->root.bo->tbo.base.resv;
-       r = amdgpu_bo_create_vm(adev, &bp, vmbo);
-       if (r)
-               return r;
-       bo = &(*vmbo)->bo;
-       if (vm->is_compute_context || (adev->flags & AMD_IS_APU)) {
-               (*vmbo)->shadow = NULL;
-               return 0;
-       }
-       if (!bp.resv)
-               WARN_ON(dma_resv_lock(bo->tbo.base.resv,
-                                     NULL));
-       resv = bp.resv;
-       memset(&bp, 0, sizeof(bp));
-       bp.size = amdgpu_vm_bo_size(adev, level);
-       bp.domain = AMDGPU_GEM_DOMAIN_GTT;
-       bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
-       bp.type = ttm_bo_type_kernel;
-       bp.resv = bo->tbo.base.resv;
-       bp.bo_ptr_size = sizeof(struct amdgpu_bo);
-       r = amdgpu_bo_create(adev, &bp, &(*vmbo)->shadow);
-       if (!resv)
-               dma_resv_unlock(bo->tbo.base.resv);
-       if (r) {
-               amdgpu_bo_unref(&bo);
-               return r;
-       }
-       (*vmbo)->shadow->parent = amdgpu_bo_ref(bo);
-       amdgpu_bo_add_to_shadow_list(*vmbo);
-       return 0;
- }
- /**
-  * amdgpu_vm_alloc_pts - Allocate a specific page table
-  *
-  * @adev: amdgpu_device pointer
-  * @vm: VM to allocate page tables for
-  * @cursor: Which page table to allocate
-  * @immediate: use an immediate update
-  *
-  * Make sure a specific page table or directory is allocated.
-  *
-  * Returns:
-  * 1 if page table needed to be allocated, 0 if page table was already
-  * allocated, negative errno if an error occurred.
-  */
- static int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
-                              struct amdgpu_vm *vm,
-                              struct amdgpu_vm_pt_cursor *cursor,
-                              bool immediate)
- {
-       struct amdgpu_vm_bo_base *entry = cursor->entry;
-       struct amdgpu_bo *pt_bo;
-       struct amdgpu_bo_vm *pt;
-       int r;
-       if (entry->bo)
-               return 0;
-       r = amdgpu_vm_pt_create(adev, vm, cursor->level, immediate, &pt);
-       if (r)
-               return r;
-       /* Keep a reference to the root directory to avoid
-        * freeing them up in the wrong order.
-        */
-       pt_bo = &pt->bo;
-       pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
-       amdgpu_vm_bo_base_init(entry, vm, pt_bo);
-       r = amdgpu_vm_clear_bo(adev, vm, pt, immediate);
-       if (r)
-               goto error_free_pt;
-       return 0;
- error_free_pt:
-       amdgpu_bo_unref(&pt->shadow);
-       amdgpu_bo_unref(&pt_bo);
-       return r;
- }
- /**
-  * amdgpu_vm_free_table - fre one PD/PT
-  *
-  * @entry: PDE to free
-  */
- static void amdgpu_vm_free_table(struct amdgpu_vm_bo_base *entry)
- {
-       struct amdgpu_bo *shadow;
-       if (!entry->bo)
-               return;
-       shadow = amdgpu_bo_shadowed(entry->bo);
-       if (shadow) {
-               ttm_bo_set_bulk_move(&shadow->tbo, NULL);
-               amdgpu_bo_unref(&shadow);
-       }
-       ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
-       entry->bo->vm_bo = NULL;
-       list_del(&entry->vm_status);
-       amdgpu_bo_unref(&entry->bo);
- }
- /**
-  * amdgpu_vm_free_pts - free PD/PT levels
-  *
-  * @adev: amdgpu device structure
-  * @vm: amdgpu vm structure
-  * @start: optional cursor where to start freeing PDs/PTs
-  *
-  * Free the page directory or page table level and all sub levels.
-  */
- static void amdgpu_vm_free_pts(struct amdgpu_device *adev,
-                              struct amdgpu_vm *vm,
-                              struct amdgpu_vm_pt_cursor *start)
- {
-       struct amdgpu_vm_pt_cursor cursor;
-       struct amdgpu_vm_bo_base *entry;
-       for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
-               amdgpu_vm_free_table(entry);
-       if (start)
-               amdgpu_vm_free_table(start->entry);
- }
  /**
   * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
   *
@@@ -1281,53 -719,6 +661,6 @@@ uint64_t amdgpu_vm_map_gart(const dma_a
        return result;
  }
  
- /**
-  * amdgpu_vm_update_pde - update a single level in the hierarchy
-  *
-  * @params: parameters for the update
-  * @vm: requested vm
-  * @entry: entry to update
-  *
-  * Makes sure the requested entry in parent is up to date.
-  */
- static int amdgpu_vm_update_pde(struct amdgpu_vm_update_params *params,
-                               struct amdgpu_vm *vm,
-                               struct amdgpu_vm_bo_base *entry)
- {
-       struct amdgpu_vm_bo_base *parent = amdgpu_vm_pt_parent(entry);
-       struct amdgpu_bo *bo = parent->bo, *pbo;
-       uint64_t pde, pt, flags;
-       unsigned level;
-       for (level = 0, pbo = bo->parent; pbo; ++level)
-               pbo = pbo->parent;
-       level += params->adev->vm_manager.root_level;
-       amdgpu_gmc_get_pde_for_bo(entry->bo, level, &pt, &flags);
-       pde = (entry - to_amdgpu_bo_vm(parent->bo)->entries) * 8;
-       return vm->update_funcs->update(params, to_amdgpu_bo_vm(bo), pde, pt,
-                                       1, 0, flags);
- }
- /**
-  * amdgpu_vm_invalidate_pds - mark all PDs as invalid
-  *
-  * @adev: amdgpu_device pointer
-  * @vm: related vm
-  *
-  * Mark all PD level as invalid after an error.
-  */
- static void amdgpu_vm_invalidate_pds(struct amdgpu_device *adev,
-                                    struct amdgpu_vm *vm)
- {
-       struct amdgpu_vm_pt_cursor cursor;
-       struct amdgpu_vm_bo_base *entry;
-       for_each_amdgpu_vm_pt_dfs_safe(adev, vm, NULL, cursor, entry)
-               if (entry->bo && !entry->moved)
-                       amdgpu_vm_bo_relocated(entry);
- }
  /**
   * amdgpu_vm_update_pdes - make sure that all directories are valid
   *
@@@ -1344,6 -735,7 +677,7 @@@ int amdgpu_vm_update_pdes(struct amdgpu
                          struct amdgpu_vm *vm, bool immediate)
  {
        struct amdgpu_vm_update_params params;
+       struct amdgpu_vm_bo_base *entry;
        int r, idx;
  
        if (list_empty(&vm->relocated))
  
        r = vm->update_funcs->prepare(&params, NULL, AMDGPU_SYNC_EXPLICIT);
        if (r)
-               goto exit;
-       while (!list_empty(&vm->relocated)) {
-               struct amdgpu_vm_bo_base *entry;
-               entry = list_first_entry(&vm->relocated,
-                                        struct amdgpu_vm_bo_base,
-                                        vm_status);
-               amdgpu_vm_bo_idle(entry);
+               goto error;
  
-               r = amdgpu_vm_update_pde(&params, vm, entry);
+       list_for_each_entry(entry, &vm->relocated, vm_status) {
+               r = amdgpu_vm_pde_update(&params, entry);
                if (r)
                        goto error;
        }
        r = vm->update_funcs->commit(&params, &vm->last_update);
        if (r)
                goto error;
-       drm_dev_exit(idx);
-       return 0;
+       while (!list_empty(&vm->relocated)) {
+               entry = list_first_entry(&vm->relocated,
+                                        struct amdgpu_vm_bo_base,
+                                        vm_status);
+               amdgpu_vm_bo_idle(entry);
+       }
  
  error:
-       amdgpu_vm_invalidate_pds(adev, vm);
- exit:
        drm_dev_exit(idx);
        return r;
  }
  
- /*
-  * amdgpu_vm_update_flags - figure out flags for PTE updates
-  *
-  * Make sure to set the right flags for the PTEs at the desired level.
-  */
- static void amdgpu_vm_update_flags(struct amdgpu_vm_update_params *params,
-                                  struct amdgpu_bo_vm *pt, unsigned int level,
-                                  uint64_t pe, uint64_t addr,
-                                  unsigned int count, uint32_t incr,
-                                  uint64_t flags)
- {
-       if (level != AMDGPU_VM_PTB) {
-               flags |= AMDGPU_PDE_PTE;
-               amdgpu_gmc_get_vm_pde(params->adev, level, &addr, &flags);
-       } else if (params->adev->asic_type >= CHIP_VEGA10 &&
-                  !(flags & AMDGPU_PTE_VALID) &&
-                  !(flags & AMDGPU_PTE_PRT)) {
-               /* Workaround for fault priority problem on GMC9 */
-               flags |= AMDGPU_PTE_EXECUTABLE;
-       }
-       params->vm->update_funcs->update(params, pt, pe, addr, count, incr,
-                                        flags);
- }
  /**
-  * amdgpu_vm_fragment - get fragment for PTEs
+  * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
+  * @fence: unused
+  * @cb: the callback structure
   *
-  * @params: see amdgpu_vm_update_params definition
-  * @start: first PTE to handle
-  * @end: last PTE to handle
-  * @flags: hw mapping flags
-  * @frag: resulting fragment size
-  * @frag_end: end of this fragment
-  *
-  * Returns the first possible fragment for the start and end address.
-  */
- static void amdgpu_vm_fragment(struct amdgpu_vm_update_params *params,
-                              uint64_t start, uint64_t end, uint64_t flags,
-                              unsigned int *frag, uint64_t *frag_end)
- {
-       /**
-        * The MC L1 TLB supports variable sized pages, based on a fragment
-        * field in the PTE. When this field is set to a non-zero value, page
-        * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
-        * flags are considered valid for all PTEs within the fragment range
-        * and corresponding mappings are assumed to be physically contiguous.
-        *
-        * The L1 TLB can store a single PTE for the whole fragment,
-        * significantly increasing the space available for translation
-        * caching. This leads to large improvements in throughput when the
-        * TLB is under pressure.
-        *
-        * The L2 TLB distributes small and large fragments into two
-        * asymmetric partitions. The large fragment cache is significantly
-        * larger. Thus, we try to use large fragments wherever possible.
-        * Userspace can support this by aligning virtual base address and
-        * allocation size to the fragment size.
-        *
-        * Starting with Vega10 the fragment size only controls the L1. The L2
-        * is now directly feed with small/huge/giant pages from the walker.
-        */
-       unsigned max_frag;
-       if (params->adev->asic_type < CHIP_VEGA10)
-               max_frag = params->adev->vm_manager.fragment_size;
-       else
-               max_frag = 31;
-       /* system pages are non continuously */
-       if (params->pages_addr) {
-               *frag = 0;
-               *frag_end = end;
-               return;
-       }
-       /* This intentionally wraps around if no bit is set */
-       *frag = min((unsigned)ffs(start) - 1, (unsigned)fls64(end - start) - 1);
-       if (*frag >= max_frag) {
-               *frag = max_frag;
-               *frag_end = end & ~((1ULL << max_frag) - 1);
-       } else {
-               *frag_end = start + (1 << *frag);
-       }
- }
- /**
-  * amdgpu_vm_update_ptes - make sure that page tables are valid
-  *
-  * @params: see amdgpu_vm_update_params definition
-  * @start: start of GPU address range
-  * @end: end of GPU address range
-  * @dst: destination address to map to, the next dst inside the function
-  * @flags: mapping flags
-  *
-  * Update the page tables in the range @start - @end.
-  *
-  * Returns:
-  * 0 for success, -EINVAL for failure.
+  * Increments the tlb sequence to make sure that future CS execute a VM flush.
   */
- static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
-                                uint64_t start, uint64_t end,
-                                uint64_t dst, uint64_t flags)
+ static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
+                                struct dma_fence_cb *cb)
  {
-       struct amdgpu_device *adev = params->adev;
-       struct amdgpu_vm_pt_cursor cursor;
-       uint64_t frag_start = start, frag_end;
-       unsigned int frag;
-       int r;
-       /* figure out the initial fragment */
-       amdgpu_vm_fragment(params, frag_start, end, flags, &frag, &frag_end);
-       /* walk over the address space and update the PTs */
-       amdgpu_vm_pt_start(adev, params->vm, start, &cursor);
-       while (cursor.pfn < end) {
-               unsigned shift, parent_shift, mask;
-               uint64_t incr, entry_end, pe_start;
-               struct amdgpu_bo *pt;
-               if (!params->unlocked) {
-                       /* make sure that the page tables covering the
-                        * address range are actually allocated
-                        */
-                       r = amdgpu_vm_alloc_pts(params->adev, params->vm,
-                                               &cursor, params->immediate);
-                       if (r)
-                               return r;
-               }
-               shift = amdgpu_vm_level_shift(adev, cursor.level);
-               parent_shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
-               if (params->unlocked) {
-                       /* Unlocked updates are only allowed on the leaves */
-                       if (amdgpu_vm_pt_descendant(adev, &cursor))
-                               continue;
-               } else if (adev->asic_type < CHIP_VEGA10 &&
-                          (flags & AMDGPU_PTE_VALID)) {
-                       /* No huge page support before GMC v9 */
-                       if (cursor.level != AMDGPU_VM_PTB) {
-                               if (!amdgpu_vm_pt_descendant(adev, &cursor))
-                                       return -ENOENT;
-                               continue;
-                       }
-               } else if (frag < shift) {
-                       /* We can't use this level when the fragment size is
-                        * smaller than the address shift. Go to the next
-                        * child entry and try again.
-                        */
-                       if (amdgpu_vm_pt_descendant(adev, &cursor))
-                               continue;
-               } else if (frag >= parent_shift) {
-                       /* If the fragment size is even larger than the parent
-                        * shift we should go up one level and check it again.
-                        */
-                       if (!amdgpu_vm_pt_ancestor(&cursor))
-                               return -EINVAL;
-                       continue;
-               }
+       struct amdgpu_vm_tlb_seq_cb *tlb_cb;
  
-               pt = cursor.entry->bo;
-               if (!pt) {
-                       /* We need all PDs and PTs for mapping something, */
-                       if (flags & AMDGPU_PTE_VALID)
-                               return -ENOENT;
-                       /* but unmapping something can happen at a higher
-                        * level.
-                        */
-                       if (!amdgpu_vm_pt_ancestor(&cursor))
-                               return -EINVAL;
-                       pt = cursor.entry->bo;
-                       shift = parent_shift;
-                       frag_end = max(frag_end, ALIGN(frag_start + 1,
-                                  1ULL << shift));
-               }
-               /* Looks good so far, calculate parameters for the update */
-               incr = (uint64_t)AMDGPU_GPU_PAGE_SIZE << shift;
-               mask = amdgpu_vm_entries_mask(adev, cursor.level);
-               pe_start = ((cursor.pfn >> shift) & mask) * 8;
-               entry_end = ((uint64_t)mask + 1) << shift;
-               entry_end += cursor.pfn & ~(entry_end - 1);
-               entry_end = min(entry_end, end);
-               do {
-                       struct amdgpu_vm *vm = params->vm;
-                       uint64_t upd_end = min(entry_end, frag_end);
-                       unsigned nptes = (upd_end - frag_start) >> shift;
-                       uint64_t upd_flags = flags | AMDGPU_PTE_FRAG(frag);
-                       /* This can happen when we set higher level PDs to
-                        * silent to stop fault floods.
-                        */
-                       nptes = max(nptes, 1u);
-                       trace_amdgpu_vm_update_ptes(params, frag_start, upd_end,
-                                                   min(nptes, 32u), dst, incr, upd_flags,
-                                                   vm->task_info.pid,
-                                                   vm->immediate.fence_context);
-                       amdgpu_vm_update_flags(params, to_amdgpu_bo_vm(pt),
-                                              cursor.level, pe_start, dst,
-                                              nptes, incr, upd_flags);
-                       pe_start += nptes * 8;
-                       dst += nptes * incr;
-                       frag_start = upd_end;
-                       if (frag_start >= frag_end) {
-                               /* figure out the next fragment */
-                               amdgpu_vm_fragment(params, frag_start, end,
-                                                  flags, &frag, &frag_end);
-                               if (frag < shift)
-                                       break;
-                       }
-               } while (frag_start < entry_end);
-               if (amdgpu_vm_pt_descendant(adev, &cursor)) {
-                       /* Free all child entries.
-                        * Update the tables with the flags and addresses and free up subsequent
-                        * tables in the case of huge pages or freed up areas.
-                        * This is the maximum you can free, because all other page tables are not
-                        * completely covered by the range and so potentially still in use.
-                        */
-                       while (cursor.pfn < frag_start) {
-                               /* Make sure previous mapping is freed */
-                               if (cursor.entry->bo) {
-                                       params->table_freed = true;
-                                       amdgpu_vm_free_pts(adev, params->vm, &cursor);
-                               }
-                               amdgpu_vm_pt_next(adev, &cursor);
-                       }
-               } else if (frag >= shift) {
-                       /* or just move on to the next on the same level. */
-                       amdgpu_vm_pt_next(adev, &cursor);
-               }
-       }
-       return 0;
+       tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
+       atomic64_inc(&tlb_cb->vm->tlb_seq);
+       kfree(tlb_cb);
  }
  
  /**
-  * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
+  * amdgpu_vm_update_range - update a range in the vm page table
   *
-  * @adev: amdgpu_device pointer of the VM
-  * @bo_adev: amdgpu_device pointer of the mapped BO
-  * @vm: requested vm
+  * @adev: amdgpu_device pointer to use for commands
+  * @vm: the VM to update the range
   * @immediate: immediate submission in a page fault
   * @unlocked: unlocked invalidation during MM callback
+  * @flush_tlb: trigger tlb invalidation after update completed
   * @resv: fences we need to sync to
   * @start: start of mapped range
   * @last: last mapped entry
   * @flags: flags for the entries
   * @offset: offset into nodes and pages_addr
+  * @vram_base: base for vram mappings
   * @res: ttm_resource to map
   * @pages_addr: DMA addresses to use for mapping
   * @fence: optional resulting fence
-  * @table_freed: return true if page table is freed
   *
   * Fill in the page table entries between @start and @last.
   *
   * Returns:
-  * 0 for success, -EINVAL for failure.
+  * 0 for success, negative erro code for failure.
   */
- int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
-                               struct amdgpu_device *bo_adev,
-                               struct amdgpu_vm *vm, bool immediate,
-                               bool unlocked, struct dma_resv *resv,
-                               uint64_t start, uint64_t last,
-                               uint64_t flags, uint64_t offset,
-                               struct ttm_resource *res,
-                               dma_addr_t *pages_addr,
-                               struct dma_fence **fence,
-                               bool *table_freed)
+ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+                          bool immediate, bool unlocked, bool flush_tlb,
+                          struct dma_resv *resv, uint64_t start, uint64_t last,
+                          uint64_t flags, uint64_t offset, uint64_t vram_base,
+                          struct ttm_resource *res, dma_addr_t *pages_addr,
+                          struct dma_fence **fence)
  {
        struct amdgpu_vm_update_params params;
+       struct amdgpu_vm_tlb_seq_cb *tlb_cb;
        struct amdgpu_res_cursor cursor;
        enum amdgpu_sync_mode sync_mode;
        int r, idx;
        if (!drm_dev_enter(adev_to_drm(adev), &idx))
                return -ENODEV;
  
+       tlb_cb = kmalloc(sizeof(*tlb_cb), GFP_KERNEL);
+       if (!tlb_cb) {
+               r = -ENOMEM;
+               goto error_unlock;
+       }
+       /* Vega20+XGMI where PTEs get inadvertently cached in L2 texture cache,
+        * heavy-weight flush TLB unconditionally.
+        */
+       flush_tlb |= adev->gmc.xgmi.num_physical_nodes &&
+                    adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0);
        memset(&params, 0, sizeof(params));
        params.adev = adev;
        params.vm = vm;
        amdgpu_vm_eviction_lock(vm);
        if (vm->evicting) {
                r = -EBUSY;
-               goto error_unlock;
+               goto error_free;
        }
  
        if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) {
  
        r = vm->update_funcs->prepare(&params, resv, sync_mode);
        if (r)
-               goto error_unlock;
+               goto error_free;
  
        amdgpu_res_first(pages_addr ? NULL : res, offset,
                         (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, &cursor);
                        }
  
                } else if (flags & (AMDGPU_PTE_VALID | AMDGPU_PTE_PRT)) {
-                       addr = bo_adev->vm_manager.vram_base_offset +
-                               cursor.start;
+                       addr = vram_base + cursor.start;
                } else {
                        addr = 0;
                }
  
                tmp = start + num_entries;
-               r = amdgpu_vm_update_ptes(&params, start, tmp, addr, flags);
+               r = amdgpu_vm_ptes_update(&params, start, tmp, addr, flags);
                if (r)
-                       goto error_unlock;
+                       goto error_free;
  
                amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
                start = tmp;
  
        r = vm->update_funcs->commit(&params, fence);
  
-       if (table_freed)
-               *table_freed = *table_freed || params.table_freed;
+       if (flush_tlb || params.table_freed) {
+               tlb_cb->vm = vm;
+               if (fence && *fence &&
+                   !dma_fence_add_callback(*fence, &tlb_cb->cb,
+                                          amdgpu_vm_tlb_seq_cb)) {
+                       dma_fence_put(vm->last_tlb_flush);
+                       vm->last_tlb_flush = dma_fence_get(*fence);
+               } else {
+                       amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
+               }
+               tlb_cb = NULL;
+       }
+ error_free:
+       kfree(tlb_cb);
  
  error_unlock:
        amdgpu_vm_eviction_unlock(vm);
@@@ -1822,7 -1002,6 +944,6 @@@ void amdgpu_vm_get_memory(struct amdgpu
   * @adev: amdgpu_device pointer
   * @bo_va: requested BO and VM object
   * @clear: if true clear the entries
-  * @table_freed: return true if page table is freed
   *
   * Fill in the page table entries for @bo_va.
   *
   * 0 for success, -EINVAL for failure.
   */
  int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
-                       bool clear, bool *table_freed)
+                       bool clear)
  {
        struct amdgpu_bo *bo = bo_va->base.bo;
        struct amdgpu_vm *vm = bo_va->base.vm;
        dma_addr_t *pages_addr = NULL;
        struct ttm_resource *mem;
        struct dma_fence **last_update;
+       bool flush_tlb = clear;
        struct dma_resv *resv;
+       uint64_t vram_base;
        uint64_t flags;
-       struct amdgpu_device *bo_adev = adev;
        int r;
  
        if (clear || !bo) {
        }
  
        if (bo) {
+               struct amdgpu_device *bo_adev;
                flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
  
                if (amdgpu_bo_encrypted(bo))
                        flags |= AMDGPU_PTE_TMZ;
  
                bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
+               vram_base = bo_adev->vm_manager.vram_base_offset;
        } else {
                flags = 0x0;
+               vram_base = 0;
        }
  
        if (clear || (bo && bo->tbo.base.resv ==
                last_update = &bo_va->last_pt_update;
  
        if (!clear && bo_va->base.moved) {
-               bo_va->base.moved = false;
+               flush_tlb = true;
                list_splice_init(&bo_va->valids, &bo_va->invalids);
  
        } else if (bo_va->cleared != clear) {
  
                trace_amdgpu_vm_bo_update(mapping);
  
-               r = amdgpu_vm_bo_update_mapping(adev, bo_adev, vm, false, false,
-                                               resv, mapping->start,
-                                               mapping->last, update_flags,
-                                               mapping->offset, mem,
-                                               pages_addr, last_update, table_freed);
+               r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb,
+                                          resv, mapping->start, mapping->last,
+                                          update_flags, mapping->offset,
+                                          vram_base, mem, pages_addr,
+                                          last_update);
                if (r)
                        return r;
        }
  
        list_splice_init(&bo_va->invalids, &bo_va->valids);
        bo_va->cleared = clear;
+       bo_va->base.moved = false;
  
        if (trace_amdgpu_vm_bo_mapping_enabled()) {
                list_for_each_entry(mapping, &bo_va->valids, list)
@@@ -2059,7 -1244,7 +1186,7 @@@ static void amdgpu_vm_prt_fini(struct a
        struct dma_resv_iter cursor;
        struct dma_fence *fence;
  
 -      dma_resv_for_each_fence(&cursor, resv, true, fence) {
 +      dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, fence) {
                /* Add a callback for each fence in the reservation object */
                amdgpu_vm_prt_get(adev);
                amdgpu_vm_add_prt_cb(adev, fence);
@@@ -2100,10 -1285,10 +1227,10 @@@ int amdgpu_vm_clear_freed(struct amdgpu
                    mapping->start < AMDGPU_GMC_HOLE_START)
                        init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
  
-               r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false,
-                                               resv, mapping->start,
-                                               mapping->last, init_pte_value,
-                                               0, NULL, NULL, &f, NULL);
+               r = amdgpu_vm_update_range(adev, vm, false, false, true, resv,
+                                          mapping->start, mapping->last,
+                                          init_pte_value, 0, 0, NULL, NULL,
+                                          &f);
                amdgpu_vm_free_mapping(adev, vm, mapping, f);
                if (r) {
                        dma_fence_put(f);
@@@ -2145,7 -1330,7 +1272,7 @@@ int amdgpu_vm_handle_moved(struct amdgp
  
        list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
                /* Per VM BOs never need to bo cleared in the page tables */
-               r = amdgpu_vm_bo_update(adev, bo_va, false, NULL);
+               r = amdgpu_vm_bo_update(adev, bo_va, false);
                if (r)
                        return r;
        }
                else
                        clear = true;
  
-               r = amdgpu_vm_bo_update(adev, bo_va, clear, NULL);
+               r = amdgpu_vm_bo_update(adev, bo_va, clear);
                if (r)
                        return r;
  
@@@ -2611,7 -1796,7 +1738,7 @@@ void amdgpu_vm_bo_del(struct amdgpu_dev
        if (bo) {
                dma_resv_assert_held(bo->tbo.base.resv);
                if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv)
 -                      vm->bulk_moveable = false;
 +                      ttm_bo_set_bulk_move(&bo->tbo, NULL);
  
                for (base = &bo_va->base.bo->vm_bo; *base;
                     base = &(*base)->next) {
@@@ -2665,7 -1850,7 +1792,7 @@@ bool amdgpu_vm_evictable(struct amdgpu_
                return true;
  
        /* Don't evict VM page tables while they are busy */
 -      if (!dma_resv_test_signaled(bo->tbo.base.resv, true))
 +      if (!dma_resv_test_signaled(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP))
                return false;
  
        /* Try to block ongoing updates */
@@@ -2845,8 -2030,7 +1972,8 @@@ void amdgpu_vm_adjust_size(struct amdgp
   */
  long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout)
  {
 -      timeout = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv, true,
 +      timeout = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv,
 +                                      DMA_RESV_USAGE_BOOKKEEP,
                                        true, timeout);
        if (timeout <= 0)
                return timeout;
@@@ -2914,6 -2098,7 +2041,7 @@@ int amdgpu_vm_init(struct amdgpu_devic
                vm->update_funcs = &amdgpu_vm_sdma_funcs;
        vm->last_update = NULL;
        vm->last_unlocked = dma_fence_get_stub();
+       vm->last_tlb_flush = dma_fence_get_stub();
  
        mutex_init(&vm->eviction_lock);
        vm->evicting = false;
        if (r)
                goto error_free_root;
  
 -      r = dma_resv_reserve_shared(root_bo->tbo.base.resv, 1);
 +      r = dma_resv_reserve_fences(root_bo->tbo.base.resv, 1);
        if (r)
                goto error_unreserve;
  
        amdgpu_vm_bo_base_init(&vm->root, vm, root_bo);
  
-       r = amdgpu_vm_clear_bo(adev, vm, root, false);
+       r = amdgpu_vm_pt_clear(adev, vm, root, false);
        if (r)
                goto error_unreserve;
  
@@@ -2952,6 -2137,7 +2080,7 @@@ error_free_root
        vm->root.bo = NULL;
  
  error_free_delayed:
+       dma_fence_put(vm->last_tlb_flush);
        dma_fence_put(vm->last_unlocked);
        drm_sched_entity_destroy(&vm->delayed);
  
@@@ -2961,34 -2147,6 +2090,6 @@@ error_free_immediate
        return r;
  }
  
- /**
-  * amdgpu_vm_check_clean_reserved - check if a VM is clean
-  *
-  * @adev: amdgpu_device pointer
-  * @vm: the VM to check
-  *
-  * check all entries of the root PD, if any subsequent PDs are allocated,
-  * it means there are page table creating and filling, and is no a clean
-  * VM
-  *
-  * Returns:
-  *    0 if this VM is clean
-  */
- static int amdgpu_vm_check_clean_reserved(struct amdgpu_device *adev,
-                                         struct amdgpu_vm *vm)
- {
-       enum amdgpu_vm_level root = adev->vm_manager.root_level;
-       unsigned int entries = amdgpu_vm_num_entries(adev, root);
-       unsigned int i = 0;
-       for (i = 0; i < entries; i++) {
-               if (to_amdgpu_bo_vm(vm->root.bo)->entries[i].bo)
-                       return -EINVAL;
-       }
-       return 0;
- }
  /**
   * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
   *
@@@ -3018,17 -2176,17 +2119,17 @@@ int amdgpu_vm_make_compute(struct amdgp
                return r;
  
        /* Sanity checks */
-       r = amdgpu_vm_check_clean_reserved(adev, vm);
-       if (r)
+       if (!amdgpu_vm_pt_is_root_clean(adev, vm)) {
+               r = -EINVAL;
                goto unreserve_bo;
+       }
  
        /* Check if PD needs to be reinitialized and do it before
         * changing any other state, in case it fails.
         */
        if (pte_support_ats != vm->pte_support_ats) {
                vm->pte_support_ats = pte_support_ats;
-               r = amdgpu_vm_clear_bo(adev, vm,
-                                      to_amdgpu_bo_vm(vm->root.bo),
+               r = amdgpu_vm_pt_clear(adev, vm, to_amdgpu_bo_vm(vm->root.bo),
                                       false);
                if (r)
                        goto unreserve_bo;
@@@ -3096,6 -2254,7 +2197,7 @@@ void amdgpu_vm_fini(struct amdgpu_devic
        struct amdgpu_bo_va_mapping *mapping, *tmp;
        bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
        struct amdgpu_bo *root;
+       unsigned long flags;
        int i;
  
        amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
        amdgpu_vm_set_pasid(adev, vm, 0);
        dma_fence_wait(vm->last_unlocked, false);
        dma_fence_put(vm->last_unlocked);
+       dma_fence_wait(vm->last_tlb_flush, false);
+       /* Make sure that all fence callbacks have completed */
+       spin_lock_irqsave(vm->last_tlb_flush->lock, flags);
+       spin_unlock_irqrestore(vm->last_tlb_flush->lock, flags);
+       dma_fence_put(vm->last_tlb_flush);
  
        list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
                if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
                amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
        }
  
-       amdgpu_vm_free_pts(adev, vm, NULL);
+       amdgpu_vm_pt_free_root(adev, vm);
        amdgpu_bo_unreserve(root);
        amdgpu_bo_unref(&root);
        WARN_ON(vm->root.bo);
@@@ -3370,15 -2534,14 +2477,14 @@@ bool amdgpu_vm_handle_fault(struct amdg
                value = 0;
        }
  
 -      r = dma_resv_reserve_shared(root->tbo.base.resv, 1);
 +      r = dma_resv_reserve_fences(root->tbo.base.resv, 1);
        if (r) {
                pr_debug("failed %d to reserve fence slot\n", r);
                goto error_unlock;
        }
  
-       r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr,
-                                       addr, flags, value, NULL, NULL, NULL,
-                                       NULL);
+       r = amdgpu_vm_update_range(adev, vm, true, false, false, NULL, addr,
+                                  addr, flags, value, 0, NULL, NULL, NULL);
        if (r)
                goto error_unlock;
  
index bd7892482bbfa85d706671998fff3772b0204288,6b06a214f05f483a4cd674f2d14a44fbfaf27553..9ecb7f663e1967d57ee9f8e9802bfb6eaa13feb9
@@@ -284,6 -284,10 +284,10 @@@ struct amdgpu_vm 
        struct drm_sched_entity immediate;
        struct drm_sched_entity delayed;
  
+       /* Last finished delayed update */
+       atomic64_t              tlb_seq;
+       struct dma_fence        *last_tlb_flush;
        /* Last unlocked submission to the scheduler entities */
        struct dma_fence        *last_unlocked;
  
  
        /* Store positions of group of BOs */
        struct ttm_lru_bulk_move lru_bulk_move;
 -      /* mark whether can do the bulk move */
 -      bool                    bulk_moveable;
        /* Flag to indicate if VM is used for compute */
        bool                    is_compute_context;
  };
@@@ -395,18 -401,17 +399,17 @@@ int amdgpu_vm_clear_freed(struct amdgpu
                          struct dma_fence **fence);
  int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
                           struct amdgpu_vm *vm);
- int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
-                               struct amdgpu_device *bo_adev,
-                               struct amdgpu_vm *vm, bool immediate,
-                               bool unlocked, struct dma_resv *resv,
-                               uint64_t start, uint64_t last,
-                               uint64_t flags, uint64_t offset,
-                               struct ttm_resource *res,
-                               dma_addr_t *pages_addr,
-                               struct dma_fence **fence, bool *free_table);
+ void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
+                           struct amdgpu_vm *vm, struct amdgpu_bo *bo);
+ int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+                          bool immediate, bool unlocked, bool flush_tlb,
+                          struct dma_resv *resv, uint64_t start, uint64_t last,
+                          uint64_t flags, uint64_t offset, uint64_t vram_base,
+                          struct ttm_resource *res, dma_addr_t *pages_addr,
+                          struct dma_fence **fence);
  int amdgpu_vm_bo_update(struct amdgpu_device *adev,
                        struct amdgpu_bo_va *bo_va,
-                       bool clear, bool *table_freed);
+                       bool clear);
  bool amdgpu_vm_evictable(struct amdgpu_bo *bo);
  void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
                             struct amdgpu_bo *bo, bool evicted);
@@@ -452,11 -457,38 +455,37 @@@ void amdgpu_vm_set_task_info(struct amd
  
  void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
                                struct amdgpu_vm *vm);
 -void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo);
  void amdgpu_vm_get_memory(struct amdgpu_vm *vm, uint64_t *vram_mem,
                                uint64_t *gtt_mem, uint64_t *cpu_mem);
  
+ int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+                      struct amdgpu_bo_vm *vmbo, bool immediate);
+ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+                       int level, bool immediate, struct amdgpu_bo_vm **vmbo);
+ void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm);
+ bool amdgpu_vm_pt_is_root_clean(struct amdgpu_device *adev,
+                               struct amdgpu_vm *vm);
+ int amdgpu_vm_pde_update(struct amdgpu_vm_update_params *params,
+                        struct amdgpu_vm_bo_base *entry);
+ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,
+                         uint64_t start, uint64_t end,
+                         uint64_t dst, uint64_t flags);
  #if defined(CONFIG_DEBUG_FS)
  void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);
  #endif
  
+ /**
+  * amdgpu_vm_tlb_seq - return tlb flush sequence number
+  * @vm: the amdgpu_vm structure to query
+  *
+  * Returns the tlb flush sequence number which indicates that the VM TLBs needs
+  * to be invalidated whenever the sequence number change.
+  */
+ static inline uint64_t amdgpu_vm_tlb_seq(struct amdgpu_vm *vm)
+ {
+       return atomic64_read(&vm->tlb_seq);
+ }
  #endif
index 0000000000000000000000000000000000000000,958d7ed978826f9ed6b4e2fc1a50a5f33736ce0d..7761a3ea172e297e38d8afd00b9ebca4ec08ec31
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,979 +1,977 @@@
 -      vm->bulk_moveable = false;
 -
+ // SPDX-License-Identifier: GPL-2.0 OR MIT
+ /*
+  * Copyright 2022 Advanced Micro Devices, Inc.
+  *
+  * Permission is hereby granted, free of charge, to any person obtaining a
+  * copy of this software and associated documentation files (the "Software"),
+  * to deal in the Software without restriction, including without limitation
+  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+  * and/or sell copies of the Software, and to permit persons to whom the
+  * Software is furnished to do so, subject to the following conditions:
+  *
+  * The above copyright notice and this permission notice shall be included in
+  * all copies or substantial portions of the Software.
+  *
+  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+  * OTHER DEALINGS IN THE SOFTWARE.
+  */
+ #include <drm/drm_drv.h>
+ #include "amdgpu.h"
+ #include "amdgpu_trace.h"
+ #include "amdgpu_vm.h"
+ /*
+  * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt
+  */
+ struct amdgpu_vm_pt_cursor {
+       uint64_t pfn;
+       struct amdgpu_vm_bo_base *parent;
+       struct amdgpu_vm_bo_base *entry;
+       unsigned int level;
+ };
+ /**
+  * amdgpu_vm_pt_level_shift - return the addr shift for each level
+  *
+  * @adev: amdgpu_device pointer
+  * @level: VMPT level
+  *
+  * Returns:
+  * The number of bits the pfn needs to be right shifted for a level.
+  */
+ static unsigned int amdgpu_vm_pt_level_shift(struct amdgpu_device *adev,
+                                            unsigned int level)
+ {
+       switch (level) {
+       case AMDGPU_VM_PDB2:
+       case AMDGPU_VM_PDB1:
+       case AMDGPU_VM_PDB0:
+               return 9 * (AMDGPU_VM_PDB0 - level) +
+                       adev->vm_manager.block_size;
+       case AMDGPU_VM_PTB:
+               return 0;
+       default:
+               return ~0;
+       }
+ }
+ /**
+  * amdgpu_vm_pt_num_entries - return the number of entries in a PD/PT
+  *
+  * @adev: amdgpu_device pointer
+  * @level: VMPT level
+  *
+  * Returns:
+  * The number of entries in a page directory or page table.
+  */
+ static unsigned int amdgpu_vm_pt_num_entries(struct amdgpu_device *adev,
+                                            unsigned int level)
+ {
+       unsigned int shift;
+       shift = amdgpu_vm_pt_level_shift(adev, adev->vm_manager.root_level);
+       if (level == adev->vm_manager.root_level)
+               /* For the root directory */
+               return round_up(adev->vm_manager.max_pfn, 1ULL << shift)
+                       >> shift;
+       else if (level != AMDGPU_VM_PTB)
+               /* Everything in between */
+               return 512;
+       /* For the page tables on the leaves */
+       return AMDGPU_VM_PTE_COUNT(adev);
+ }
+ /**
+  * amdgpu_vm_pt_num_ats_entries - return the number of ATS entries in the root PD
+  *
+  * @adev: amdgpu_device pointer
+  *
+  * Returns:
+  * The number of entries in the root page directory which needs the ATS setting.
+  */
+ static unsigned int amdgpu_vm_pt_num_ats_entries(struct amdgpu_device *adev)
+ {
+       unsigned int shift;
+       shift = amdgpu_vm_pt_level_shift(adev, adev->vm_manager.root_level);
+       return AMDGPU_GMC_HOLE_START >> (shift + AMDGPU_GPU_PAGE_SHIFT);
+ }
+ /**
+  * amdgpu_vm_pt_entries_mask - the mask to get the entry number of a PD/PT
+  *
+  * @adev: amdgpu_device pointer
+  * @level: VMPT level
+  *
+  * Returns:
+  * The mask to extract the entry number of a PD/PT from an address.
+  */
+ static uint32_t amdgpu_vm_pt_entries_mask(struct amdgpu_device *adev,
+                                         unsigned int level)
+ {
+       if (level <= adev->vm_manager.root_level)
+               return 0xffffffff;
+       else if (level != AMDGPU_VM_PTB)
+               return 0x1ff;
+       else
+               return AMDGPU_VM_PTE_COUNT(adev) - 1;
+ }
+ /**
+  * amdgpu_vm_pt_size - returns the size of the page table in bytes
+  *
+  * @adev: amdgpu_device pointer
+  * @level: VMPT level
+  *
+  * Returns:
+  * The size of the BO for a page directory or page table in bytes.
+  */
+ static unsigned int amdgpu_vm_pt_size(struct amdgpu_device *adev,
+                                     unsigned int level)
+ {
+       return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_pt_num_entries(adev, level) * 8);
+ }
+ /**
+  * amdgpu_vm_pt_parent - get the parent page directory
+  *
+  * @pt: child page table
+  *
+  * Helper to get the parent entry for the child page table. NULL if we are at
+  * the root page directory.
+  */
+ static struct amdgpu_vm_bo_base *
+ amdgpu_vm_pt_parent(struct amdgpu_vm_bo_base *pt)
+ {
+       struct amdgpu_bo *parent = pt->bo->parent;
+       if (!parent)
+               return NULL;
+       return parent->vm_bo;
+ }
+ /**
+  * amdgpu_vm_pt_start - start PD/PT walk
+  *
+  * @adev: amdgpu_device pointer
+  * @vm: amdgpu_vm structure
+  * @start: start address of the walk
+  * @cursor: state to initialize
+  *
+  * Initialize a amdgpu_vm_pt_cursor to start a walk.
+  */
+ static void amdgpu_vm_pt_start(struct amdgpu_device *adev,
+                              struct amdgpu_vm *vm, uint64_t start,
+                              struct amdgpu_vm_pt_cursor *cursor)
+ {
+       cursor->pfn = start;
+       cursor->parent = NULL;
+       cursor->entry = &vm->root;
+       cursor->level = adev->vm_manager.root_level;
+ }
+ /**
+  * amdgpu_vm_pt_descendant - go to child node
+  *
+  * @adev: amdgpu_device pointer
+  * @cursor: current state
+  *
+  * Walk to the child node of the current node.
+  * Returns:
+  * True if the walk was possible, false otherwise.
+  */
+ static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev,
+                                   struct amdgpu_vm_pt_cursor *cursor)
+ {
+       unsigned int mask, shift, idx;
+       if ((cursor->level == AMDGPU_VM_PTB) || !cursor->entry ||
+           !cursor->entry->bo)
+               return false;
+       mask = amdgpu_vm_pt_entries_mask(adev, cursor->level);
+       shift = amdgpu_vm_pt_level_shift(adev, cursor->level);
+       ++cursor->level;
+       idx = (cursor->pfn >> shift) & mask;
+       cursor->parent = cursor->entry;
+       cursor->entry = &to_amdgpu_bo_vm(cursor->entry->bo)->entries[idx];
+       return true;
+ }
+ /**
+  * amdgpu_vm_pt_sibling - go to sibling node
+  *
+  * @adev: amdgpu_device pointer
+  * @cursor: current state
+  *
+  * Walk to the sibling node of the current node.
+  * Returns:
+  * True if the walk was possible, false otherwise.
+  */
+ static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev,
+                                struct amdgpu_vm_pt_cursor *cursor)
+ {
+       unsigned int shift, num_entries;
+       struct amdgpu_bo_vm *parent;
+       /* Root doesn't have a sibling */
+       if (!cursor->parent)
+               return false;
+       /* Go to our parents and see if we got a sibling */
+       shift = amdgpu_vm_pt_level_shift(adev, cursor->level - 1);
+       num_entries = amdgpu_vm_pt_num_entries(adev, cursor->level - 1);
+       parent = to_amdgpu_bo_vm(cursor->parent->bo);
+       if (cursor->entry == &parent->entries[num_entries - 1])
+               return false;
+       cursor->pfn += 1ULL << shift;
+       cursor->pfn &= ~((1ULL << shift) - 1);
+       ++cursor->entry;
+       return true;
+ }
+ /**
+  * amdgpu_vm_pt_ancestor - go to parent node
+  *
+  * @cursor: current state
+  *
+  * Walk to the parent node of the current node.
+  * Returns:
+  * True if the walk was possible, false otherwise.
+  */
+ static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor)
+ {
+       if (!cursor->parent)
+               return false;
+       --cursor->level;
+       cursor->entry = cursor->parent;
+       cursor->parent = amdgpu_vm_pt_parent(cursor->parent);
+       return true;
+ }
+ /**
+  * amdgpu_vm_pt_next - get next PD/PT in hieratchy
+  *
+  * @adev: amdgpu_device pointer
+  * @cursor: current state
+  *
+  * Walk the PD/PT tree to the next node.
+  */
+ static void amdgpu_vm_pt_next(struct amdgpu_device *adev,
+                             struct amdgpu_vm_pt_cursor *cursor)
+ {
+       /* First try a newborn child */
+       if (amdgpu_vm_pt_descendant(adev, cursor))
+               return;
+       /* If that didn't worked try to find a sibling */
+       while (!amdgpu_vm_pt_sibling(adev, cursor)) {
+               /* No sibling, go to our parents and grandparents */
+               if (!amdgpu_vm_pt_ancestor(cursor)) {
+                       cursor->pfn = ~0ll;
+                       return;
+               }
+       }
+ }
+ /**
+  * amdgpu_vm_pt_first_dfs - start a deep first search
+  *
+  * @adev: amdgpu_device structure
+  * @vm: amdgpu_vm structure
+  * @start: optional cursor to start with
+  * @cursor: state to initialize
+  *
+  * Starts a deep first traversal of the PD/PT tree.
+  */
+ static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev,
+                                  struct amdgpu_vm *vm,
+                                  struct amdgpu_vm_pt_cursor *start,
+                                  struct amdgpu_vm_pt_cursor *cursor)
+ {
+       if (start)
+               *cursor = *start;
+       else
+               amdgpu_vm_pt_start(adev, vm, 0, cursor);
+       while (amdgpu_vm_pt_descendant(adev, cursor))
+               ;
+ }
+ /**
+  * amdgpu_vm_pt_continue_dfs - check if the deep first search should continue
+  *
+  * @start: starting point for the search
+  * @entry: current entry
+  *
+  * Returns:
+  * True when the search should continue, false otherwise.
+  */
+ static bool amdgpu_vm_pt_continue_dfs(struct amdgpu_vm_pt_cursor *start,
+                                     struct amdgpu_vm_bo_base *entry)
+ {
+       return entry && (!start || entry != start->entry);
+ }
+ /**
+  * amdgpu_vm_pt_next_dfs - get the next node for a deep first search
+  *
+  * @adev: amdgpu_device structure
+  * @cursor: current state
+  *
+  * Move the cursor to the next node in a deep first search.
+  */
+ static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev,
+                                 struct amdgpu_vm_pt_cursor *cursor)
+ {
+       if (!cursor->entry)
+               return;
+       if (!cursor->parent)
+               cursor->entry = NULL;
+       else if (amdgpu_vm_pt_sibling(adev, cursor))
+               while (amdgpu_vm_pt_descendant(adev, cursor))
+                       ;
+       else
+               amdgpu_vm_pt_ancestor(cursor);
+ }
+ /*
+  * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs
+  */
+ #define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)                \
+       for (amdgpu_vm_pt_first_dfs((adev), (vm), (start), &(cursor)),          \
+            (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\
+            amdgpu_vm_pt_continue_dfs((start), (entry));                       \
+            (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor)))
+ /**
+  * amdgpu_vm_pt_clear - initially clear the PDs/PTs
+  *
+  * @adev: amdgpu_device pointer
+  * @vm: VM to clear BO from
+  * @vmbo: BO to clear
+  * @immediate: use an immediate update
+  *
+  * Root PD needs to be reserved when calling this.
+  *
+  * Returns:
+  * 0 on success, errno otherwise.
+  */
+ int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+                      struct amdgpu_bo_vm *vmbo, bool immediate)
+ {
+       unsigned int level = adev->vm_manager.root_level;
+       struct ttm_operation_ctx ctx = { true, false };
+       struct amdgpu_vm_update_params params;
+       struct amdgpu_bo *ancestor = &vmbo->bo;
+       unsigned int entries, ats_entries;
+       struct amdgpu_bo *bo = &vmbo->bo;
+       uint64_t addr;
+       int r, idx;
+       /* Figure out our place in the hierarchy */
+       if (ancestor->parent) {
+               ++level;
+               while (ancestor->parent->parent) {
+                       ++level;
+                       ancestor = ancestor->parent;
+               }
+       }
+       entries = amdgpu_bo_size(bo) / 8;
+       if (!vm->pte_support_ats) {
+               ats_entries = 0;
+       } else if (!bo->parent) {
+               ats_entries = amdgpu_vm_pt_num_ats_entries(adev);
+               ats_entries = min(ats_entries, entries);
+               entries -= ats_entries;
+       } else {
+               struct amdgpu_vm_bo_base *pt;
+               pt = ancestor->vm_bo;
+               ats_entries = amdgpu_vm_pt_num_ats_entries(adev);
+               if ((pt - to_amdgpu_bo_vm(vm->root.bo)->entries) >=
+                   ats_entries) {
+                       ats_entries = 0;
+               } else {
+                       ats_entries = entries;
+                       entries = 0;
+               }
+       }
+       r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+       if (r)
+               return r;
+       if (vmbo->shadow) {
+               struct amdgpu_bo *shadow = vmbo->shadow;
+               r = ttm_bo_validate(&shadow->tbo, &shadow->placement, &ctx);
+               if (r)
+                       return r;
+       }
+       if (!drm_dev_enter(adev_to_drm(adev), &idx))
+               return -ENODEV;
+       r = vm->update_funcs->map_table(vmbo);
+       if (r)
+               goto exit;
+       memset(&params, 0, sizeof(params));
+       params.adev = adev;
+       params.vm = vm;
+       params.immediate = immediate;
+       r = vm->update_funcs->prepare(&params, NULL, AMDGPU_SYNC_EXPLICIT);
+       if (r)
+               goto exit;
+       addr = 0;
+       if (ats_entries) {
+               uint64_t value = 0, flags;
+               flags = AMDGPU_PTE_DEFAULT_ATC;
+               if (level != AMDGPU_VM_PTB) {
+                       /* Handle leaf PDEs as PTEs */
+                       flags |= AMDGPU_PDE_PTE;
+                       amdgpu_gmc_get_vm_pde(adev, level, &value, &flags);
+               }
+               r = vm->update_funcs->update(&params, vmbo, addr, 0,
+                                            ats_entries, value, flags);
+               if (r)
+                       goto exit;
+               addr += ats_entries * 8;
+       }
+       if (entries) {
+               uint64_t value = 0, flags = 0;
+               if (adev->asic_type >= CHIP_VEGA10) {
+                       if (level != AMDGPU_VM_PTB) {
+                               /* Handle leaf PDEs as PTEs */
+                               flags |= AMDGPU_PDE_PTE;
+                               amdgpu_gmc_get_vm_pde(adev, level,
+                                                     &value, &flags);
+                       } else {
+                               /* Workaround for fault priority problem on GMC9 */
+                               flags = AMDGPU_PTE_EXECUTABLE;
+                       }
+               }
+               r = vm->update_funcs->update(&params, vmbo, addr, 0, entries,
+                                            value, flags);
+               if (r)
+                       goto exit;
+       }
+       r = vm->update_funcs->commit(&params, NULL);
+ exit:
+       drm_dev_exit(idx);
+       return r;
+ }
+ /**
+  * amdgpu_vm_pt_create - create bo for PD/PT
+  *
+  * @adev: amdgpu_device pointer
+  * @vm: requesting vm
+  * @level: the page table level
+  * @immediate: use a immediate update
+  * @vmbo: pointer to the buffer object pointer
+  */
+ int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+                       int level, bool immediate, struct amdgpu_bo_vm **vmbo)
+ {
+       struct amdgpu_bo_param bp;
+       struct amdgpu_bo *bo;
+       struct dma_resv *resv;
+       unsigned int num_entries;
+       int r;
+       memset(&bp, 0, sizeof(bp));
+       bp.size = amdgpu_vm_pt_size(adev, level);
+       bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
+       bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
+       bp.domain = amdgpu_bo_get_preferred_domain(adev, bp.domain);
+       bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
+               AMDGPU_GEM_CREATE_CPU_GTT_USWC;
+       if (level < AMDGPU_VM_PTB)
+               num_entries = amdgpu_vm_pt_num_entries(adev, level);
+       else
+               num_entries = 0;
+       bp.bo_ptr_size = struct_size((*vmbo), entries, num_entries);
+       if (vm->use_cpu_for_update)
+               bp.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+       bp.type = ttm_bo_type_kernel;
+       bp.no_wait_gpu = immediate;
+       if (vm->root.bo)
+               bp.resv = vm->root.bo->tbo.base.resv;
+       r = amdgpu_bo_create_vm(adev, &bp, vmbo);
+       if (r)
+               return r;
+       bo = &(*vmbo)->bo;
+       if (vm->is_compute_context || (adev->flags & AMD_IS_APU)) {
+               (*vmbo)->shadow = NULL;
+               return 0;
+       }
+       if (!bp.resv)
+               WARN_ON(dma_resv_lock(bo->tbo.base.resv,
+                                     NULL));
+       resv = bp.resv;
+       memset(&bp, 0, sizeof(bp));
+       bp.size = amdgpu_vm_pt_size(adev, level);
+       bp.domain = AMDGPU_GEM_DOMAIN_GTT;
+       bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
+       bp.type = ttm_bo_type_kernel;
+       bp.resv = bo->tbo.base.resv;
+       bp.bo_ptr_size = sizeof(struct amdgpu_bo);
+       r = amdgpu_bo_create(adev, &bp, &(*vmbo)->shadow);
+       if (!resv)
+               dma_resv_unlock(bo->tbo.base.resv);
+       if (r) {
+               amdgpu_bo_unref(&bo);
+               return r;
+       }
+       (*vmbo)->shadow->parent = amdgpu_bo_ref(bo);
+       amdgpu_bo_add_to_shadow_list(*vmbo);
+       return 0;
+ }
+ /**
+  * amdgpu_vm_pt_alloc - Allocate a specific page table
+  *
+  * @adev: amdgpu_device pointer
+  * @vm: VM to allocate page tables for
+  * @cursor: Which page table to allocate
+  * @immediate: use an immediate update
+  *
+  * Make sure a specific page table or directory is allocated.
+  *
+  * Returns:
+  * 1 if page table needed to be allocated, 0 if page table was already
+  * allocated, negative errno if an error occurred.
+  */
+ static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
+                             struct amdgpu_vm *vm,
+                             struct amdgpu_vm_pt_cursor *cursor,
+                             bool immediate)
+ {
+       struct amdgpu_vm_bo_base *entry = cursor->entry;
+       struct amdgpu_bo *pt_bo;
+       struct amdgpu_bo_vm *pt;
+       int r;
+       if (entry->bo)
+               return 0;
+       r = amdgpu_vm_pt_create(adev, vm, cursor->level, immediate, &pt);
+       if (r)
+               return r;
+       /* Keep a reference to the root directory to avoid
+        * freeing them up in the wrong order.
+        */
+       pt_bo = &pt->bo;
+       pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
+       amdgpu_vm_bo_base_init(entry, vm, pt_bo);
+       r = amdgpu_vm_pt_clear(adev, vm, pt, immediate);
+       if (r)
+               goto error_free_pt;
+       return 0;
+ error_free_pt:
+       amdgpu_bo_unref(&pt->shadow);
+       amdgpu_bo_unref(&pt_bo);
+       return r;
+ }
+ /**
+  * amdgpu_vm_pt_free - free one PD/PT
+  *
+  * @entry: PDE to free
+  */
+ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
+ {
+       struct amdgpu_bo *shadow;
+       if (!entry->bo)
+               return;
+       shadow = amdgpu_bo_shadowed(entry->bo);
+       entry->bo->vm_bo = NULL;
+       list_del(&entry->vm_status);
+       amdgpu_bo_unref(&shadow);
+       amdgpu_bo_unref(&entry->bo);
+ }
+ /**
+  * amdgpu_vm_pt_free_dfs - free PD/PT levels
+  *
+  * @adev: amdgpu device structure
+  * @vm: amdgpu vm structure
+  * @start: optional cursor where to start freeing PDs/PTs
+  *
+  * Free the page directory or page table level and all sub levels.
+  */
+ static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
+                                 struct amdgpu_vm *vm,
+                                 struct amdgpu_vm_pt_cursor *start)
+ {
+       struct amdgpu_vm_pt_cursor cursor;
+       struct amdgpu_vm_bo_base *entry;
+       for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
+               amdgpu_vm_pt_free(entry);
+       if (start)
+               amdgpu_vm_pt_free(start->entry);
+ }
+ /**
+  * amdgpu_vm_pt_free_root - free root PD
+  * @adev: amdgpu device structure
+  * @vm: amdgpu vm structure
+  *
+  * Free the root page directory and everything below it.
+  */
+ void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm)
+ {
+       amdgpu_vm_pt_free_dfs(adev, vm, NULL);
+ }
+ /**
+  * amdgpu_vm_pt_is_root_clean - check if a root PD is clean
+  *
+  * @adev: amdgpu_device pointer
+  * @vm: the VM to check
+  *
+  * Check all entries of the root PD, if any subsequent PDs are allocated,
+  * it means there are page table creating and filling, and is no a clean
+  * VM
+  *
+  * Returns:
+  *    0 if this VM is clean
+  */
+ bool amdgpu_vm_pt_is_root_clean(struct amdgpu_device *adev,
+                               struct amdgpu_vm *vm)
+ {
+       enum amdgpu_vm_level root = adev->vm_manager.root_level;
+       unsigned int entries = amdgpu_vm_pt_num_entries(adev, root);
+       unsigned int i = 0;
+       for (i = 0; i < entries; i++) {
+               if (to_amdgpu_bo_vm(vm->root.bo)->entries[i].bo)
+                       return false;
+       }
+       return true;
+ }
+ /**
+  * amdgpu_vm_pde_update - update a single level in the hierarchy
+  *
+  * @params: parameters for the update
+  * @entry: entry to update
+  *
+  * Makes sure the requested entry in parent is up to date.
+  */
+ int amdgpu_vm_pde_update(struct amdgpu_vm_update_params *params,
+                        struct amdgpu_vm_bo_base *entry)
+ {
+       struct amdgpu_vm_bo_base *parent = amdgpu_vm_pt_parent(entry);
+       struct amdgpu_bo *bo = parent->bo, *pbo;
+       struct amdgpu_vm *vm = params->vm;
+       uint64_t pde, pt, flags;
+       unsigned int level;
+       for (level = 0, pbo = bo->parent; pbo; ++level)
+               pbo = pbo->parent;
+       level += params->adev->vm_manager.root_level;
+       amdgpu_gmc_get_pde_for_bo(entry->bo, level, &pt, &flags);
+       pde = (entry - to_amdgpu_bo_vm(parent->bo)->entries) * 8;
+       return vm->update_funcs->update(params, to_amdgpu_bo_vm(bo), pde, pt,
+                                       1, 0, flags);
+ }
+ /*
+  * amdgpu_vm_pte_update_flags - figure out flags for PTE updates
+  *
+  * Make sure to set the right flags for the PTEs at the desired level.
+  */
+ static void amdgpu_vm_pte_update_flags(struct amdgpu_vm_update_params *params,
+                                      struct amdgpu_bo_vm *pt,
+                                      unsigned int level,
+                                      uint64_t pe, uint64_t addr,
+                                      unsigned int count, uint32_t incr,
+                                      uint64_t flags)
+ {
+       if (level != AMDGPU_VM_PTB) {
+               flags |= AMDGPU_PDE_PTE;
+               amdgpu_gmc_get_vm_pde(params->adev, level, &addr, &flags);
+       } else if (params->adev->asic_type >= CHIP_VEGA10 &&
+                  !(flags & AMDGPU_PTE_VALID) &&
+                  !(flags & AMDGPU_PTE_PRT)) {
+               /* Workaround for fault priority problem on GMC9 */
+               flags |= AMDGPU_PTE_EXECUTABLE;
+       }
+       params->vm->update_funcs->update(params, pt, pe, addr, count, incr,
+                                        flags);
+ }
+ /**
+  * amdgpu_vm_pte_fragment - get fragment for PTEs
+  *
+  * @params: see amdgpu_vm_update_params definition
+  * @start: first PTE to handle
+  * @end: last PTE to handle
+  * @flags: hw mapping flags
+  * @frag: resulting fragment size
+  * @frag_end: end of this fragment
+  *
+  * Returns the first possible fragment for the start and end address.
+  */
+ static void amdgpu_vm_pte_fragment(struct amdgpu_vm_update_params *params,
+                                  uint64_t start, uint64_t end, uint64_t flags,
+                                  unsigned int *frag, uint64_t *frag_end)
+ {
+       /**
+        * The MC L1 TLB supports variable sized pages, based on a fragment
+        * field in the PTE. When this field is set to a non-zero value, page
+        * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
+        * flags are considered valid for all PTEs within the fragment range
+        * and corresponding mappings are assumed to be physically contiguous.
+        *
+        * The L1 TLB can store a single PTE for the whole fragment,
+        * significantly increasing the space available for translation
+        * caching. This leads to large improvements in throughput when the
+        * TLB is under pressure.
+        *
+        * The L2 TLB distributes small and large fragments into two
+        * asymmetric partitions. The large fragment cache is significantly
+        * larger. Thus, we try to use large fragments wherever possible.
+        * Userspace can support this by aligning virtual base address and
+        * allocation size to the fragment size.
+        *
+        * Starting with Vega10 the fragment size only controls the L1. The L2
+        * is now directly feed with small/huge/giant pages from the walker.
+        */
+       unsigned int max_frag;
+       if (params->adev->asic_type < CHIP_VEGA10)
+               max_frag = params->adev->vm_manager.fragment_size;
+       else
+               max_frag = 31;
+       /* system pages are non continuously */
+       if (params->pages_addr) {
+               *frag = 0;
+               *frag_end = end;
+               return;
+       }
+       /* This intentionally wraps around if no bit is set */
+       *frag = min_t(unsigned int, ffs(start) - 1, fls64(end - start) - 1);
+       if (*frag >= max_frag) {
+               *frag = max_frag;
+               *frag_end = end & ~((1ULL << max_frag) - 1);
+       } else {
+               *frag_end = start + (1 << *frag);
+       }
+ }
+ /**
+  * amdgpu_vm_ptes_update - make sure that page tables are valid
+  *
+  * @params: see amdgpu_vm_update_params definition
+  * @start: start of GPU address range
+  * @end: end of GPU address range
+  * @dst: destination address to map to, the next dst inside the function
+  * @flags: mapping flags
+  *
+  * Update the page tables in the range @start - @end.
+  *
+  * Returns:
+  * 0 for success, -EINVAL for failure.
+  */
+ int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,
+                         uint64_t start, uint64_t end,
+                         uint64_t dst, uint64_t flags)
+ {
+       struct amdgpu_device *adev = params->adev;
+       struct amdgpu_vm_pt_cursor cursor;
+       uint64_t frag_start = start, frag_end;
+       unsigned int frag;
+       int r;
+       /* figure out the initial fragment */
+       amdgpu_vm_pte_fragment(params, frag_start, end, flags, &frag,
+                              &frag_end);
+       /* walk over the address space and update the PTs */
+       amdgpu_vm_pt_start(adev, params->vm, start, &cursor);
+       while (cursor.pfn < end) {
+               unsigned int shift, parent_shift, mask;
+               uint64_t incr, entry_end, pe_start;
+               struct amdgpu_bo *pt;
+               if (!params->unlocked) {
+                       /* make sure that the page tables covering the
+                        * address range are actually allocated
+                        */
+                       r = amdgpu_vm_pt_alloc(params->adev, params->vm,
+                                              &cursor, params->immediate);
+                       if (r)
+                               return r;
+               }
+               shift = amdgpu_vm_pt_level_shift(adev, cursor.level);
+               parent_shift = amdgpu_vm_pt_level_shift(adev, cursor.level - 1);
+               if (params->unlocked) {
+                       /* Unlocked updates are only allowed on the leaves */
+                       if (amdgpu_vm_pt_descendant(adev, &cursor))
+                               continue;
+               } else if (adev->asic_type < CHIP_VEGA10 &&
+                          (flags & AMDGPU_PTE_VALID)) {
+                       /* No huge page support before GMC v9 */
+                       if (cursor.level != AMDGPU_VM_PTB) {
+                               if (!amdgpu_vm_pt_descendant(adev, &cursor))
+                                       return -ENOENT;
+                               continue;
+                       }
+               } else if (frag < shift) {
+                       /* We can't use this level when the fragment size is
+                        * smaller than the address shift. Go to the next
+                        * child entry and try again.
+                        */
+                       if (amdgpu_vm_pt_descendant(adev, &cursor))
+                               continue;
+               } else if (frag >= parent_shift) {
+                       /* If the fragment size is even larger than the parent
+                        * shift we should go up one level and check it again.
+                        */
+                       if (!amdgpu_vm_pt_ancestor(&cursor))
+                               return -EINVAL;
+                       continue;
+               }
+               pt = cursor.entry->bo;
+               if (!pt) {
+                       /* We need all PDs and PTs for mapping something, */
+                       if (flags & AMDGPU_PTE_VALID)
+                               return -ENOENT;
+                       /* but unmapping something can happen at a higher
+                        * level.
+                        */
+                       if (!amdgpu_vm_pt_ancestor(&cursor))
+                               return -EINVAL;
+                       pt = cursor.entry->bo;
+                       shift = parent_shift;
+                       frag_end = max(frag_end, ALIGN(frag_start + 1,
+                                  1ULL << shift));
+               }
+               /* Looks good so far, calculate parameters for the update */
+               incr = (uint64_t)AMDGPU_GPU_PAGE_SIZE << shift;
+               mask = amdgpu_vm_pt_entries_mask(adev, cursor.level);
+               pe_start = ((cursor.pfn >> shift) & mask) * 8;
+               entry_end = ((uint64_t)mask + 1) << shift;
+               entry_end += cursor.pfn & ~(entry_end - 1);
+               entry_end = min(entry_end, end);
+               do {
+                       struct amdgpu_vm *vm = params->vm;
+                       uint64_t upd_end = min(entry_end, frag_end);
+                       unsigned int nptes = (upd_end - frag_start) >> shift;
+                       uint64_t upd_flags = flags | AMDGPU_PTE_FRAG(frag);
+                       /* This can happen when we set higher level PDs to
+                        * silent to stop fault floods.
+                        */
+                       nptes = max(nptes, 1u);
+                       trace_amdgpu_vm_update_ptes(params, frag_start, upd_end,
+                                                   min(nptes, 32u), dst, incr,
+                                                   upd_flags,
+                                                   vm->task_info.pid,
+                                                   vm->immediate.fence_context);
+                       amdgpu_vm_pte_update_flags(params, to_amdgpu_bo_vm(pt),
+                                                  cursor.level, pe_start, dst,
+                                                  nptes, incr, upd_flags);
+                       pe_start += nptes * 8;
+                       dst += nptes * incr;
+                       frag_start = upd_end;
+                       if (frag_start >= frag_end) {
+                               /* figure out the next fragment */
+                               amdgpu_vm_pte_fragment(params, frag_start, end,
+                                                      flags, &frag, &frag_end);
+                               if (frag < shift)
+                                       break;
+                       }
+               } while (frag_start < entry_end);
+               if (amdgpu_vm_pt_descendant(adev, &cursor)) {
+                       /* Free all child entries.
+                        * Update the tables with the flags and addresses and free up subsequent
+                        * tables in the case of huge pages or freed up areas.
+                        * This is the maximum you can free, because all other page tables are not
+                        * completely covered by the range and so potentially still in use.
+                        */
+                       while (cursor.pfn < frag_start) {
+                               /* Make sure previous mapping is freed */
+                               if (cursor.entry->bo) {
+                                       params->table_freed = true;
+                                       amdgpu_vm_pt_free_dfs(adev, params->vm,
+                                                             &cursor);
+                               }
+                               amdgpu_vm_pt_next(adev, &cursor);
+                       }
+               } else if (frag >= shift) {
+                       /* or just move on to the next on the same level. */
+                       amdgpu_vm_pt_next(adev, &cursor);
+               }
+       }
+       return 0;
+ }
index bdb44cee19d350eb01952a8e95290d7319c215a3,69fba68ff88ea1f408e4a97df90c21293df7b34d..1fd3cbca20a2907f1aaf985ce8a5e8fa56a22ee3
@@@ -109,7 -109,7 +109,7 @@@ static int amdgpu_vm_sdma_commit(struc
        if (p->unlocked) {
                struct dma_fence *tmp = dma_fence_get(f);
  
-               swap(p->vm->last_unlocked, f);
+               swap(p->vm->last_unlocked, tmp);
                dma_fence_put(tmp);
        } else {
                amdgpu_bo_fence(p->vm->root.bo, f, true);
@@@ -204,19 -204,14 +204,19 @@@ static int amdgpu_vm_sdma_update(struc
        struct amdgpu_bo *bo = &vmbo->bo;
        enum amdgpu_ib_pool_type pool = p->immediate ? AMDGPU_IB_POOL_IMMEDIATE
                : AMDGPU_IB_POOL_DELAYED;
 +      struct dma_resv_iter cursor;
        unsigned int i, ndw, nptes;
 +      struct dma_fence *fence;
        uint64_t *pte;
        int r;
  
        /* Wait for PD/PT moves to be completed */
 -      r = amdgpu_sync_fence(&p->job->sync, bo->tbo.moving);
 -      if (r)
 -              return r;
 +      dma_resv_for_each_fence(&cursor, bo->tbo.base.resv,
 +                              DMA_RESV_USAGE_KERNEL, fence) {
 +              r = amdgpu_sync_fence(&p->job->sync, fence);
 +              if (r)
 +                      return r;
 +      }
  
        do {
                ndw = p->num_dw_left;
index 9967a73d5b0f2e41a402a178957b3d8ad5f957f6,8a43def1f6387d90fff0ba8ccba26b2cb1430ea3..644f616b776f31ed5b52f00a30414138fc6c2a4b
@@@ -26,7 -26,6 +26,7 @@@
  
  #include <linux/hashtable.h>
  #include <linux/mmu_notifier.h>
 +#include <linux/memremap.h>
  #include <linux/mutex.h>
  #include <linux/types.h>
  #include <linux/atomic.h>
@@@ -706,6 -705,7 +706,7 @@@ struct kfd_process_device 
        /* VM context for GPUVM allocations */
        struct file *drm_file;
        void *drm_priv;
+       atomic64_t tlb_seq;
  
        /* GPUVM allocations storage */
        struct idr alloc_idr;
@@@ -1016,6 -1016,8 +1017,8 @@@ int kfd_topology_add_device(struct kfd_
  int kfd_topology_remove_device(struct kfd_dev *gpu);
  struct kfd_topology_device *kfd_topology_device_by_proximity_domain(
                                                uint32_t proximity_domain);
+ struct kfd_topology_device *kfd_topology_device_by_proximity_domain_no_lock(
+                                               uint32_t proximity_domain);
  struct kfd_topology_device *kfd_topology_device_by_id(uint32_t gpu_id);
  struct kfd_dev *kfd_device_by_id(uint32_t gpu_id);
  struct kfd_dev *kfd_device_by_pci_dev(const struct pci_dev *pdev);
index b3fc3e9582275a7343eeab7e65a92d2a810b314d,459fa07a3bcc85d998897d77b801700ef44f8fdd..11b395b90a3dcd8cfd9139e6d7f0e345328cce25
@@@ -548,7 -548,7 +548,7 @@@ svm_range_vram_node_new(struct amdgpu_d
                goto reserve_bo_failed;
        }
  
 -      r = dma_resv_reserve_shared(bo->tbo.base.resv, 1);
 +      r = dma_resv_reserve_fences(bo->tbo.base.resv, 1);
        if (r) {
                pr_debug("failed %d to reserve bo\n", r);
                amdgpu_bo_unreserve(bo);
@@@ -1188,9 -1188,9 +1188,9 @@@ svm_range_unmap_from_gpu(struct amdgpu_
  
        pr_debug("[0x%llx 0x%llx]\n", start, last);
  
-       return amdgpu_vm_bo_update_mapping(adev, adev, vm, false, true, NULL,
-                                          start, last, init_pte_value, 0,
-                                          NULL, NULL, fence, NULL);
+       return amdgpu_vm_update_range(adev, vm, false, true, true, NULL, start,
+                                     last, init_pte_value, 0, 0, NULL, NULL,
+                                     fence);
  }
  
  static int
@@@ -1243,7 -1243,6 +1243,6 @@@ svm_range_map_to_gpu(struct kfd_process
  {
        struct amdgpu_device *adev = pdd->dev->adev;
        struct amdgpu_vm *vm = drm_priv_to_vm(pdd->drm_priv);
-       bool table_freed = false;
        uint64_t pte_flags;
        unsigned long last_start;
        int last_domain;
                         (last_domain == SVM_RANGE_VRAM_DOMAIN) ? 1 : 0,
                         pte_flags);
  
-               r = amdgpu_vm_bo_update_mapping(adev, bo_adev, vm, false, false,
-                                               NULL, last_start,
-                                               prange->start + i, pte_flags,
-                                               last_start - prange->start,
-                                               NULL, dma_addr,
-                                               &vm->last_update,
-                                               &table_freed);
+               r = amdgpu_vm_update_range(adev, vm, false, false, false, NULL,
+                                          last_start, prange->start + i,
+                                          pte_flags,
+                                          last_start - prange->start,
+                                          bo_adev ? bo_adev->vm_manager.vram_base_offset : 0,
+                                          NULL, dma_addr, &vm->last_update);
  
                for (j = last_start - prange->start; j <= i; j++)
                        dma_addr[j] |= last_domain;
        if (fence)
                *fence = dma_fence_get(vm->last_update);
  
-       if (table_freed)
-               kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY);
  out:
        return r;
  }
@@@ -1363,6 -1359,8 +1359,8 @@@ svm_range_map_to_gpus(struct svm_range 
                                break;
                        }
                }
+               kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY);
        }
  
        return r;
@@@ -1372,7 -1370,7 +1370,7 @@@ struct svm_validate_context 
        struct kfd_process *process;
        struct svm_range *prange;
        bool intr;
-       unsigned long bitmap[MAX_GPU_INSTANCE];
+       DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
        struct ttm_validate_buffer tv[MAX_GPU_INSTANCE];
        struct list_head validate_list;
        struct ww_acquire_ctx ticket;
@@@ -2687,11 -2685,6 +2685,6 @@@ svm_range_restore_pages(struct amdgpu_d
                pr_debug("kfd process not founded pasid 0x%x\n", pasid);
                return 0;
        }
-       if (!p->xnack_enabled) {
-               pr_debug("XNACK not enabled for pasid 0x%x\n", pasid);
-               r = -EFAULT;
-               goto out;
-       }
        svms = &p->svms;
  
        pr_debug("restoring svms 0x%p fault address 0x%llx\n", svms, addr);
                goto out;
        }
  
+       if (!p->xnack_enabled) {
+               pr_debug("XNACK not enabled for pasid 0x%x\n", pasid);
+               r = -EFAULT;
+               goto out;
+       }
        /* p->lead_thread is available as kfd_process_wq_release flush the work
         * before releasing task ref.
         */
index 73423b805b54ed9c0de5d2a4d0293d3952e448ee,a6c3e1d74124d1b4f175edb5917d61c7bcdec0fe..f732af52bd64c06f2d0271a9111d9ef535e9a998
@@@ -9238,8 -9238,7 +9238,8 @@@ static void amdgpu_dm_commit_planes(str
                 * deadlock during GPU reset when this fence will not signal
                 * but we hold reservation lock for the BO.
                 */
 -              r = dma_resv_wait_timeout(abo->tbo.base.resv, true, false,
 +              r = dma_resv_wait_timeout(abo->tbo.base.resv,
 +                                        DMA_RESV_USAGE_WRITE, false,
                                          msecs_to_jiffies(5000));
                if (unlikely(r <= 0))
                        DRM_ERROR("Waiting for fences timed out!");
                        &bundle->flip_addrs[planes_count].address,
                        afb->tmz_surface, false);
  
-               DRM_DEBUG_ATOMIC("plane: id=%d dcc_en=%d\n",
+               drm_dbg_state(state->dev, "plane: id=%d dcc_en=%d\n",
                                 new_plane_state->plane->index,
                                 bundle->plane_infos[planes_count].dcc.enable);
  
                                dc_plane,
                                bundle->flip_addrs[planes_count].flip_timestamp_in_us);
  
-               DRM_DEBUG_ATOMIC("%s Flipping to hi: 0x%x, low: 0x%x\n",
+               drm_dbg_state(state->dev, "%s Flipping to hi: 0x%x, low: 0x%x\n",
                                 __func__,
                                 bundle->flip_addrs[planes_count].address.grph.addr.high_part,
                                 bundle->flip_addrs[planes_count].address.grph.addr.low_part);
@@@ -9627,7 -9626,7 +9627,7 @@@ static void amdgpu_dm_atomic_commit_tai
                dm_new_crtc_state = to_dm_crtc_state(new_crtc_state);
                dm_old_crtc_state = to_dm_crtc_state(old_crtc_state);
  
-               DRM_DEBUG_ATOMIC(
+               drm_dbg_state(state->dev,
                        "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
                        "planes_changed:%d, mode_changed:%d,active_changed:%d,"
                        "connectors_changed:%d\n",
@@@ -10331,7 -10330,7 +10331,7 @@@ static int dm_update_crtc_state(struct 
        if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
                goto skip_modeset;
  
-       DRM_DEBUG_ATOMIC(
+       drm_dbg_state(state->dev,
                "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, "
                "planes_changed:%d, mode_changed:%d,active_changed:%d,"
                "connectors_changed:%d\n",
index 91af98e6617c64c7d2a5d0d7dea4dd756cb33586,77a27b1b98a6288614c0aa420c18597e56088e95..c2eb119e1f4a6e0e67991c415db810740ec719cb
@@@ -361,6 -361,7 +361,7 @@@ struct drm_panel
  # define DP_PSR_IS_SUPPORTED                1
  # define DP_PSR2_IS_SUPPORTED             2       /* eDP 1.4 */
  # define DP_PSR2_WITH_Y_COORD_IS_SUPPORTED  3     /* eDP 1.4a */
+ # define DP_PSR2_WITH_Y_COORD_ET_SUPPORTED  4     /* eDP 1.5, adopted eDP 1.4b SCR */
  
  #define DP_PSR_CAPS                         0x071   /* XXX 1.2? */
  # define DP_PSR_NO_TRAIN_ON_EXIT            1
  # define DP_PSR_SETUP_TIME_SHIFT            1
  # define DP_PSR2_SU_Y_COORDINATE_REQUIRED   (1 << 4)  /* eDP 1.4a */
  # define DP_PSR2_SU_GRANULARITY_REQUIRED    (1 << 5)  /* eDP 1.4b */
+ # define DP_PSR2_SU_AUX_FRAME_SYNC_NOT_NEEDED (1 << 6)/* eDP 1.5, adopted eDP 1.4b SCR */
  
  #define DP_PSR2_SU_X_GRANULARITY          0x072 /* eDP 1.4b */
  #define DP_PSR2_SU_Y_GRANULARITY          0x074 /* eDP 1.4b */
@@@ -2053,7 -2055,6 +2055,7 @@@ struct drm_dp_aux 
        bool is_remote;
  };
  
 +int drm_dp_dpcd_probe(struct drm_dp_aux *aux, unsigned int offset);
  ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
                         void *buffer, size_t size);
  ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
@@@ -2149,10 -2150,8 +2151,10 @@@ bool drm_dp_read_sink_count_cap(struct 
  int drm_dp_read_sink_count(struct drm_dp_aux *aux);
  
  int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,
 +                                const u8 dpcd[DP_RECEIVER_CAP_SIZE],
                                  u8 caps[DP_LTTPR_COMMON_CAP_SIZE]);
  int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,
 +                             const u8 dpcd[DP_RECEIVER_CAP_SIZE],
                               enum drm_dp_phy dp_phy,
                               u8 caps[DP_LTTPR_PHY_CAP_SIZE]);
  int drm_dp_lttpr_count(const u8 cap[DP_LTTPR_COMMON_CAP_SIZE]);