Merge branch 'drm-next-4.15' of git://people.freedesktop.org/~agd5f/linux into drm...
authorDave Airlie <airlied@redhat.com>
Wed, 27 Sep 2017 22:37:02 +0000 (08:37 +1000)
committerDave Airlie <airlied@redhat.com>
Wed, 27 Sep 2017 22:37:02 +0000 (08:37 +1000)
First feature pull for 4.15.  Highlights:
- Per VM BO support
- Lots of powerplay cleanups
- Powerplay support for CI
- pasid mgr for kfd
- interrupt infrastructure for recoverable page faults
- SR-IOV fixes
- initial GPU reset for vega10
- prime mmap support
- ttm page table debugging improvements
- lots of bug fixes

* 'drm-next-4.15' of git://people.freedesktop.org/~agd5f/linux: (232 commits)
  drm/amdgpu: clarify license in amdgpu_trace_points.c
  drm/amdgpu: Add gem_prime_mmap support
  drm/amd/powerplay: delete dead code in smumgr
  drm/amd/powerplay: delete SMUM_FIELD_MASK
  drm/amd/powerplay: delete SMUM_WAIT_INDIRECT_FIELD
  drm/amd/powerplay: delete SMUM_READ_FIELD
  drm/amd/powerplay: delete SMUM_SET_FIELD
  drm/amd/powerplay: delete SMUM_READ_VFPF_INDIRECT_FIELD
  drm/amd/powerplay: delete SMUM_WRITE_VFPF_INDIRECT_FIELD
  drm/amd/powerplay: delete SMUM_WRITE_FIELD
  drm/amd/powerplay: delete SMU_WRITE_INDIRECT_FIELD
  drm/amd/powerplay: move macros to hwmgr.h
  drm/amd/powerplay: move PHM_WAIT_VFPF_INDIRECT_FIELD to hwmgr.h
  drm/amd/powerplay: move SMUM_WAIT_VFPF_INDIRECT_FIELD_UNEQUAL to hwmgr.h
  drm/amd/powerplay: move SMUM_WAIT_INDIRECT_FIELD_UNEQUAL to hwmgr.h
  drm/amd/powerplay: add new helper functions in hwmgr.h
  drm/amd/powerplay: use SMU_IND_INDEX/DATA_11 pair
  drm/amd/powerplay: refine powerplay code.
  drm/amd/powerplay: delete dead code in hwmgr.h
  drm/amd/powerplay: refine interface in struct pp_smumgr_func
  ...

1  2 
drivers/gpu/drm/Kconfig
drivers/gpu/drm/Makefile
drivers/gpu/drm/amd/amdgpu/amdgpu_mn.c
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h

diff --combined drivers/gpu/drm/Kconfig
@@@ -110,7 -110,7 +110,7 @@@ config DRM_FBDEV_OVERALLO
  
  config DRM_LOAD_EDID_FIRMWARE
        bool "Allow to specify an EDID data set instead of probing for it"
 -      depends on DRM_KMS_HELPER
 +      depends on DRM
        help
          Say Y here, if you want to use EDID data to be loaded from the
          /lib/firmware directory or one of the provided built-in
@@@ -184,6 -184,7 +184,7 @@@ config DRM_AMDGP
        select BACKLIGHT_CLASS_DEVICE
        select BACKLIGHT_LCD_SUPPORT
        select INTERVAL_TREE
+       select CHASH
        help
          Choose this option if you have a recent AMD Radeon graphics card.
  
  
  source "drivers/gpu/drm/amd/amdgpu/Kconfig"
  
+ source "drivers/gpu/drm/amd/lib/Kconfig"
  source "drivers/gpu/drm/nouveau/Kconfig"
  
  source "drivers/gpu/drm/i915/Kconfig"
@@@ -278,8 -281,6 +281,8 @@@ source "drivers/gpu/drm/tinydrm/Kconfig
  
  source "drivers/gpu/drm/pl111/Kconfig"
  
 +source "drivers/gpu/drm/tve200/Kconfig"
 +
  # Keep legacy drivers last
  
  menuconfig DRM_LEGACY
diff --combined drivers/gpu/drm/Makefile
@@@ -28,7 -28,6 +28,7 @@@ drm-$(CONFIG_DRM_PANEL) += drm_panel.
  drm-$(CONFIG_OF) += drm_of.o
  drm-$(CONFIG_AGP) += drm_agpsupport.o
  drm-$(CONFIG_DEBUG_FS) += drm_debugfs.o drm_debugfs_crc.o
 +drm-$(CONFIG_DRM_LOAD_EDID_FIRMWARE) += drm_edid_load.o
  
  drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_probe_helper.o \
                drm_plane_helper.o drm_dp_mst_topology.o drm_atomic_helper.o \
@@@ -37,6 -36,7 +37,6 @@@
                drm_scdc_helper.o drm_gem_framebuffer_helper.o
  
  drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
 -drm_kms_helper-$(CONFIG_DRM_LOAD_EDID_FIRMWARE) += drm_edid_load.o
  drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
  drm_kms_helper-$(CONFIG_DRM_KMS_CMA_HELPER) += drm_fb_cma_helper.o
  drm_kms_helper-$(CONFIG_DRM_DP_AUX_CHARDEV) += drm_dp_aux_dev.o
  obj-$(CONFIG_DRM_KMS_HELPER) += drm_kms_helper.o
  obj-$(CONFIG_DRM_DEBUG_MM_SELFTEST) += selftests/
  
 -CFLAGS_drm_trace_points.o := -I$(src)
 -
  obj-$(CONFIG_DRM)     += drm.o
  obj-$(CONFIG_DRM_MIPI_DSI) += drm_mipi_dsi.o
  obj-$(CONFIG_DRM_ARM) += arm/
  obj-$(CONFIG_DRM_TTM) += ttm/
  obj-$(CONFIG_DRM_TDFX)        += tdfx/
  obj-$(CONFIG_DRM_R128)        += r128/
+ obj-y                 += amd/lib/
  obj-$(CONFIG_HSA_AMD) += amd/amdkfd/
  obj-$(CONFIG_DRM_RADEON)+= radeon/
  obj-$(CONFIG_DRM_AMDGPU)+= amd/amdgpu/
@@@ -98,4 -101,3 +99,4 @@@ obj-$(CONFIG_DRM_ZTE)        += zte
  obj-$(CONFIG_DRM_MXSFB)       += mxsfb/
  obj-$(CONFIG_DRM_TINYDRM) += tinydrm/
  obj-$(CONFIG_DRM_PL111) += pl111/
 +obj-$(CONFIG_DRM_TVE200) += tve200/
@@@ -50,8 -50,10 +50,10 @@@ struct amdgpu_mn 
        struct hlist_node       node;
  
        /* objects protected by lock */
-       struct mutex            lock;
+       struct rw_semaphore     lock;
 -      struct rb_root          objects;
 +      struct rb_root_cached   objects;
