Merge tag 'drm-misc-next-2021-07-16' of git://anongit.freedesktop.org/drm/drm-misc...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_object.c
index 22fb4ab43eefc4aa257e95d022552b32340d94eb..261283f5b4249719cfb0ec7c352dee1437f18d6f 100644 (file)
 
 static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
 {
-       struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
        struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
-       struct amdgpu_bo_user *ubo;
 
        amdgpu_bo_kunmap(bo);
 
        if (bo->tbo.base.import_attach)
                drm_prime_gem_destroy(&bo->tbo.base, bo->tbo.sg);
        drm_gem_object_release(&bo->tbo.base);
+       amdgpu_bo_unref(&bo->parent);
+       kvfree(bo);
+}
+
+static void amdgpu_bo_user_destroy(struct ttm_buffer_object *tbo)
+{
+       struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
+       struct amdgpu_bo_user *ubo;
+
+       ubo = to_amdgpu_bo_user(bo);
+       kfree(ubo->metadata);
+       amdgpu_bo_destroy(tbo);
+}
+
+static void amdgpu_bo_vm_destroy(struct ttm_buffer_object *tbo)
+{
+       struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
+       struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
+       struct amdgpu_bo_vm *vmbo;
+
+       vmbo = to_amdgpu_bo_vm(bo);
        /* in case amdgpu_device_recover_vram got NULL of bo->parent */
-       if (!list_empty(&bo->shadow_list)) {
+       if (!list_empty(&vmbo->shadow_list)) {
                mutex_lock(&adev->shadow_list_lock);
-               list_del_init(&bo->shadow_list);
+               list_del_init(&vmbo->shadow_list);
                mutex_unlock(&adev->shadow_list_lock);
        }
-       amdgpu_bo_unref(&bo->parent);
-
-       if (bo->tbo.type != ttm_bo_type_kernel) {
-               ubo = to_amdgpu_bo_user(bo);
-               kfree(ubo->metadata);
-       }
 
-       kvfree(bo);
+       amdgpu_bo_destroy(tbo);
 }
 
 /**
@@ -91,8 +104,11 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
  */
 bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
 {
-       if (bo->destroy == &amdgpu_bo_destroy)
+       if (bo->destroy == &amdgpu_bo_destroy ||
+           bo->destroy == &amdgpu_bo_user_destroy ||
+           bo->destroy == &amdgpu_bo_vm_destroy)
                return true;
+
        return false;
 }
 
@@ -545,7 +561,6 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
        if (bo == NULL)
                return -ENOMEM;
        drm_gem_private_object_init(adev_to_drm(adev), &bo->tbo.base, size);
-       INIT_LIST_HEAD(&bo->shadow_list);
        bo->vm_bo = NULL;
        bo->preferred_domains = bp->preferred_domain ? bp->preferred_domain :
                bp->domain;
@@ -568,9 +583,12 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
        if (bp->type == ttm_bo_type_kernel)
                bo->tbo.priority = 1;
 
+       if (!bp->destroy)
+               bp->destroy = &amdgpu_bo_destroy;
+
        r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
                                 &bo->placement, page_align, &ctx,  NULL,
-                                bp->resv, &amdgpu_bo_destroy);
+                                bp->resv, bp->destroy);
        if (unlikely(r != 0))
                return r;
 
@@ -634,6 +652,7 @@ int amdgpu_bo_create_user(struct amdgpu_device *adev,
        int r;
 
        bp->bo_ptr_size = sizeof(struct amdgpu_bo_user);
+       bp->destroy = &amdgpu_bo_user_destroy;
        r = amdgpu_bo_create(adev, bp, &bo_ptr);
        if (r)
                return r;
@@ -665,11 +684,13 @@ int amdgpu_bo_create_vm(struct amdgpu_device *adev,
         * num of amdgpu_vm_pt entries.
         */
        BUG_ON(bp->bo_ptr_size < sizeof(struct amdgpu_bo_vm));
+       bp->destroy = &amdgpu_bo_vm_destroy;
        r = amdgpu_bo_create(adev, bp, &bo_ptr);
        if (r)
                return r;
 
        *vmbo_ptr = to_amdgpu_bo_vm(bo_ptr);
+       INIT_LIST_HEAD(&(*vmbo_ptr)->shadow_list);
        return r;
 }
 
@@ -714,12 +735,12 @@ retry:
  *
  * Insert a BO to the shadow list.
  */
-void amdgpu_bo_add_to_shadow_list(struct amdgpu_bo *bo)
+void amdgpu_bo_add_to_shadow_list(struct amdgpu_bo_vm *vmbo)
 {
-       struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
+       struct amdgpu_device *adev = amdgpu_ttm_adev(vmbo->bo.tbo.bdev);
 
        mutex_lock(&adev->shadow_list_lock);
-       list_add_tail(&bo->shadow_list, &adev->shadow_list);
+       list_add_tail(&vmbo->shadow_list, &adev->shadow_list);
        mutex_unlock(&adev->shadow_list_lock);
 }