init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
                if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
                        pr_info("AMD IOMMU disabled on kernel command-line\n");
-                       free_dma_resources();
-                       free_iommu_resources();
                        init_state = IOMMU_CMDLINE_DISABLED;
                        ret = -EINVAL;
                }
                BUG();
        }
 
+       if (ret) {
+               free_dma_resources();
+               if (!irq_remapping_enabled) {
+                       disable_iommus();
+                       free_iommu_resources();
+               } else {
+                       struct amd_iommu *iommu;
+
+                       uninit_device_table_dma();
+                       for_each_iommu(iommu)
+                               iommu_flush_all_caches(iommu);
+               }
+       }
        return ret;
 }
 
        int ret;
 
        ret = iommu_go_to_state(IOMMU_INITIALIZED);
-       if (ret) {
-               free_dma_resources();
-               if (!irq_remapping_enabled) {
-                       disable_iommus();
-                       free_iommu_resources();
-               } else {
-                       uninit_device_table_dma();
-                       for_each_iommu(iommu)
-                               iommu_flush_all_caches(iommu);
-               }
-       }
-
 #ifdef CONFIG_GART_IOMMU
        if (ret && list_empty(&amd_iommu_list)) {
                /*