Skip to content

Commit 33cfb80

Browse files
ashkalraherbertx
authored andcommitted
crypto: ccp - Add support for SNP_FEATURE_INFO command
The FEATURE_INFO command provides hypervisors with a programmatic means to learn about the supported features of the currently loaded firmware. This command mimics the CPUID instruction relative to sub-leaf input and the four unsigned integer output values. To obtain information regarding the features present in the currently loaded SEV firmware, use the SNP_FEATURE_INFO command. Cache the SNP platform status and feature information from CPUID 0x8000_0024 in the sev_device structure. If SNP is enabled, utilize this cached SNP platform status for the API major, minor and build version. Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com> Signed-off-by: Ashish Kalra <ashish.kalra@amd.com> Reviewed-by: Kim Phillips <kim.phillips@amd.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 459daec commit 33cfb80

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

drivers/crypto/ccp/sev-dev.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ static int sev_cmd_buffer_len(int cmd)
233233
case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request);
234234
case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_user_data_snp_config);
235235
case SEV_CMD_SNP_COMMIT: return sizeof(struct sev_data_snp_commit);
236+
case SEV_CMD_SNP_FEATURE_INFO: return sizeof(struct sev_data_snp_feature_info);
236237
default: return 0;
237238
}
238239

@@ -1073,6 +1074,67 @@ static void snp_set_hsave_pa(void *arg)
10731074
wrmsrq(MSR_VM_HSAVE_PA, 0);
10741075
}
10751076

1077+
static int snp_get_platform_data(struct sev_device *sev, int *error)
1078+
{
1079+
struct sev_data_snp_feature_info snp_feat_info;
1080+
struct snp_feature_info *feat_info;
1081+
struct sev_data_snp_addr buf;
1082+
struct page *page;
1083+
int rc;
1084+
1085+
/*
1086+
* This function is expected to be called before SNP is
1087+
* initialized.
1088+
*/
1089+
if (sev->snp_initialized)
1090+
return -EINVAL;
1091+
1092+
buf.address = __psp_pa(&sev->snp_plat_status);
1093+
rc = sev_do_cmd(SEV_CMD_SNP_PLATFORM_STATUS, &buf, error);
1094+
if (rc) {
1095+
dev_err(sev->dev, "SNP PLATFORM_STATUS command failed, ret = %d, error = %#x\n",
1096+
rc, *error);
1097+
return rc;
1098+
}
1099+
1100+
sev->api_major = sev->snp_plat_status.api_major;
1101+
sev->api_minor = sev->snp_plat_status.api_minor;
1102+
sev->build = sev->snp_plat_status.build_id;
1103+
1104+
/*
1105+
* Do feature discovery of the currently loaded firmware,
1106+
* and cache feature information from CPUID 0x8000_0024,
1107+
* sub-function 0.
1108+
*/
1109+
if (!sev->snp_plat_status.feature_info)
1110+
return 0;
1111+
1112+
/*
1113+
* Use dynamically allocated structure for the SNP_FEATURE_INFO
1114+
* command to ensure structure is 8-byte aligned, and does not
1115+
* cross a page boundary.
1116+
*/
1117+
page = alloc_page(GFP_KERNEL);
1118+
if (!page)
1119+
return -ENOMEM;
1120+
1121+
feat_info = page_address(page);
1122+
snp_feat_info.length = sizeof(snp_feat_info);
1123+
snp_feat_info.ecx_in = 0;
1124+
snp_feat_info.feature_info_paddr = __psp_pa(feat_info);
1125+
1126+
rc = sev_do_cmd(SEV_CMD_SNP_FEATURE_INFO, &snp_feat_info, error);
1127+
if (!rc)
1128+
sev->snp_feat_info_0 = *feat_info;
1129+
else
1130+
dev_err(sev->dev, "SNP FEATURE_INFO command failed, ret = %d, error = %#x\n",
1131+
rc, *error);
1132+
1133+
__free_page(page);
1134+
1135+
return rc;
1136+
}
1137+
10761138
static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg)
10771139
{
10781140
struct sev_data_range_list *range_list = arg;
@@ -1599,6 +1661,16 @@ static int sev_get_api_version(void)
15991661
struct sev_user_data_status status;
16001662
int error = 0, ret;
16011663

1664+
/*
1665+
* Cache SNP platform status and SNP feature information
1666+
* if SNP is available.
1667+
*/
1668+
if (cc_platform_has(CC_ATTR_HOST_SEV_SNP)) {
1669+
ret = snp_get_platform_data(sev, &error);
1670+
if (ret)
1671+
return 1;
1672+
}
1673+
16021674
ret = sev_platform_status(&status, &error);
16031675
if (ret) {
16041676
dev_err(sev->dev,

drivers/crypto/ccp/sev-dev.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ struct sev_device {
5858
bool snp_initialized;
5959

6060
struct sev_user_data_status sev_plat_status;
61+
62+
struct sev_user_data_snp_status snp_plat_status;
63+
struct snp_feature_info snp_feat_info_0;
6164
};
6265

6366
int sev_dev_init(struct psp_device *psp);

include/linux/psp-sev.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ enum sev_cmd {
107107
SEV_CMD_SNP_DOWNLOAD_FIRMWARE_EX = 0x0CA,
108108
SEV_CMD_SNP_COMMIT = 0x0CB,
109109
SEV_CMD_SNP_VLEK_LOAD = 0x0CD,
110+
SEV_CMD_SNP_FEATURE_INFO = 0x0CE,
110111

111112
SEV_CMD_MAX,
112113
};
@@ -814,6 +815,34 @@ struct sev_data_snp_commit {
814815
u32 len;
815816
} __packed;
816817

818+
/**
819+
* struct sev_data_snp_feature_info - SEV_SNP_FEATURE_INFO structure
820+
*
821+
* @length: len of the command buffer read by the PSP
822+
* @ecx_in: subfunction index
823+
* @feature_info_paddr : System Physical Address of the FEATURE_INFO structure
824+
*/
825+
struct sev_data_snp_feature_info {
826+
u32 length;
827+
u32 ecx_in;
828+
u64 feature_info_paddr;
829+
} __packed;
830+
831+
/**
832+
* struct feature_info - FEATURE_INFO structure
833+
*
834+
* @eax: output of SNP_FEATURE_INFO command
835+
* @ebx: output of SNP_FEATURE_INFO command
836+
* @ecx: output of SNP_FEATURE_INFO command
837+
* #edx: output of SNP_FEATURE_INFO command
838+
*/
839+
struct snp_feature_info {
840+
u32 eax;
841+
u32 ebx;
842+
u32 ecx;
843+
u32 edx;
844+
} __packed;
845+
817846
#ifdef CONFIG_CRYPTO_DEV_SP_PSP
818847

819848
/**

0 commit comments

Comments
 (0)