#include "amdgpu_trace.h"
 #include "amdgpu_amdkfd.h"
 #include "amdgpu_gmc.h"
+#include "amdgpu_xgmi.h"
 
 /**
  * DOC: GPUVM
        struct ttm_mem_reg *mem;
        struct drm_mm_node *nodes;
        struct dma_fence *exclusive, **last_update;
-       uint64_t flags;
        struct amdgpu_device *bo_adev = adev;
+       bool is_xgmi = false;
+       uint64_t flags;
        int r;
 
        if (clear || !bo) {
        if (bo) {
                flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
                bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
+               if (adev != bo_adev &&
+                   adev->gmc.xgmi.hive_id &&
+                   adev->gmc.xgmi.hive_id == bo_adev->gmc.xgmi.hive_id)
+                       is_xgmi = true;
        } else {
                flags = 0x0;
        }
        }
 
        list_for_each_entry(mapping, &bo_va->invalids, list) {
+               if (mapping->is_xgmi != is_xgmi) {
+                       if (is_xgmi) {
+                               /* Adding an XGMI mapping to the PT */
+                               if (atomic_inc_return(&adev->xgmi_map_counter) == 1)
+                                       amdgpu_xgmi_set_pstate(adev, 1);
+                       } else {
+                               /* Removing an XGMI mapping from the PT */
+                               if (atomic_dec_return(&adev->xgmi_map_counter) == 0)
+                                       amdgpu_xgmi_set_pstate(adev, 0);
+                       }
+                       mapping->is_xgmi = is_xgmi;
+               }
+
                r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
                                               mapping, flags, bo_adev, nodes,
                                               last_update);
                r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
                                                mapping->start, mapping->last,
                                                init_pte_value, 0, &f);
+
+               if (mapping->is_xgmi) {
+                       /* Removing an XGMI mapping from the PT */
+                       if (atomic_dec_return(&adev->xgmi_map_counter) == 0)
+                               amdgpu_xgmi_set_pstate(adev, 0);
+               }
+
                amdgpu_vm_free_mapping(adev, vm, mapping, f);
                if (r) {
                        dma_fence_put(f);
        mapping->last = eaddr;
        mapping->offset = offset;
        mapping->flags = flags;
+       mapping->is_xgmi = false;
 
        amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
 
 
 
        if (lock)
                mutex_lock(&tmp->hive_lock);
+       tmp->pstate = -1;
 
        mutex_unlock(&xgmi_mutex);
 
                mutex_unlock(&hive->hive_lock);
        }
 }
+
+int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate)
+{
+       int ret = 0;
+       struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, 0);
+
+       if (!hive)
+               return 0;
+
+       if (hive->pstate == pstate)
+               return 0;
+       /* Todo : sent the message to SMU for pstate change */
+       return ret;
+}
\ No newline at end of file
 
        struct kobject *kobj;
        struct device_attribute dev_attr;
        struct amdgpu_device *adev;
+       int pstate; /*0 -- low , 1 -- high , -1 unknown*/
 };
 
 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock);
 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev);
 int amdgpu_xgmi_add_device(struct amdgpu_device *adev);
 void amdgpu_xgmi_remove_device(struct amdgpu_device *adev);
+int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate);
 
 #endif