+       struct mutex            read_lock;
+       atomic_t                recursion;
  };
  
  struct amdgpu_mn_node {
@@@ -74,17 -76,17 +76,17 @@@ static void amdgpu_mn_destroy(struct wo
        struct amdgpu_bo *bo, *next_bo;
  
        mutex_lock(&adev->mn_lock);
-       mutex_lock(&rmn->lock);
+       down_write(&rmn->lock);
        hash_del(&rmn->node);
 -      rbtree_postorder_for_each_entry_safe(node, next_node, &rmn->objects,
 -                                           it.rb) {
 +      rbtree_postorder_for_each_entry_safe(node, next_node,
 +                                           &rmn->objects.rb_root, it.rb) {
                list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
                        bo->mn = NULL;
                        list_del_init(&bo->mn_list);
                }
                kfree(node);
        }
-       mutex_unlock(&rmn->lock);
+       up_write(&rmn->lock);
        mutex_unlock(&adev->mn_lock);
        mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
        kfree(rmn);
@@@ -106,6 -108,53 +108,53 @@@ static void amdgpu_mn_release(struct mm
        schedule_work(&rmn->work);
  }
  
+ /**
+  * amdgpu_mn_lock - take the write side lock for this mn
+  */
+ void amdgpu_mn_lock(struct amdgpu_mn *mn)
+ {
+       if (mn)
+               down_write(&mn->lock);
+ }
+ /**
+  * amdgpu_mn_unlock - drop the write side lock for this mn
+  */
+ void amdgpu_mn_unlock(struct amdgpu_mn *mn)
+ {
+       if (mn)
+               up_write(&mn->lock);
+ }
+ /**
+  * amdgpu_mn_read_lock - take the rmn read lock
+  *
+  * @rmn: our notifier
+  *
+  * Take the rmn read side lock.
+  */
+ static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
+ {
+       mutex_lock(&rmn->read_lock);
+       if (atomic_inc_return(&rmn->recursion) == 1)
+               down_read_non_owner(&rmn->lock);
+       mutex_unlock(&rmn->read_lock);
+ }
+ /**
+  * amdgpu_mn_read_unlock - drop the rmn read lock
+  *
+  * @rmn: our notifier
+  *
+  * Drop the rmn read side lock.
+  */
+ static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
+ {
+       if (atomic_dec_return(&rmn->recursion) == 0)
+               up_read_non_owner(&rmn->lock);
+ }
  /**
   * amdgpu_mn_invalidate_node - unmap all BOs of a node
   *
@@@ -126,27 -175,46 +175,16 @@@ static void amdgpu_mn_invalidate_node(s
                if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
                        continue;
  
-               r = amdgpu_bo_reserve(bo, true);
-               if (r) {
-                       DRM_ERROR("(%ld) failed to reserve user bo\n", r);
-                       continue;
-               }
                r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
                        true, false, MAX_SCHEDULE_TIMEOUT);
                if (r <= 0)
                        DRM_ERROR("(%ld) failed to wait for user bo\n", r);
  
-               amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
-               r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
-               if (r)
-                       DRM_ERROR("(%ld) failed to validate user bo\n", r);
-               amdgpu_bo_unreserve(bo);
+               amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
        }
  }
  
  /**
 - * amdgpu_mn_invalidate_page - callback to notify about mm change
 - *
 - * @mn: our notifier
 - * @mn: the mm this callback is about
 - * @address: address of invalidate page
 - *
 - * Invalidation of a single page. Blocks for all BOs mapping it
 - * and unmap them by move them into system domain again.
 - */
 -static void amdgpu_mn_invalidate_page(struct mmu_notifier *mn,
 -                                    struct mm_struct *mm,
 -                                    unsigned long address)
 -{
 -      struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
 -      struct interval_tree_node *it;
 -
 -      amdgpu_mn_read_lock(rmn);
 -
 -      it = interval_tree_iter_first(&rmn->objects, address, address);
 -      if (it) {
 -              struct amdgpu_mn_node *node;
 -
 -              node = container_of(it, struct amdgpu_mn_node, it);
 -              amdgpu_mn_invalidate_node(node, address, address);
 -      }
 -
 -      amdgpu_mn_read_unlock(rmn);
 -}
 -
 -/**
   * amdgpu_mn_invalidate_range_start - callback to notify about mm change
   *
   * @mn: our notifier
@@@ -168,7 -236,7 +206,7 @@@ static void amdgpu_mn_invalidate_range_
        /* notification is exclusive, but interval is inclusive */
        end -= 1;
  
-       mutex_lock(&rmn->lock);
+       amdgpu_mn_read_lock(rmn);
  
        it = interval_tree_iter_first(&rmn->objects, start, end);
        while (it) {
  
                amdgpu_mn_invalidate_node(node, start, end);
        }
+ }
  
-       mutex_unlock(&rmn->lock);
+ /**
+  * amdgpu_mn_invalidate_range_end - callback to notify about mm change
+  *
+  * @mn: our notifier
+  * @mn: the mm this callback is about
+  * @start: start of updated range
+  * @end: end of updated range
+  *
+  * Release the lock again to allow new command submissions.
+  */
+ static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
+                                          struct mm_struct *mm,
+                                          unsigned long start,
+                                          unsigned long end)
+ {
+       struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
+       amdgpu_mn_read_unlock(rmn);
  }
  
  static const struct mmu_notifier_ops amdgpu_mn_ops = {
        .release = amdgpu_mn_release,
 -      .invalidate_page = amdgpu_mn_invalidate_page,
        .invalidate_range_start = amdgpu_mn_invalidate_range_start,
+       .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  };
  
  /**
   *
   * Creates a notifier context for current->mm.
   */
- static struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
+ struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev)
  {
        struct mm_struct *mm = current->mm;
        struct amdgpu_mn *rmn;
        rmn->adev = adev;
        rmn->mm = mm;
        rmn->mn.ops = &amdgpu_mn_ops;
-       mutex_init(&rmn->lock);
+       init_rwsem(&rmn->lock);
 -      rmn->objects = RB_ROOT;
 +      rmn->objects = RB_ROOT_CACHED;
+       mutex_init(&rmn->read_lock);
+       atomic_set(&rmn->recursion, 0);
  
        r = __mmu_notifier_register(&rmn->mn, mm);
        if (r)
@@@ -267,7 -357,7 +326,7 @@@ int amdgpu_mn_register(struct amdgpu_b
  
        INIT_LIST_HEAD(&bos);
  
-       mutex_lock(&rmn->lock);
+       down_write(&rmn->lock);
  
        while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
                kfree(node);
        if (!node) {
                node = kmalloc(sizeof(struct amdgpu_mn_node), GFP_KERNEL);
                if (!node) {
-                       mutex_unlock(&rmn->lock);
+                       up_write(&rmn->lock);
                        return -ENOMEM;
                }
        }
  
        interval_tree_insert(&node->it, &rmn->objects);
  
-       mutex_unlock(&rmn->lock);
+       up_write(&rmn->lock);
  
        return 0;
  }
@@@ -322,7 -412,7 +381,7 @@@ void amdgpu_mn_unregister(struct amdgpu
                return;
        }
  
-       mutex_lock(&rmn->lock);
+       down_write(&rmn->lock);
  
        /* save the next list entry for later */
        head = bo->mn_list.next;
                kfree(node);
        }
  
-       mutex_unlock(&rmn->lock);
+       up_write(&rmn->lock);
        mutex_unlock(&adev->mn_lock);
  }
@@@ -42,6 -42,7 +42,7 @@@
  #include <linux/swap.h>
  #include <linux/pagemap.h>
  #include <linux/debugfs.h>
+ #include <linux/iommu.h>
  #include "amdgpu.h"
  #include "amdgpu_trace.h"
  #include "bif/bif_4_1_d.h"
@@@ -608,6 -609,7 +609,7 @@@ struct amdgpu_ttm_tt 
        spinlock_t              guptasklock;
        struct list_head        guptasks;
        atomic_t                mmu_invalidations;
+       uint32_t                last_set_pages;
        struct list_head        list;
  };
  
@@@ -621,6 -623,8 +623,8 @@@ int amdgpu_ttm_tt_get_user_pages(struc
        if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
                flags |= FOLL_WRITE;
  
+       down_read(&current->mm->mmap_sem);
        if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
                /* check that we only use anonymous memory
                   to prevent problems with writeback */
                struct vm_area_struct *vma;
  
                vma = find_vma(gtt->usermm, gtt->userptr);
-               if (!vma || vma->vm_file || vma->vm_end < end)
+               if (!vma || vma->vm_file || vma->vm_end < end) {
+                       up_read(&current->mm->mmap_sem);
                        return -EPERM;
+               }
        }
  
        do {
  
        } while (pinned < ttm->num_pages);
  
+       up_read(&current->mm->mmap_sem);
        return 0;
  
  release_pages:
        release_pages(pages, pinned, 0);
+       up_read(&current->mm->mmap_sem);
        return r;
  }
  
