Skip to content

Commit 04d65a9

Browse files
ashkalrabp3tk0v
authored andcommitted
iommu/amd: Don't rely on external callers to enable IOMMU SNP support
Currently, the expectation is that the kernel will call amd_iommu_snp_enable() to perform various checks and set the amd_iommu_snp_en flag that the IOMMU uses to adjust its setup routines to account for additional requirements on hosts where SNP is enabled. This is somewhat fragile as it relies on this call being done prior to IOMMU setup. It is more robust to just do this automatically as part of IOMMU initialization, so rework the code accordingly. There is still a need to export information about whether or not the IOMMU is configured in a manner compatible with SNP, so relocate the existing amd_iommu_snp_en flag so it can be used to convey that information in place of the return code that was previously provided by calls to amd_iommu_snp_enable(). While here, also adjust the kernel messages related to IOMMU SNP enablement for consistency/grammar/clarity. Suggested-by: Borislav Petkov (AMD) <bp@alien8.de> Signed-off-by: Ashish Kalra <ashish.kalra@amd.com> Co-developed-by: Michael Roth <michael.roth@amd.com> Signed-off-by: Michael Roth <michael.roth@amd.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Acked-by: Joerg Roedel <jroedel@suse.de> Link: https://lore.kernel.org/r/20240126041126.1927228-4-michael.roth@amd.com
1 parent acaa4b5 commit 04d65a9

File tree

4 files changed

+32
-43
lines changed

4 files changed

+32
-43
lines changed

arch/x86/include/asm/iommu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern int force_iommu, no_iommu;
1010
extern int iommu_detected;
1111
extern int iommu_merge;
1212
extern int panic_on_overflow;
13+
extern bool amd_iommu_snp_en;
1314

1415
#ifdef CONFIG_SWIOTLB
1516
extern bool x86_swiotlb_enable;

drivers/iommu/amd/amd_iommu.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,4 @@ void amd_iommu_domain_set_pgtable(struct protection_domain *domain,
164164
u64 *root, int mode);
165165
struct dev_table_entry *get_dev_table(struct amd_iommu *iommu);
166166

167-
extern bool amd_iommu_snp_en;
168167
#endif

drivers/iommu/amd/init.c

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,6 +3221,36 @@ static bool __init detect_ivrs(void)
32213221
return true;
32223222
}
32233223

3224+
static void iommu_snp_enable(void)
3225+
{
3226+
#ifdef CONFIG_KVM_AMD_SEV
3227+
if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP))
3228+
return;
3229+
/*
3230+
* The SNP support requires that IOMMU must be enabled, and is
3231+
* not configured in the passthrough mode.
3232+
*/
3233+
if (no_iommu || iommu_default_passthrough()) {
3234+
pr_err("SNP: IOMMU disabled or configured in passthrough mode, SNP cannot be supported.\n");
3235+
return;
3236+
}
3237+
3238+
amd_iommu_snp_en = check_feature(FEATURE_SNP);
3239+
if (!amd_iommu_snp_en) {
3240+
pr_err("SNP: IOMMU SNP feature not enabled, SNP cannot be supported.\n");
3241+
return;
3242+
}
3243+
3244+
pr_info("IOMMU SNP support enabled.\n");
3245+
3246+
/* Enforce IOMMU v1 pagetable when SNP is enabled. */
3247+
if (amd_iommu_pgtable != AMD_IOMMU_V1) {
3248+
pr_warn("Forcing use of AMD IOMMU v1 page table due to SNP.\n");
3249+
amd_iommu_pgtable = AMD_IOMMU_V1;
3250+
}
3251+
#endif
3252+
}
3253+
32243254
/****************************************************************************
32253255
*
32263256
* AMD IOMMU Initialization State Machine
@@ -3256,6 +3286,7 @@ static int __init state_next(void)
32563286
break;
32573287
case IOMMU_ENABLED:
32583288
register_syscore_ops(&amd_iommu_syscore_ops);
3289+
iommu_snp_enable();
32593290
ret = amd_iommu_init_pci();
32603291
init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
32613292
break;
@@ -3766,41 +3797,3 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64
37663797

37673798
return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
37683799
}
3769-
3770-
#ifdef CONFIG_AMD_MEM_ENCRYPT
3771-
int amd_iommu_snp_enable(void)
3772-
{
3773-
/*
3774-
* The SNP support requires that IOMMU must be enabled, and is
3775-
* not configured in the passthrough mode.
3776-
*/
3777-
if (no_iommu || iommu_default_passthrough()) {
3778-
pr_err("SNP: IOMMU is disabled or configured in passthrough mode, SNP cannot be supported");
3779-
return -EINVAL;
3780-
}
3781-
3782-
/*
3783-
* Prevent enabling SNP after IOMMU_ENABLED state because this process
3784-
* affect how IOMMU driver sets up data structures and configures
3785-
* IOMMU hardware.
3786-
*/
3787-
if (init_state > IOMMU_ENABLED) {
3788-
pr_err("SNP: Too late to enable SNP for IOMMU.\n");
3789-
return -EINVAL;
3790-
}
3791-
3792-
amd_iommu_snp_en = check_feature(FEATURE_SNP);
3793-
if (!amd_iommu_snp_en)
3794-
return -EINVAL;
3795-
3796-
pr_info("SNP enabled\n");
3797-
3798-
/* Enforce IOMMU v1 pagetable when SNP is enabled. */
3799-
if (amd_iommu_pgtable != AMD_IOMMU_V1) {
3800-
pr_warn("Force to using AMD IOMMU v1 page table due to SNP\n");
3801-
amd_iommu_pgtable = AMD_IOMMU_V1;
3802-
}
3803-
3804-
return 0;
3805-
}
3806-
#endif

include/linux/amd-iommu.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,4 @@ int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn,
8585
u64 *value);
8686
struct amd_iommu *get_amd_iommu(unsigned int idx);
8787

88-
#ifdef CONFIG_AMD_MEM_ENCRYPT
89-
int amd_iommu_snp_enable(void);
90-
#endif
91-
9288
#endif /* _ASM_X86_AMD_IOMMU_H */

0 commit comments

Comments
 (0)