Skip to content

Commit fb2acca

Browse files
codomaniajoergroedel
authored andcommitted
iommu/amd: Introduce function to check and enable SNP
To support SNP, IOMMU needs to be enabled, and prohibits IOMMU configurations where DTE[Mode]=0, which means it cannot be supported with IOMMU passthrough domain (a.k.a IOMMU_DOMAIN_IDENTITY), and when AMD IOMMU driver is configured to not use the IOMMU host (v1) page table. Otherwise, RMP table initialization could cause the system to crash. The request to enable SNP support in IOMMU must be done before PCI initialization state of the IOMMU driver because enabling SNP affects how IOMMU driver sets up IOMMU data structures (i.e. DTE). Unlike other IOMMU features, SNP feature does not have an enable bit in the IOMMU control register. Instead, the IOMMU driver introduces an amd_iommu_snp_en variable to track enabling state of SNP. Introduce amd_iommu_snp_enable() for other drivers to request enabling the SNP support in IOMMU, which checks all prerequisites and determines if the feature can be safely enabled. Please see the IOMMU spec section 2.12 for further details. Reviewed-by: Robin Murphy <[email protected]> Co-developed-by: Suravee Suthikulpanit <[email protected]> Signed-off-by: Suravee Suthikulpanit <[email protected]> Signed-off-by: Brijesh Singh <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent 02c6f31 commit fb2acca

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

drivers/iommu/amd/amd_iommu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,6 @@ extern struct dev_table_entry *get_dev_table(struct amd_iommu *iommu);
140140

141141
extern u64 amd_iommu_efr;
142142
extern u64 amd_iommu_efr2;
143+
144+
extern bool amd_iommu_snp_en;
143145
#endif

drivers/iommu/amd/init.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ static int amd_iommu_target_ivhd_type;
168168
u64 amd_iommu_efr;
169169
u64 amd_iommu_efr2;
170170

171+
/* SNP is enabled on the system? */
172+
bool amd_iommu_snp_en;
173+
EXPORT_SYMBOL(amd_iommu_snp_en);
174+
171175
LIST_HEAD(amd_iommu_pci_seg_list); /* list of all PCI segments */
172176
LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
173177
system */
@@ -3556,3 +3560,41 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64
35563560

35573561
return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
35583562
}
3563+
3564+
#ifdef CONFIG_AMD_MEM_ENCRYPT
3565+
int amd_iommu_snp_enable(void)
3566+
{
3567+
/*
3568+
* The SNP support requires that IOMMU must be enabled, and is
3569+
* not configured in the passthrough mode.
3570+
*/
3571+
if (no_iommu || iommu_default_passthrough()) {
3572+
pr_err("SNP: IOMMU is disabled or configured in passthrough mode, SNP cannot be supported");
3573+
return -EINVAL;
3574+
}
3575+
3576+
/*
3577+
* Prevent enabling SNP after IOMMU_ENABLED state because this process
3578+
* affect how IOMMU driver sets up data structures and configures
3579+
* IOMMU hardware.
3580+
*/
3581+
if (init_state > IOMMU_ENABLED) {
3582+
pr_err("SNP: Too late to enable SNP for IOMMU.\n");
3583+
return -EINVAL;
3584+
}
3585+
3586+
amd_iommu_snp_en = check_feature_on_all_iommus(FEATURE_SNP);
3587+
if (!amd_iommu_snp_en)
3588+
return -EINVAL;
3589+
3590+
pr_info("SNP enabled\n");
3591+
3592+
/* Enforce IOMMU v1 pagetable when SNP is enabled. */
3593+
if (amd_iommu_pgtable != AMD_IOMMU_V1) {
3594+
pr_warn("Force to using AMD IOMMU v1 page table due to SNP\n");
3595+
amd_iommu_pgtable = AMD_IOMMU_V1;
3596+
}
3597+
3598+
return 0;
3599+
}
3600+
#endif

include/linux/amd-iommu.h

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

209+
#ifdef CONFIG_AMD_MEM_ENCRYPT
210+
int amd_iommu_snp_enable(void);
211+
#endif
212+
209213
#endif /* _ASM_X86_AMD_IOMMU_H */

0 commit comments

Comments
 (0)