static void amdgpu_trace_dma_map(struct ttm_tt *ttm)
void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
  {
-       struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
        unsigned i;
  
-       if (unlikely(trace_amdgpu_ttm_tt_populate_enabled())) {
-               for (i = 0; i < ttm->num_pages; i++) {
-                       trace_amdgpu_ttm_tt_populate(
-                               adev,
-                               gtt->ttm.dma_address[i],
-                               page_to_phys(ttm->pages[i]));
-               }
+       gtt->last_set_pages = atomic_read(&gtt->mmu_invalidations);
+       for (i = 0; i < ttm->num_pages; ++i) {
+               if (ttm->pages[i])
+                       put_page(ttm->pages[i]);
+               ttm->pages[i] = pages ? pages[i] : NULL;
        }
  }
  
static void amdgpu_trace_dma_unmap(struct ttm_tt *ttm)
void amdgpu_ttm_tt_mark_user_pages(struct ttm_tt *ttm)
  {
-       struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
        unsigned i;
  
-       if (unlikely(trace_amdgpu_ttm_tt_unpopulate_enabled())) {
-               for (i = 0; i < ttm->num_pages; i++) {
-                       trace_amdgpu_ttm_tt_unpopulate(
-                               adev,
-                               gtt->ttm.dma_address[i],
-                               page_to_phys(ttm->pages[i]));
-               }
+       for (i = 0; i < ttm->num_pages; ++i) {
+               struct page *page = ttm->pages[i];
+               if (!page)
+                       continue;
+               if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
+                       set_page_dirty(page);
+               mark_page_accessed(page);
        }
  }
  
@@@ -721,8 -729,6 +729,6 @@@ static int amdgpu_ttm_tt_pin_userptr(st
        drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
                                         gtt->ttm.dma_address, ttm->num_pages);
  
-       amdgpu_trace_dma_map(ttm);
        return 0;
  
  release_sg:
@@@ -734,7 -740,6 +740,6 @@@ static void amdgpu_ttm_tt_unpin_userptr
  {
        struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
-       struct sg_page_iter sg_iter;
  
        int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
        enum dma_data_direction direction = write ?
        /* free the sg table and pages again */
        dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
  
-       for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
-               struct page *page = sg_page_iter_page(&sg_iter);
-               if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
-                       set_page_dirty(page);
-               mark_page_accessed(page);
-               put_page(page);
-       }
-       amdgpu_trace_dma_unmap(ttm);
+       amdgpu_ttm_tt_mark_user_pages(ttm);
  
        sg_free_table(ttm->sg);
  }
@@@ -818,7 -814,7 +814,6 @@@ int amdgpu_ttm_bind(struct ttm_buffer_o
        struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
        struct ttm_tt *ttm = bo->ttm;
        struct ttm_mem_reg tmp;
--
        struct ttm_placement placement;
        struct ttm_place placements;
        int r;
        placement.busy_placement = &placements;
        placements.fpfn = 0;
        placements.lpfn = adev->mc.gart_size >> PAGE_SHIFT;
-       placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
+       placements.flags = bo->mem.placement | TTM_PL_FLAG_TT;
  
        r = ttm_bo_mem_space(bo, &placement, &tmp, true, false);
        if (unlikely(r))
@@@ -941,8 -937,6 +936,6 @@@ static int amdgpu_ttm_tt_populate(struc
  {
        struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
-       unsigned i;
-       int r;
        bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  
        if (ttm->state != tt_unpopulated)
                drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
                                                 gtt->ttm.dma_address, ttm->num_pages);
                ttm->state = tt_unbound;
-               r = 0;
-               goto trace_mappings;
+               return 0;
        }
  
  #ifdef CONFIG_SWIOTLB
        if (swiotlb_nr_tbl()) {
-               r = ttm_dma_populate(&gtt->ttm, adev->dev);
-               goto trace_mappings;
+               return ttm_dma_populate(&gtt->ttm, adev->dev);
        }
  #endif
  
-       r = ttm_pool_populate(ttm);
-       if (r) {
-               return r;
-       }
-       for (i = 0; i < ttm->num_pages; i++) {
-               gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
-                                                      0, PAGE_SIZE,
-                                                      PCI_DMA_BIDIRECTIONAL);
-               if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
-                       while (i--) {
-                               pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
-                                              PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
-                               gtt->ttm.dma_address[i] = 0;
-                       }
-                       ttm_pool_unpopulate(ttm);
-                       return -EFAULT;
-               }
-       }
-       r = 0;
- trace_mappings:
-       if (likely(!r))
-               amdgpu_trace_dma_map(ttm);
-       return r;
+       return ttm_populate_and_map_pages(adev->dev, &gtt->ttm);
  }
  
  static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
  {
        struct amdgpu_device *adev;
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
-       unsigned i;
        bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
  
        if (gtt && gtt->userptr) {
+               amdgpu_ttm_tt_set_user_pages(ttm, NULL);
                kfree(ttm->sg);
                ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
                return;
  
        adev = amdgpu_ttm_adev(ttm->bdev);
  
-       amdgpu_trace_dma_unmap(ttm);
  #ifdef CONFIG_SWIOTLB
        if (swiotlb_nr_tbl()) {
                ttm_dma_unpopulate(&gtt->ttm, adev->dev);
        }
  #endif
  
-       for (i = 0; i < ttm->num_pages; i++) {
-               if (gtt->ttm.dma_address[i]) {
-                       pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
-                                      PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
-               }
-       }
-       ttm_pool_unpopulate(ttm);
+       ttm_unmap_and_unpopulate_pages(adev->dev, &gtt->ttm);
  }
  
  int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
        spin_lock_init(&gtt->guptasklock);
        INIT_LIST_HEAD(&gtt->guptasks);
        atomic_set(&gtt->mmu_invalidations, 0);
+       gtt->last_set_pages = 0;
  
        return 0;
  }
@@@ -1103,6 -1063,16 +1062,16 @@@ bool amdgpu_ttm_tt_userptr_invalidated(
        return prev_invalidated != *last_invalidated;
  }
  
+ bool amdgpu_ttm_tt_userptr_needs_pages(struct ttm_tt *ttm)
+ {
+       struct amdgpu_ttm_tt *gtt = (void *)ttm;
+       if (gtt == NULL || !gtt->userptr)
+               return false;
+       return atomic_read(&gtt->mmu_invalidations) != gtt->last_set_pages;
+ }
  bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
  {
        struct amdgpu_ttm_tt *gtt = (void *)ttm;
@@@ -1202,14 -1172,14 +1171,14 @@@ static int amdgpu_ttm_access_memory(str
                }
  
                spin_lock_irqsave(&adev->mmio_idx_lock, flags);
-               WREG32(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
-               WREG32(mmMM_INDEX_HI, aligned_pos >> 31);
+               WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
+               WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
                if (!write || mask != 0xffffffff)
-                       value = RREG32(mmMM_DATA);
+                       value = RREG32_NO_KIQ(mmMM_DATA);
                if (write) {
                        value &= ~mask;
                        value |= (*(uint32_t *)buf << shift) & mask;
-                       WREG32(mmMM_DATA, value);
+                       WREG32_NO_KIQ(mmMM_DATA, value);
                }
                spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
                if (!write) {
@@@ -1557,8 -1527,8 +1526,8 @@@ int amdgpu_fill_buffer(struct amdgpu_b
                       struct dma_fence **fence)
  {
        struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
-       /* max_bytes applies to SDMA_OP_PTEPDE as well as SDMA_OP_CONST_FILL*/
-       uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
+       uint32_t max_bytes = 8 *
+                       adev->vm_manager.vm_pte_funcs->set_max_nums_pte_pde;
        struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  
        struct drm_mm_node *mm_node;
                ++mm_node;
        }
  
-       /* 10 double words for each SDMA_OP_PTEPDE cmd */
-       num_dw = num_loops * 10;
+       /* num of dwords for each SDMA_OP_PTEPDE cmd */
+       num_dw = num_loops * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
  
        /* for IB padding */
        num_dw += 64;
@@@ -1697,9 -1667,9 +1666,9 @@@ static ssize_t amdgpu_ttm_vram_read(str
                        return result;
  
                spin_lock_irqsave(&adev->mmio_idx_lock, flags);
-               WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
-               WREG32(mmMM_INDEX_HI, *pos >> 31);
-               value = RREG32(mmMM_DATA);
+               WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
+               WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
+               value = RREG32_NO_KIQ(mmMM_DATA);
                spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
  
                r = put_user(value, (uint32_t *)buf);
        return result;
  }
  
+ static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
+                                   size_t size, loff_t *pos)
+ {
+       struct amdgpu_device *adev = file_inode(f)->i_private;
+       ssize_t result = 0;
+       int r;
+       if (size & 0x3 || *pos & 0x3)
+               return -EINVAL;
+       if (*pos >= adev->mc.mc_vram_size)
+               return -ENXIO;
+       while (size) {
+               unsigned long flags;
+               uint32_t value;
+               if (*pos >= adev->mc.mc_vram_size)
+                       return result;
+               r = get_user(value, (uint32_t *)buf);
+               if (r)
+                       return r;
+               spin_lock_irqsave(&adev->mmio_idx_lock, flags);
+               WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
+               WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
+               WREG32_NO_KIQ(mmMM_DATA, value);
+               spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
+               result += 4;
+               buf += 4;
+               *pos += 4;
+               size -= 4;
+       }
+       return result;
+ }
  static const struct file_operations amdgpu_ttm_vram_fops = {
        .owner = THIS_MODULE,
        .read = amdgpu_ttm_vram_read,
-       .llseek = default_llseek
+       .write = amdgpu_ttm_vram_write,
+       .llseek = default_llseek,
  };
  
  #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
@@@ -1770,6 -1780,53 +1779,53 @@@ static const struct file_operations amd
  
  #endif
  
+ static ssize_t amdgpu_iova_to_phys_read(struct file *f, char __user *buf,
+                                  size_t size, loff_t *pos)
+ {
+       struct amdgpu_device *adev = file_inode(f)->i_private;
+       int r;
+       uint64_t phys;
+       struct iommu_domain *dom;
+       // always return 8 bytes
+       if (size != 8)
+               return -EINVAL;
+       // only accept page addresses
+       if (*pos & 0xFFF)
+               return -EINVAL;
+       dom = iommu_get_domain_for_dev(adev->dev);
+       if (dom)
+               phys = iommu_iova_to_phys(dom, *pos);
+       else
+               phys = *pos;
+       r = copy_to_user(buf, &phys, 8);
+       if (r)
+               return -EFAULT;
+       return 8;
+ }
+ static const struct file_operations amdgpu_ttm_iova_fops = {
+       .owner = THIS_MODULE,
+       .read = amdgpu_iova_to_phys_read,
+       .llseek = default_llseek
+ };
+ static const struct {
+       char *name;
+       const struct file_operations *fops;
+       int domain;
+ } ttm_debugfs_entries[] = {
+       { "amdgpu_vram", &amdgpu_ttm_vram_fops, TTM_PL_VRAM },
+ #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
+       { "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
+ #endif
+       { "amdgpu_iova", &amdgpu_ttm_iova_fops, TTM_PL_SYSTEM },
+ };
  #endif
  
  static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
        struct drm_minor *minor = adev->ddev->primary;
        struct dentry *ent, *root = minor->debugfs_root;
  
-       ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
-                                 adev, &amdgpu_ttm_vram_fops);
-       if (IS_ERR(ent))
-               return PTR_ERR(ent);
-       i_size_write(ent->d_inode, adev->mc.mc_vram_size);
-       adev->mman.vram = ent;
- #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
-       ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
-                                 adev, &amdgpu_ttm_gtt_fops);
-       if (IS_ERR(ent))
-               return PTR_ERR(ent);
-       i_size_write(ent->d_inode, adev->mc.gart_size);
-       adev->mman.gtt = ent;
+       for (count = 0; count < ARRAY_SIZE(ttm_debugfs_entries); count++) {
+               ent = debugfs_create_file(
+                               ttm_debugfs_entries[count].name,
+                               S_IFREG | S_IRUGO, root,
+                               adev,
+                               ttm_debugfs_entries[count].fops);
+               if (IS_ERR(ent))
+                       return PTR_ERR(ent);
+               if (ttm_debugfs_entries[count].domain == TTM_PL_VRAM)
+                       i_size_write(ent->d_inode, adev->mc.mc_vram_size);
+               else if (ttm_debugfs_entries[count].domain == TTM_PL_TT)
+                       i_size_write(ent->d_inode, adev->mc.gart_size);
+               adev->mman.debugfs_entries[count] = ent;
+       }
  
- #endif
        count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
  
  #ifdef CONFIG_SWIOTLB
  
        return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
  #else
        return 0;
  #endif
  }
  static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
  {
  #if defined(CONFIG_DEBUG_FS)
+       unsigned i;
  
-       debugfs_remove(adev->mman.vram);
-       adev->mman.vram = NULL;
- #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
-       debugfs_remove(adev->mman.gtt);
-       adev->mman.gtt = NULL;
- #endif
+       for (i = 0; i < ARRAY_SIZE(ttm_debugfs_entries); i++)
+               debugfs_remove(adev->mman.debugfs_entries[i]);
  #endif
  }
   */
  #include <linux/dma-fence-array.h>
  #include <linux/interval_tree_generic.h>
