amdgpu_bo_init() reserves the WC memtype for the VRAM aperture and adds
an MTRR before calling amdgpu_ttm_init(). If amdgpu_ttm_init() fails,
the GMC sw_init callers just return the error and GMC's sw_fini is never
run, so amdgpu_bo_fini(), the only place that drops the memtype and MTRR,
is never called. A later rebind then reserves the same aperture again
with the old reservation still held.

Undo the memtype reservation and the MTRR in amdgpu_bo_init() itself when
amdgpu_ttm_init() fails.

Signed-off-by: Mario Limonciello <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
index 6aa895f63aeaf..0b34458301f3f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
@@ -1108,11 +1108,13 @@ static const char * const amdgpu_vram_names[] = {
  */
 int amdgpu_bo_init(struct amdgpu_device *adev)
 {
+       int r;
+
        /* On A+A platform, VRAM can be mapped as WB */
        if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) {
                /* reserve PAT memory space to WC for VRAM */
-               int r = arch_io_reserve_memtype_wc(adev->gmc.aper_base,
-                               adev->gmc.aper_size);
+               r = arch_io_reserve_memtype_wc(adev->gmc.aper_base,
+                                              adev->gmc.aper_size);
 
                if (r) {
                        DRM_ERROR("Unable to set WC memtype for the aperture 
base\n");
@@ -1129,7 +1131,19 @@ int amdgpu_bo_init(struct amdgpu_device *adev)
                 (unsigned long long)adev->gmc.aper_size >> 20);
        drm_info(adev_to_drm(adev), "RAM width %dbits %s\n",
                 adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
-       return amdgpu_ttm_init(adev);
+
+       r = amdgpu_ttm_init(adev);
+       if (r) {
+               /* the caller does not call amdgpu_bo_fini() when we fail */
+               if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) {
+                       arch_phys_wc_del(adev->gmc.vram_mtrr);
+                       arch_io_free_memtype_wc(adev->gmc.aper_base,
+                                               adev->gmc.aper_size);
+               }
+               return r;
+       }
+
+       return 0;
 }
 
 /**

-- 
2.43.0

Reply via email to