+ #include <linux/idr.h>
  #include <drm/drmP.h>
  #include <drm/amdgpu_drm.h>
  #include "amdgpu.h"
  #include "amdgpu_trace.h"
  
  /*
+  * PASID manager
+  *
+  * PASIDs are global address space identifiers that can be shared
+  * between the GPU, an IOMMU and the driver. VMs on different devices
+  * may use the same PASID if they share the same address
+  * space. Therefore PASIDs are allocated using a global IDA. VMs are
+  * looked up from the PASID per amdgpu_device.
+  */
+ static DEFINE_IDA(amdgpu_vm_pasid_ida);
+ /**
+  * amdgpu_vm_alloc_pasid - Allocate a PASID
+  * @bits: Maximum width of the PASID in bits, must be at least 1
+  *
+  * Allocates a PASID of the given width while keeping smaller PASIDs
+  * available if possible.
+  *
+  * Returns a positive integer on success. Returns %-EINVAL if bits==0.
+  * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
+  * memory allocation failure.
+  */
+ int amdgpu_vm_alloc_pasid(unsigned int bits)
+ {
+       int pasid = -EINVAL;
+       for (bits = min(bits, 31U); bits > 0; bits--) {
+               pasid = ida_simple_get(&amdgpu_vm_pasid_ida,
+                                      1U << (bits - 1), 1U << bits,
+                                      GFP_KERNEL);
+               if (pasid != -ENOSPC)
+                       break;
+       }
+       return pasid;
+ }
+ /**
+  * amdgpu_vm_free_pasid - Free a PASID
+  * @pasid: PASID to free
+  */
+ void amdgpu_vm_free_pasid(unsigned int pasid)
+ {
+       ida_simple_remove(&amdgpu_vm_pasid_ida, pasid);
+ }
+ /*
   * GPUVM
   * GPUVM is similar to the legacy gart on older asics, however
   * rather than there being a single global gart table
@@@ -140,7 -187,7 +187,7 @@@ void amdgpu_vm_get_pd_bo(struct amdgpu_
                         struct list_head *validated,
                         struct amdgpu_bo_list_entry *entry)
  {
-       entry->robj = vm->root.bo;
+       entry->robj = vm->root.base.bo;
        entry->priority = 0;
        entry->tv.bo = &entry->robj->tbo;
        entry->tv.shared = true;
  }
  
  /**
-  * amdgpu_vm_validate_layer - validate a single page table level
+  * amdgpu_vm_validate_pt_bos - validate the page table BOs
   *
-  * @parent: parent page table level
+  * @adev: amdgpu device pointer
+  * @vm: vm providing the BOs
   * @validate: callback to do the validation
   * @param: parameter for the validation callback
   *
   * Validate the page table BOs on command submission if neccessary.
   */
- static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent,
-                                   int (*validate)(void *, struct amdgpu_bo *),
-                                   void *param, bool use_cpu_for_update,
-                                   struct ttm_bo_global *glob)
+ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
+                             int (*validate)(void *p, struct amdgpu_bo *bo),
+                             void *param)
  {
-       unsigned i;
+       struct ttm_bo_global *glob = adev->mman.bdev.glob;
        int r;
  
-       if (use_cpu_for_update) {
-               r = amdgpu_bo_kmap(parent->bo, NULL);
-               if (r)
-                       return r;
-       }
-       if (!parent->entries)
-               return 0;
+       spin_lock(&vm->status_lock);
+       while (!list_empty(&vm->evicted)) {
+               struct amdgpu_vm_bo_base *bo_base;
+               struct amdgpu_bo *bo;
  
-       for (i = 0; i <= parent->last_entry_used; ++i) {
-               struct amdgpu_vm_pt *entry = &parent->entries[i];
+               bo_base = list_first_entry(&vm->evicted,
+                                          struct amdgpu_vm_bo_base,
+                                          vm_status);
+               spin_unlock(&vm->status_lock);
  
-               if (!entry->bo)
-                       continue;
+               bo = bo_base->bo;
+               BUG_ON(!bo);
+               if (bo->parent) {
+                       r = validate(param, bo);
+                       if (r)
+                               return r;
  
-               r = validate(param, entry->bo);
-               if (r)
-                       return r;
+                       spin_lock(&glob->lru_lock);
+                       ttm_bo_move_to_lru_tail(&bo->tbo);
+                       if (bo->shadow)
+                               ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
+                       spin_unlock(&glob->lru_lock);
+               }
  
-               spin_lock(&glob->lru_lock);
-               ttm_bo_move_to_lru_tail(&entry->bo->tbo);
-               if (entry->bo->shadow)
-                       ttm_bo_move_to_lru_tail(&entry->bo->shadow->tbo);
-               spin_unlock(&glob->lru_lock);
+               if (bo->tbo.type == ttm_bo_type_kernel &&
+                   vm->use_cpu_for_update) {
+                       r = amdgpu_bo_kmap(bo, NULL);
+                       if (r)
+                               return r;
+               }
  
-               /*
-                * Recurse into the sub directory. This is harmless because we
-                * have only a maximum of 5 layers.
-                */
-               r = amdgpu_vm_validate_level(entry, validate, param,
-                                            use_cpu_for_update, glob);
-               if (r)
-                       return r;
+               spin_lock(&vm->status_lock);
+               if (bo->tbo.type != ttm_bo_type_kernel)
+                       list_move(&bo_base->vm_status, &vm->moved);
+               else
+                       list_move(&bo_base->vm_status, &vm->relocated);
        }
+       spin_unlock(&vm->status_lock);
  
-       return r;
+       return 0;
  }
  
  /**
-  * amdgpu_vm_validate_pt_bos - validate the page table BOs
+  * amdgpu_vm_ready - check VM is ready for updates
   *
-  * @adev: amdgpu device pointer
-  * @vm: vm providing the BOs
-  * @validate: callback to do the validation
-  * @param: parameter for the validation callback
+  * @vm: VM to check
   *
-  * Validate the page table BOs on command submission if neccessary.
+  * Check if all VM PDs/PTs are ready for updates
   */
- int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
-                             int (*validate)(void *p, struct amdgpu_bo *bo),
-                             void *param)
+ bool amdgpu_vm_ready(struct amdgpu_vm *vm)
  {
-       uint64_t num_evictions;
+       bool ready;
  
-       /* We only need to validate the page tables
-        * if they aren't already valid.
-        */
-       num_evictions = atomic64_read(&adev->num_evictions);
-       if (num_evictions == vm->last_eviction_counter)
-               return 0;
+       spin_lock(&vm->status_lock);
+       ready = list_empty(&vm->evicted);
+       spin_unlock(&vm->status_lock);
  
-       return amdgpu_vm_validate_level(&vm->root, validate, param,
-                                       vm->use_cpu_for_update,
-                                       adev->mman.bdev.glob);
+       return ready;
  }
  
  /**
@@@ -294,11 -335,11 +335,11 @@@ static int amdgpu_vm_alloc_levels(struc
  
        /* walk over the address space and allocate the page tables */
        for (pt_idx = from; pt_idx <= to; ++pt_idx) {
-               struct reservation_object *resv = vm->root.bo->tbo.resv;
+               struct reservation_object *resv = vm->root.base.bo->tbo.resv;
                struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
                struct amdgpu_bo *pt;
  
-               if (!entry->bo) {
+               if (!entry->base.bo) {
                        r = amdgpu_bo_create(adev,
                                             amdgpu_vm_bo_size(adev, level),
                                             AMDGPU_GPU_PAGE_SIZE, true,
                        /* Keep a reference to the root directory to avoid
                        * freeing them up in the wrong order.
                        */
-                       pt->parent = amdgpu_bo_ref(vm->root.bo);
-                       entry->bo = pt;
+                       pt->parent = amdgpu_bo_ref(parent->base.bo);
+                       entry->base.vm = vm;
+                       entry->base.bo = pt;
+                       list_add_tail(&entry->base.bo_list, &pt->va);
+                       spin_lock(&vm->status_lock);
+                       list_add(&entry->base.vm_status, &vm->relocated);
+                       spin_unlock(&vm->status_lock);
                        entry->addr = 0;
                }
  
@@@ -988,7 -1034,7 +1034,7 @@@ static int amdgpu_vm_wait_pd(struct amd
        int r;
  
        amdgpu_sync_create(&sync);
-       amdgpu_sync_resv(adev, &sync, vm->root.bo->tbo.resv, owner);
+       amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner);
        r = amdgpu_sync_wait(&sync, true);
        amdgpu_sync_free(&sync);
  
   */
  static int amdgpu_vm_update_level(struct amdgpu_device *adev,
                                  struct amdgpu_vm *vm,
-                                 struct amdgpu_vm_pt *parent,
-                                 unsigned level)
+                                 struct amdgpu_vm_pt *parent)
  {
        struct amdgpu_bo *shadow;
        struct amdgpu_ring *ring = NULL;
        uint64_t pd_addr, shadow_addr = 0;
-       uint32_t incr = amdgpu_vm_bo_size(adev, level + 1);
        uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
        unsigned count = 0, pt_idx, ndw = 0;
        struct amdgpu_job *job;
        struct amdgpu_pte_update_params params;
        struct dma_fence *fence = NULL;
+       uint32_t incr;
  
        int r;
  
  
        memset(&params, 0, sizeof(params));
        params.adev = adev;
-       shadow = parent->bo->shadow;
+       shadow = parent->base.bo->shadow;
  
        if (vm->use_cpu_for_update) {
-               pd_addr = (unsigned long)amdgpu_bo_kptr(parent->bo);
+               pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
                r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
                if (unlikely(r))
                        return r;
                /* assume the worst case */
                ndw += parent->last_entry_used * 6;
  
-               pd_addr = amdgpu_bo_gpu_offset(parent->bo);
+               pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
  
                if (shadow) {
                        shadow_addr = amdgpu_bo_gpu_offset(shadow);
  
        /* walk over the address space and update the directory */
        for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
-               struct amdgpu_bo *bo = parent->entries[pt_idx].bo;
+               struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
+               struct amdgpu_bo *bo = entry->base.bo;
                uint64_t pde, pt;
  
                if (bo == NULL)
                        continue;
  
+               spin_lock(&vm->status_lock);
+               list_del_init(&entry->base.vm_status);
+               spin_unlock(&vm->status_lock);
                pt = amdgpu_bo_gpu_offset(bo);
                pt = amdgpu_gart_get_vm_pde(adev, pt);
                /* Don't update huge pages here */
                parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
  
                pde = pd_addr + pt_idx * 8;
+               incr = amdgpu_bo_size(bo);
                if (((last_pde + 8 * count) != pde) ||
                    ((last_pt + incr * count) != pt) ||
                    (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
        }
  
        if (count) {
-               if (vm->root.bo->shadow)
+               if (vm->root.base.bo->shadow)
                        params.func(&params, last_shadow, last_pt,
                                    count, incr, AMDGPU_PTE_VALID);
  
                        amdgpu_job_free(job);
                } else {
                        amdgpu_ring_pad_ib(ring, params.ib);
-                       amdgpu_sync_resv(adev, &job->sync, parent->bo->tbo.resv,
+                       amdgpu_sync_resv(adev, &job->sync,
+                                        parent->base.bo->tbo.resv,
                                         AMDGPU_FENCE_OWNER_VM);
                        if (shadow)
                                amdgpu_sync_resv(adev, &job->sync,
                        if (r)
                                goto error_free;
  
-                       amdgpu_bo_fence(parent->bo, fence, true);
-                       dma_fence_put(vm->last_dir_update);
-                       vm->last_dir_update = dma_fence_get(fence);
-                       dma_fence_put(fence);
+                       amdgpu_bo_fence(parent->base.bo, fence, true);
+                       dma_fence_put(vm->last_update);
+                       vm->last_update = fence;
                }
        }
-       /*
-        * Recurse into the subdirectories. This recursion is harmless because
-        * we only have a maximum of 5 layers.
-        */
-       for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
-               struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
-               if (!entry->bo)
-                       continue;
-               r = amdgpu_vm_update_level(adev, vm, entry, level + 1);
-               if (r)
-                       return r;
-       }
  
        return 0;
  
@@@ -1170,7 -1207,8 +1207,8 @@@ error_free
   *
   * Mark all PD level as invalid after an error.
   */
- static void amdgpu_vm_invalidate_level(struct amdgpu_vm_pt *parent)
+ static void amdgpu_vm_invalidate_level(struct amdgpu_vm *vm,
+                                      struct amdgpu_vm_pt *parent)
  {
        unsigned pt_idx;
  
        for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
                struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  
-               if (!entry->bo)
+               if (!entry->base.bo)
                        continue;
  
                entry->addr = ~0ULL;
-               amdgpu_vm_invalidate_level(entry);
+               spin_lock(&vm->status_lock);
+               if (list_empty(&entry->base.vm_status))
+                       list_add(&entry->base.vm_status, &vm->relocated);
+               spin_unlock(&vm->status_lock);
+               amdgpu_vm_invalidate_level(vm, entry);
        }
  }
  
@@@ -1203,9 -1245,38 +1245,38 @@@ int amdgpu_vm_update_directories(struc
  {
        int r;
  
-       r = amdgpu_vm_update_level(adev, vm, &vm->root, 0);
-       if (r)
-               amdgpu_vm_invalidate_level(&vm->root);
+       spin_lock(&vm->status_lock);
+       while (!list_empty(&vm->relocated)) {
+               struct amdgpu_vm_bo_base *bo_base;
+               struct amdgpu_bo *bo;
+               bo_base = list_first_entry(&vm->relocated,
+                                          struct amdgpu_vm_bo_base,
+                                          vm_status);
+               spin_unlock(&vm->status_lock);
+               bo = bo_base->bo->parent;
+               if (bo) {
+                       struct amdgpu_vm_bo_base *parent;
+                       struct amdgpu_vm_pt *pt;
+                       parent = list_first_entry(&bo->va,
+                                                 struct amdgpu_vm_bo_base,
+                                                 bo_list);
+                       pt = container_of(parent, struct amdgpu_vm_pt, base);
+                       r = amdgpu_vm_update_level(adev, vm, pt);
+                       if (r) {
+                               amdgpu_vm_invalidate_level(vm, &vm->root);
+                               return r;
+                       }
+                       spin_lock(&vm->status_lock);
+               } else {
+                       spin_lock(&vm->status_lock);
+                       list_del_init(&bo_base->vm_status);
+               }
+       }
+       spin_unlock(&vm->status_lock);
  
        if (vm->use_cpu_for_update) {
                /* Flush HDP */
@@@ -1236,7 -1307,7 +1307,7 @@@ void amdgpu_vm_get_entry(struct amdgpu_
        *entry = &p->vm->root;
        while ((*entry)->entries) {
                idx = addr >> (p->adev->vm_manager.block_size * level--);
-               idx %= amdgpu_bo_size((*entry)->bo) / 8;
+               idx %= amdgpu_bo_size((*entry)->base.bo) / 8;
                *parent = *entry;
                *entry = &(*entry)->entries[idx];
        }
@@@ -1272,7 -1343,7 +1343,7 @@@ static void amdgpu_vm_handle_huge_pages
            p->src ||
            !(flags & AMDGPU_PTE_VALID)) {
  
-               dst = amdgpu_bo_gpu_offset(entry->bo);
+               dst = amdgpu_bo_gpu_offset(entry->base.bo);
                dst = amdgpu_gart_get_vm_pde(p->adev, dst);
                flags = AMDGPU_PTE_VALID;
        } else {
                tmp = p->pages_addr;
                p->pages_addr = NULL;
  
-               pd_addr = (unsigned long)amdgpu_bo_kptr(parent->bo);
+               pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
                pde = pd_addr + (entry - parent->entries) * 8;
                amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
  
                p->pages_addr = tmp;
        } else {
-               if (parent->bo->shadow) {
-                       pd_addr = amdgpu_bo_gpu_offset(parent->bo->shadow);
+               if (parent->base.bo->shadow) {
+                       pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
                        pde = pd_addr + (entry - parent->entries) * 8;
                        amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
                }
-               pd_addr = amdgpu_bo_gpu_offset(parent->bo);
+               pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
                pde = pd_addr + (entry - parent->entries) * 8;
                amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
        }
@@@ -1360,7 -1431,7 +1431,7 @@@ static int amdgpu_vm_update_ptes(struc
                if (entry->addr & AMDGPU_PDE_PTE)
                        continue;
  
-               pt = entry->bo;
+               pt = entry->base.bo;
                if (use_cpu_update) {
                        pe_start = (unsigned long)amdgpu_bo_kptr(pt);
                } else {
@@@ -1396,8 -1467,6 +1467,6 @@@ static int amdgpu_vm_frag_ptes(struct a
                                uint64_t start, uint64_t end,
                                uint64_t dst, uint64_t flags)
  {
-       int r;
        /**
         * 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
         * Userspace can support this by aligning virtual base address and
         * allocation size to the fragment size.
         */
-       unsigned pages_per_frag = params->adev->vm_manager.fragment_size;
-       uint64_t frag_flags = AMDGPU_PTE_FRAG(pages_per_frag);
-       uint64_t frag_align = 1 << pages_per_frag;
-       uint64_t frag_start = ALIGN(start, frag_align);
-       uint64_t frag_end = end & ~(frag_align - 1);
+       unsigned max_frag = params->adev->vm_manager.fragment_size;
+       int r;
  
        /* system pages are non continuously */
-       if (params->src || !(flags & AMDGPU_PTE_VALID) ||
-           (frag_start >= frag_end))
+       if (params->src || !(flags & AMDGPU_PTE_VALID))
                return amdgpu_vm_update_ptes(params, start, end, dst, flags);
  
-       /* handle the 4K area at the beginning */
-       if (start != frag_start) {
-               r = amdgpu_vm_update_ptes(params, start, frag_start,
-                                         dst, flags);
+       while (start != end) {
+               uint64_t frag_flags, frag_end;
+               unsigned frag;
+               /* 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_flags = AMDGPU_PTE_FRAG(max_frag);
+                       frag_end = end & ~((1ULL << max_frag) - 1);
+               } else {
+                       frag_flags = AMDGPU_PTE_FRAG(frag);
+                       frag_end = start + (1 << frag);
+               }
+               r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
+                                         flags | frag_flags);
                if (r)
                        return r;
-               dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
-       }
  
-       /* handle the area in the middle */
-       r = amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
-                                 flags | frag_flags);
-       if (r)
-               return r;
-       /* handle the 4K area at the end */
-       if (frag_end != end) {
-               dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
-               r = amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
+               dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
+               start = frag_end;
        }
-       return r;
+       return 0;
  }
  
  /**
   *
   * @adev: amdgpu_device pointer
   * @exclusive: fence we need to sync to
-  * @src: address where to copy page table entries from
   * @pages_addr: DMA addresses to use for mapping
   * @vm: requested vm
   * @start: start of mapped range
   */
  static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
                                       struct dma_fence *exclusive,
-                                      uint64_t src,
                                       dma_addr_t *pages_addr,
                                       struct amdgpu_vm *vm,
                                       uint64_t start, uint64_t last,
        memset(&params, 0, sizeof(params));
        params.adev = adev;
        params.vm = vm;
-       params.src = src;
  
        /* sync to everything on unmapping */
        if (!(flags & AMDGPU_PTE_VALID))
        nptes = last - start + 1;
  
        /*
-        * reserve space for one command every (1 << BLOCK_SIZE)
+        * reserve space for two commands every (1 << BLOCK_SIZE)
         *  entries or 2k dwords (whatever is smaller)
+          *
+          * The second command is for the shadow pagetables.
         */
-       ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1;
+       ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
  
        /* padding, etc. */
        ndw = 64;
        /* one PDE write for each huge page */
        ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
  
-       if (src) {
-               /* only copy commands needed */
-               ndw += ncmds * 7;
-               params.func = amdgpu_vm_do_copy_ptes;
-       } else if (pages_addr) {
+       if (pages_addr) {
                /* copy commands needed */
-               ndw += ncmds * 7;
+               ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
  
                /* and also PTEs */
                ndw += nptes * 2;
  
        } else {
                /* set page commands needed */
-               ndw += ncmds * 10;
+               ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
  
-               /* two extra commands for begin/end of fragment */
-               ndw += 2 * 10;
+               /* extra commands for begin/end fragments */
+               ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
+                               * adev->vm_manager.fragment_size;
  
                params.func = amdgpu_vm_do_set_ptes;
        }
  
        params.ib = &job->ibs[0];
  
-       if (!src && pages_addr) {
+       if (pages_addr) {
                uint64_t *pte;
                unsigned i;
  
        if (r)
                goto error_free;
  
-       r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
+       r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
                             owner);
        if (r)
                goto error_free;
  
-       r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
+       r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
        if (r)
                goto error_free;
  
        if (r)
                goto error_free;
  
-       amdgpu_bo_fence(vm->root.bo, f, true);
+       amdgpu_bo_fence(vm->root.base.bo, f, true);
        dma_fence_put(*fence);
        *fence = f;
        return 0;
  
  error_free:
        amdgpu_job_free(job);
-       amdgpu_vm_invalidate_level(&vm->root);
+       amdgpu_vm_invalidate_level(vm, &vm->root);
        return r;
  }
  
@@@ -1636,7 -1698,7 +1698,7 @@@ static int amdgpu_vm_bo_split_mapping(s
                                      struct drm_mm_node *nodes,
                                      struct dma_fence **fence)
  {
-       uint64_t pfn, src = 0, start = mapping->start;
+       uint64_t pfn, start = mapping->start;
        int r;
  
        /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
                addr += pfn << PAGE_SHIFT;
  
                last = min((uint64_t)mapping->last, start + max_entries - 1);
-               r = amdgpu_vm_bo_update_mapping(adev, exclusive,
-                                               src, pages_addr, vm,
+               r = amdgpu_vm_bo_update_mapping(adev, exclusive, pages_addr, vm,
                                                start, last, flags, addr,
                                                fence);
                if (r)
@@@ -1730,7 -1791,7 +1791,7 @@@ int amdgpu_vm_bo_update(struct amdgpu_d
        dma_addr_t *pages_addr = NULL;
        struct ttm_mem_reg *mem;
        struct drm_mm_node *nodes;
-       struct dma_fence *exclusive;
+       struct dma_fence *exclusive, **last_update;
        uint64_t flags;
        int r;
  
        else
                flags = 0x0;
  
-       spin_lock(&vm->status_lock);
-       if (!list_empty(&bo_va->base.vm_status))
+       if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
+               last_update = &vm->last_update;
+       else
+               last_update = &bo_va->last_pt_update;
+       if (!clear && bo_va->base.moved) {
+               bo_va->base.moved = false;
                list_splice_init(&bo_va->valids, &bo_va->invalids);
-       spin_unlock(&vm->status_lock);
+       } else if (bo_va->cleared != clear) {
+               list_splice_init(&bo_va->valids, &bo_va->invalids);
+       }
  
        list_for_each_entry(mapping, &bo_va->invalids, list) {
                r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
                                               mapping, flags, nodes,
-                                              &bo_va->last_pt_update);
+                                              last_update);
                if (r)
                        return r;
        }
  
-       if (trace_amdgpu_vm_bo_mapping_enabled()) {
-               list_for_each_entry(mapping, &bo_va->valids, list)
-                       trace_amdgpu_vm_bo_mapping(mapping);
-               list_for_each_entry(mapping, &bo_va->invalids, list)
-                       trace_amdgpu_vm_bo_mapping(mapping);
+       if (vm->use_cpu_for_update) {
+               /* Flush HDP */
+               mb();
+               amdgpu_gart_flush_gpu_tlb(adev, 0);
        }
  
        spin_lock(&vm->status_lock);
-       list_splice_init(&bo_va->invalids, &bo_va->valids);
        list_del_init(&bo_va->base.vm_status);
-       if (clear)
-               list_add(&bo_va->base.vm_status, &vm->cleared);
        spin_unlock(&vm->status_lock);
  
-       if (vm->use_cpu_for_update) {
-               /* Flush HDP */
-               mb();
-               amdgpu_gart_flush_gpu_tlb(adev, 0);
+       list_splice_init(&bo_va->invalids, &bo_va->valids);
+       bo_va->cleared = clear;
+       if (trace_amdgpu_vm_bo_mapping_enabled()) {
+               list_for_each_entry(mapping, &bo_va->valids, list)
+                       trace_amdgpu_vm_bo_mapping(mapping);
        }
  
        return 0;
@@@ -1895,7 -1961,7 +1961,7 @@@ static void amdgpu_vm_free_mapping(stru
   */
  static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  {
-       struct reservation_object *resv = vm->root.bo->tbo.resv;
+       struct reservation_object *resv = vm->root.base.bo->tbo.resv;
        struct dma_fence *excl, **shared;
        unsigned i, shared_count;
        int r;
@@@ -1953,7 -2019,7 +2019,7 @@@ int amdgpu_vm_clear_freed(struct amdgpu
                if (vm->pte_support_ats)
                        init_pte_value = AMDGPU_PTE_SYSTEM;
  
-               r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, NULL, vm,
+               r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
                                                mapping->start, mapping->last,
                                                init_pte_value, 0, &f);
                amdgpu_vm_free_mapping(adev, vm, mapping, f);
  }
  
  /**
-  * amdgpu_vm_clear_moved - clear moved BOs in the PT
+  * amdgpu_vm_handle_moved - handle moved BOs in the PT
   *
   * @adev: amdgpu_device pointer
   * @vm: requested vm
+  * @sync: sync object to add fences to
   *
-  * Make sure all moved BOs are cleared in the PT.
+  * Make sure all BOs which are moved are updated in the PTs.
   * Returns 0 for success.
   *
-  * PTs have to be reserved and mutex must be locked!
+  * PTs have to be reserved!
   */
- int amdgpu_vm_clear_moved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
-                           struct amdgpu_sync *sync)
+ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
+                          struct amdgpu_vm *vm)
  {
-       struct amdgpu_bo_va *bo_va = NULL;
+       bool clear;
        int r = 0;
  
        spin_lock(&vm->status_lock);
        while (!list_empty(&vm->moved)) {
+               struct amdgpu_bo_va *bo_va;
                bo_va = list_first_entry(&vm->moved,
                        struct amdgpu_bo_va, base.vm_status);
                spin_unlock(&vm->status_lock);
  
-               r = amdgpu_vm_bo_update(adev, bo_va, true);
+               /* Per VM BOs never need to bo cleared in the page tables */
+               clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
+               r = amdgpu_vm_bo_update(adev, bo_va, clear);
                if (r)
                        return r;
  
        }
        spin_unlock(&vm->status_lock);
  
-       if (bo_va)
-               r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
        return r;
  }
  
@@@ -2049,6 -2118,39 +2118,39 @@@ struct amdgpu_bo_va *amdgpu_vm_bo_add(s
        return bo_va;
  }
  
+ /**
+  * amdgpu_vm_bo_insert_mapping - insert a new mapping
+  *
+  * @adev: amdgpu_device pointer
+  * @bo_va: bo_va to store the address
+  * @mapping: the mapping to insert
+  *
+  * Insert a new mapping into all structures.
+  */
+ static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
+                                   struct amdgpu_bo_va *bo_va,
+                                   struct amdgpu_bo_va_mapping *mapping)
+ {
+       struct amdgpu_vm *vm = bo_va->base.vm;
+       struct amdgpu_bo *bo = bo_va->base.bo;
+       mapping->bo_va = bo_va;
+       list_add(&mapping->list, &bo_va->invalids);
+       amdgpu_vm_it_insert(mapping, &vm->va);
+       if (mapping->flags & AMDGPU_PTE_PRT)
+               amdgpu_vm_prt_get(adev);
+       if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
+               spin_lock(&vm->status_lock);
+               if (list_empty(&bo_va->base.vm_status))
+                       list_add(&bo_va->base.vm_status, &vm->moved);
+               spin_unlock(&vm->status_lock);
+       }
+       trace_amdgpu_vm_bo_map(bo_va, mapping);
+ }
  /**
   * amdgpu_vm_bo_map - map bo inside a vm
   *
@@@ -2100,17 -2202,12 +2202,12 @@@ int amdgpu_vm_bo_map(struct amdgpu_devi
        if (!mapping)
                return -ENOMEM;
  
-       INIT_LIST_HEAD(&mapping->list);
        mapping->start = saddr;
        mapping->last = eaddr;
        mapping->offset = offset;
        mapping->flags = flags;
  
-       list_add(&mapping->list, &bo_va->invalids);
-       amdgpu_vm_it_insert(mapping, &vm->va);
-       if (flags & AMDGPU_PTE_PRT)
-               amdgpu_vm_prt_get(adev);
+       amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  
        return 0;
  }
@@@ -2137,7 -2234,6 +2234,6 @@@ int amdgpu_vm_bo_replace_map(struct amd
  {
        struct amdgpu_bo_va_mapping *mapping;
        struct amdgpu_bo *bo = bo_va->base.bo;
-       struct amdgpu_vm *vm = bo_va->base.vm;
        uint64_t eaddr;
        int r;
  
        mapping->offset = offset;
        mapping->flags = flags;
  
-       list_add(&mapping->list, &bo_va->invalids);
-       amdgpu_vm_it_insert(mapping, &vm->va);
-       if (flags & AMDGPU_PTE_PRT)
-               amdgpu_vm_prt_get(adev);
+       amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  
        return 0;
  }
@@@ -2221,6 -2313,7 +2313,7 @@@ int amdgpu_vm_bo_unmap(struct amdgpu_de
  
        list_del(&mapping->list);
        amdgpu_vm_it_remove(mapping, &vm->va);
+       mapping->bo_va = NULL;
        trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  
        if (valid)
@@@ -2306,6 -2399,7 +2399,7 @@@ int amdgpu_vm_bo_clear_mappings(struct 
                if (tmp->last > eaddr)
                    tmp->last = eaddr;
  
+               tmp->bo_va = NULL;
                list_add(&tmp->list, &vm->freed);
                trace_amdgpu_vm_bo_unmap(NULL, tmp);
        }
  }
  
  /**
+  * amdgpu_vm_bo_lookup_mapping - find mapping by address
+  *
+  * @vm: the requested VM
+  *
+  * Find a mapping by it's address.
+  */
+ struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
+                                                        uint64_t addr)
+ {
+       return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
+ }
+ /**
   * amdgpu_vm_bo_rmv - remove a bo to a specific vm
   *
   * @adev: amdgpu_device pointer
@@@ -2356,6 -2463,7 +2463,7 @@@ void amdgpu_vm_bo_rmv(struct amdgpu_dev
        list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
                list_del(&mapping->list);
                amdgpu_vm_it_remove(mapping, &vm->va);
+               mapping->bo_va = NULL;
                trace_amdgpu_vm_bo_unmap(bo_va, mapping);
                list_add(&mapping->list, &vm->freed);
        }
   * Mark @bo as invalid.
   */
  void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
-                            struct amdgpu_bo *bo)
+                            struct amdgpu_bo *bo, bool evicted)
  {
        struct amdgpu_vm_bo_base *bo_base;
  
        list_for_each_entry(bo_base, &bo->va, bo_list) {
+               struct amdgpu_vm *vm = bo_base->vm;
+               bo_base->moved = true;
+               if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
+                       spin_lock(&bo_base->vm->status_lock);
+                       if (bo->tbo.type == ttm_bo_type_kernel)
+                               list_move(&bo_base->vm_status, &vm->evicted);
+                       else
+                               list_move_tail(&bo_base->vm_status,
+                                              &vm->evicted);
+                       spin_unlock(&bo_base->vm->status_lock);
+                       continue;
+               }
+               if (bo->tbo.type == ttm_bo_type_kernel) {
+                       spin_lock(&bo_base->vm->status_lock);
+                       if (list_empty(&bo_base->vm_status))
+                               list_add(&bo_base->vm_status, &vm->relocated);
+                       spin_unlock(&bo_base->vm->status_lock);
+                       continue;
+               }
                spin_lock(&bo_base->vm->status_lock);
                if (list_empty(&bo_base->vm_status))
-                       list_add(&bo_base->vm_status,
-                                &bo_base->vm->moved);
+                       list_add(&bo_base->vm_status, &vm->moved);
                spin_unlock(&bo_base->vm->status_lock);
        }
  }
@@@ -2458,7 -2587,7 +2587,7 @@@ void amdgpu_vm_adjust_size(struct amdgp
   * Init @vm fields.
   */
  int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
-                  int vm_context)
+                  int vm_context, unsigned int pasid)
  {
        const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
                AMDGPU_VM_PTE_COUNT(adev) * 8);
        u64 flags;
        uint64_t init_pde_value = 0;
  
 -      vm->va = RB_ROOT;
 +      vm->va = RB_ROOT_CACHED;
        vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
        for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
                vm->reserved_vmid[i] = NULL;
        spin_lock_init(&vm->status_lock);
+       INIT_LIST_HEAD(&vm->evicted);
+       INIT_LIST_HEAD(&vm->relocated);
        INIT_LIST_HEAD(&vm->moved);
-       INIT_LIST_HEAD(&vm->cleared);
        INIT_LIST_HEAD(&vm->freed);
  
        /* create scheduler entity for page table updates */
                         vm->use_cpu_for_update ? "CPU" : "SDMA");
        WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
                  "CPU update of VM recommended only for large BAR system\n");
-       vm->last_dir_update = NULL;
+       vm->last_update = NULL;
  
        flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
                        AMDGPU_GEM_CREATE_VRAM_CLEARED;
        r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
                             AMDGPU_GEM_DOMAIN_VRAM,
                             flags,
-                            NULL, NULL, init_pde_value, &vm->root.bo);
+                            NULL, NULL, init_pde_value, &vm->root.base.bo);
        if (r)
                goto error_free_sched_entity;
  
-       r = amdgpu_bo_reserve(vm->root.bo, false);
-       if (r)
-               goto error_free_root;
-       vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
+       vm->root.base.vm = vm;
+       list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
+       INIT_LIST_HEAD(&vm->root.base.vm_status);
  
        if (vm->use_cpu_for_update) {
-               r = amdgpu_bo_kmap(vm->root.bo, NULL);
+               r = amdgpu_bo_reserve(vm->root.base.bo, false);
                if (r)
                        goto error_free_root;
+               r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
+               amdgpu_bo_unreserve(vm->root.base.bo);
+               if (r)
+                       goto error_free_root;
+       }
+       if (pasid) {
+               unsigned long flags;
+               spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
+               r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
+                             GFP_ATOMIC);
+               spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
+               if (r < 0)
+                       goto error_free_root;
+               vm->pasid = pasid;
        }
  
-       amdgpu_bo_unreserve(vm->root.bo);
+       INIT_KFIFO(vm->faults);
  
        return 0;
  
  error_free_root:
-       amdgpu_bo_unref(&vm->root.bo->shadow);
-       amdgpu_bo_unref(&vm->root.bo);
-       vm->root.bo = NULL;
+       amdgpu_bo_unref(&vm->root.base.bo->shadow);
+       amdgpu_bo_unref(&vm->root.base.bo);
+       vm->root.base.bo = NULL;
  
  error_free_sched_entity:
        amd_sched_entity_fini(&ring->sched, &vm->entity);
@@@ -2561,9 -2707,11 +2707,11 @@@ static void amdgpu_vm_free_levels(struc
  {
        unsigned i;
  
-       if (level->bo) {
-               amdgpu_bo_unref(&level->bo->shadow);
-               amdgpu_bo_unref(&level->bo);
+       if (level->base.bo) {
+               list_del(&level->base.bo_list);
+               list_del(&level->base.vm_status);
+               amdgpu_bo_unref(&level->base.bo->shadow);
+               amdgpu_bo_unref(&level->base.bo);
        }
  
        if (level->entries)
@@@ -2586,15 -2734,27 +2734,28 @@@ void amdgpu_vm_fini(struct amdgpu_devic
  {
        struct amdgpu_bo_va_mapping *mapping, *tmp;
        bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
+       u64 fault;
        int i;
  
+       /* Clear pending page faults from IH when the VM is destroyed */
+       while (kfifo_get(&vm->faults, &fault))
+               amdgpu_ih_clear_fault(adev, fault);
+       if (vm->pasid) {
+               unsigned long flags;
+               spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
+               idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
+               spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
+       }
        amd_sched_entity_fini(vm->entity.sched, &vm->entity);
  
 -      if (!RB_EMPTY_ROOT(&vm->va)) {
 +      if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
                dev_err(adev->dev, "still active bo inside vm\n");
        }
 -      rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
 +      rbtree_postorder_for_each_entry_safe(mapping, tmp,
 +                                           &vm->va.rb_root, rb) {
                list_del(&mapping->list);
                amdgpu_vm_it_remove(mapping, &vm->va);
                kfree(mapping);
        }
  
        amdgpu_vm_free_levels(&vm->root);
-       dma_fence_put(vm->last_dir_update);
+       dma_fence_put(vm->last_update);
        for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
                amdgpu_vm_free_reserved_vmid(adev, vm, i);
  }
@@@ -2668,6 -2828,8 +2829,8 @@@ void amdgpu_vm_manager_init(struct amdg
        adev->vm_manager.vm_update_mode = 0;
  #endif
  
+       idr_init(&adev->vm_manager.pasid_idr);
+       spin_lock_init(&adev->vm_manager.pasid_lock);
  }
  
  /**
@@@ -2681,6 -2843,9 +2844,9 @@@ void amdgpu_vm_manager_fini(struct amdg
  {
        unsigned i, j;
  
+       WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
+       idr_destroy(&adev->vm_manager.pasid_idr);
        for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
                struct amdgpu_vm_id_manager *id_mgr =
                        &adev->vm_manager.id_mgr[i];
@@@ -25,6 -25,7 +25,7 @@@
  #define __AMDGPU_VM_H__
  
  #include <linux/rbtree.h>
+ #include <linux/idr.h>
  
  #include "gpu_scheduler.h"
  #include "amdgpu_sync.h"
@@@ -105,37 -106,46 +106,46 @@@ struct amdgpu_vm_bo_base 
  
        /* protected by spinlock */
        struct list_head                vm_status;
+       /* protected by the BO being reserved */
+       bool                            moved;
  };
  
  struct amdgpu_vm_pt {
-       struct amdgpu_bo        *bo;
-       uint64_t                addr;
+       struct amdgpu_vm_bo_base        base;
+       uint64_t                        addr;
  
        /* array of page tables, one for each directory entry */
-       struct amdgpu_vm_pt     *entries;
-       unsigned                last_entry_used;
+       struct amdgpu_vm_pt             *entries;
+       unsigned                        last_entry_used;
  };
  
+ #define AMDGPU_VM_FAULT(pasid, addr) (((u64)(pasid) << 48) | (addr))
+ #define AMDGPU_VM_FAULT_PASID(fault) ((u64)(fault) >> 48)
+ #define AMDGPU_VM_FAULT_ADDR(fault)  ((u64)(fault) & 0xfffffffff000ULL)
  struct amdgpu_vm {
        /* tree of virtual addresses mapped */
 -      struct rb_root          va;
 +      struct rb_root_cached   va;
  
        /* protecting invalidated */
        spinlock_t              status_lock;
  
+       /* BOs who needs a validation */
+       struct list_head        evicted;
+       /* PT BOs which relocated and their parent need an update */
+       struct list_head        relocated;
        /* BOs moved, but not yet updated in the PT */
        struct list_head        moved;
  
-       /* BOs cleared in the PT because of a move */
-       struct list_head        cleared;
        /* BO mappings freed, but not yet updated in the PT */
        struct list_head        freed;
  
        /* contains the page directory */
        struct amdgpu_vm_pt     root;
-       struct dma_fence        *last_dir_update;
-       uint64_t                last_eviction_counter;
+       struct dma_fence        *last_update;
  
        /* protecting freed */
        spinlock_t              freed_lock;
        /* Scheduler entity for page table updates */
        struct amd_sched_entity entity;
  
-       /* client id */
+       /* client id and PASID (TODO: replace client_id with PASID) */
        u64                     client_id;
+       unsigned int            pasid;
        /* dedicated to vm */
        struct amdgpu_vm_id     *reserved_vmid[AMDGPU_MAX_VMHUBS];
  
  
        /* Flag to indicate ATS support from PTE for GFX9 */
        bool                    pte_support_ats;
+       /* Up to 128 pending page faults */
+       DECLARE_KFIFO(faults, u64, 128);
  };
  
  struct amdgpu_vm_id {
@@@ -215,16 -229,25 +229,25 @@@ struct amdgpu_vm_manager 
         * BIT1[= 0] Compute updated by SDMA [= 1] by CPU
         */
        int                                     vm_update_mode;
+       /* PASID to VM mapping, will be used in interrupt context to
+        * look up VM of a page fault
+        */
+       struct idr                              pasid_idr;
+       spinlock_t                              pasid_lock;
  };
  
+ int amdgpu_vm_alloc_pasid(unsigned int bits);
+ void amdgpu_vm_free_pasid(unsigned int pasid);
  void amdgpu_vm_manager_init(struct amdgpu_device *adev);
  void amdgpu_vm_manager_fini(struct amdgpu_device *adev);
  int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
-                  int vm_context);
+                  int vm_context, unsigned int pasid);
  void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm);
  void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
                         struct list_head *validated,
                         struct amdgpu_bo_list_entry *entry);
+ bool amdgpu_vm_ready(struct amdgpu_vm *vm);
  int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
                              int (*callback)(void *p, struct amdgpu_bo *bo),
                              void *param);
@@@ -243,13 -266,13 +266,13 @@@ int amdgpu_vm_update_directories(struc
  int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
                          struct amdgpu_vm *vm,
                          struct dma_fence **fence);
- int amdgpu_vm_clear_moved(struct amdgpu_device *adev, struct amdgpu_vm *vm,
-                         struct amdgpu_sync *sync);
+ int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
+                          struct amdgpu_vm *vm);
  int amdgpu_vm_bo_update(struct amdgpu_device *adev,
                        struct amdgpu_bo_va *bo_va,
                        bool clear);
  void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
-                            struct amdgpu_bo *bo);
+                            struct amdgpu_bo *bo, bool evicted);
  struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
                                       struct amdgpu_bo *bo);
  struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
@@@ -269,6 -292,8 +292,8 @@@ int amdgpu_vm_bo_unmap(struct amdgpu_de
  int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
                                struct amdgpu_vm *vm,
                                uint64_t saddr, uint64_t size);
+ struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
+                                                        uint64_t addr);
  void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
                      struct amdgpu_bo_va *bo_va);
  void amdgpu_vm_set_fragment_size(struct amdgpu_device *adev,