1f363d039SEric Auger /* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
29d8ad114SBrijesh Singh /*
39d8ad114SBrijesh Singh  * Userspace interface for AMD Secure Encrypted Virtualization (SEV)
49d8ad114SBrijesh Singh  * platform management commands.
59d8ad114SBrijesh Singh  *
69d8ad114SBrijesh Singh  * Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
79d8ad114SBrijesh Singh  *
89d8ad114SBrijesh Singh  * Author: Brijesh Singh <brijesh.singh@amd.com>
99d8ad114SBrijesh Singh  *
10d9cb4336SCornelia Huck  * SEV API specification is available at: https://developer.amd.com/sev/
119d8ad114SBrijesh Singh  */
129d8ad114SBrijesh Singh 
139d8ad114SBrijesh Singh #ifndef __PSP_SEV_USER_H__
149d8ad114SBrijesh Singh #define __PSP_SEV_USER_H__
159d8ad114SBrijesh Singh 
169d8ad114SBrijesh Singh #include <linux/types.h>
179d8ad114SBrijesh Singh 
189d8ad114SBrijesh Singh /**
199d8ad114SBrijesh Singh  * SEV platform commands
209d8ad114SBrijesh Singh  */
219d8ad114SBrijesh Singh enum {
229d8ad114SBrijesh Singh 	SEV_FACTORY_RESET = 0,
239d8ad114SBrijesh Singh 	SEV_PLATFORM_STATUS,
249d8ad114SBrijesh Singh 	SEV_PEK_GEN,
259d8ad114SBrijesh Singh 	SEV_PEK_CSR,
269d8ad114SBrijesh Singh 	SEV_PDH_GEN,
279d8ad114SBrijesh Singh 	SEV_PDH_CERT_EXPORT,
289d8ad114SBrijesh Singh 	SEV_PEK_CERT_IMPORT,
29d9cb4336SCornelia Huck 	SEV_GET_ID,	/* This command is deprecated, use SEV_GET_ID2 */
30d9cb4336SCornelia Huck 	SEV_GET_ID2,
31*ab0c7fb2SPaolo Bonzini 	SNP_PLATFORM_STATUS,
32*ab0c7fb2SPaolo Bonzini 	SNP_COMMIT,
33*ab0c7fb2SPaolo Bonzini 	SNP_SET_CONFIG,
349d8ad114SBrijesh Singh 
359d8ad114SBrijesh Singh 	SEV_MAX,
369d8ad114SBrijesh Singh };
379d8ad114SBrijesh Singh 
389d8ad114SBrijesh Singh /**
399d8ad114SBrijesh Singh  * SEV Firmware status code
409d8ad114SBrijesh Singh  */
419d8ad114SBrijesh Singh typedef enum {
42d0bf492fSCédric Le Goater 	/*
43d0bf492fSCédric Le Goater 	 * This error code is not in the SEV spec. Its purpose is to convey that
44d0bf492fSCédric Le Goater 	 * there was an error that prevented the SEV firmware from being called.
45d0bf492fSCédric Le Goater 	 * The SEV API error codes are 16 bits, so the -1 value will not overlap
46d0bf492fSCédric Le Goater 	 * with possible values from the specification.
47d0bf492fSCédric Le Goater 	 */
48d0bf492fSCédric Le Goater 	SEV_RET_NO_FW_CALL = -1,
499d8ad114SBrijesh Singh 	SEV_RET_SUCCESS = 0,
509d8ad114SBrijesh Singh 	SEV_RET_INVALID_PLATFORM_STATE,
519d8ad114SBrijesh Singh 	SEV_RET_INVALID_GUEST_STATE,
529d8ad114SBrijesh Singh 	SEV_RET_INAVLID_CONFIG,
539d8ad114SBrijesh Singh 	SEV_RET_INVALID_LEN,
549d8ad114SBrijesh Singh 	SEV_RET_ALREADY_OWNED,
559d8ad114SBrijesh Singh 	SEV_RET_INVALID_CERTIFICATE,
569d8ad114SBrijesh Singh 	SEV_RET_POLICY_FAILURE,
579d8ad114SBrijesh Singh 	SEV_RET_INACTIVE,
589d8ad114SBrijesh Singh 	SEV_RET_INVALID_ADDRESS,
599d8ad114SBrijesh Singh 	SEV_RET_BAD_SIGNATURE,
609d8ad114SBrijesh Singh 	SEV_RET_BAD_MEASUREMENT,
619d8ad114SBrijesh Singh 	SEV_RET_ASID_OWNED,
629d8ad114SBrijesh Singh 	SEV_RET_INVALID_ASID,
639d8ad114SBrijesh Singh 	SEV_RET_WBINVD_REQUIRED,
649d8ad114SBrijesh Singh 	SEV_RET_DFFLUSH_REQUIRED,
659d8ad114SBrijesh Singh 	SEV_RET_INVALID_GUEST,
669d8ad114SBrijesh Singh 	SEV_RET_INVALID_COMMAND,
679d8ad114SBrijesh Singh 	SEV_RET_ACTIVE,
689d8ad114SBrijesh Singh 	SEV_RET_HWSEV_RET_PLATFORM,
699d8ad114SBrijesh Singh 	SEV_RET_HWSEV_RET_UNSAFE,
709d8ad114SBrijesh Singh 	SEV_RET_UNSUPPORTED,
712a886794SGreg Kurz 	SEV_RET_INVALID_PARAM,
722a886794SGreg Kurz 	SEV_RET_RESOURCE_LIMIT,
732a886794SGreg Kurz 	SEV_RET_SECURE_DATA_INVALID,
74efb91426SDaniel Henrique Barboza 	SEV_RET_INVALID_KEY = 0x27,
75*ab0c7fb2SPaolo Bonzini 	SEV_RET_INVALID_PAGE_SIZE,
76*ab0c7fb2SPaolo Bonzini 	SEV_RET_INVALID_PAGE_STATE,
77*ab0c7fb2SPaolo Bonzini 	SEV_RET_INVALID_MDATA_ENTRY,
78*ab0c7fb2SPaolo Bonzini 	SEV_RET_INVALID_PAGE_OWNER,
79*ab0c7fb2SPaolo Bonzini 	SEV_RET_INVALID_PAGE_AEAD_OFLOW,
80*ab0c7fb2SPaolo Bonzini 	SEV_RET_RMP_INIT_REQUIRED,
819d8ad114SBrijesh Singh 	SEV_RET_MAX,
829d8ad114SBrijesh Singh } sev_ret_code;
839d8ad114SBrijesh Singh 
849d8ad114SBrijesh Singh /**
859d8ad114SBrijesh Singh  * struct sev_user_data_status - PLATFORM_STATUS command parameters
869d8ad114SBrijesh Singh  *
879d8ad114SBrijesh Singh  * @major: major API version
889d8ad114SBrijesh Singh  * @minor: minor API version
899d8ad114SBrijesh Singh  * @state: platform state
909d8ad114SBrijesh Singh  * @flags: platform config flags
919d8ad114SBrijesh Singh  * @build: firmware build id for API version
929d8ad114SBrijesh Singh  * @guest_count: number of active guests
939d8ad114SBrijesh Singh  */
949d8ad114SBrijesh Singh struct sev_user_data_status {
959d8ad114SBrijesh Singh 	__u8 api_major;				/* Out */
969d8ad114SBrijesh Singh 	__u8 api_minor;				/* Out */
979d8ad114SBrijesh Singh 	__u8 state;				/* Out */
989d8ad114SBrijesh Singh 	__u32 flags;				/* Out */
999d8ad114SBrijesh Singh 	__u8 build;				/* Out */
1009d8ad114SBrijesh Singh 	__u32 guest_count;			/* Out */
1019d8ad114SBrijesh Singh } __attribute__((packed));
1029d8ad114SBrijesh Singh 
103f76b348eSCornelia Huck #define SEV_STATUS_FLAGS_CONFIG_ES	0x0100
104f76b348eSCornelia Huck 
1059d8ad114SBrijesh Singh /**
1069d8ad114SBrijesh Singh  * struct sev_user_data_pek_csr - PEK_CSR command parameters
1079d8ad114SBrijesh Singh  *
1089d8ad114SBrijesh Singh  * @address: PEK certificate chain
1099d8ad114SBrijesh Singh  * @length: length of certificate
1109d8ad114SBrijesh Singh  */
1119d8ad114SBrijesh Singh struct sev_user_data_pek_csr {
1129d8ad114SBrijesh Singh 	__u64 address;				/* In */
1139d8ad114SBrijesh Singh 	__u32 length;				/* In/Out */
1149d8ad114SBrijesh Singh } __attribute__((packed));
1159d8ad114SBrijesh Singh 
1169d8ad114SBrijesh Singh /**
1179d8ad114SBrijesh Singh  * struct sev_user_data_cert_import - PEK_CERT_IMPORT command parameters
1189d8ad114SBrijesh Singh  *
1199d8ad114SBrijesh Singh  * @pek_address: PEK certificate chain
1209d8ad114SBrijesh Singh  * @pek_len: length of PEK certificate
1219d8ad114SBrijesh Singh  * @oca_address: OCA certificate chain
1229d8ad114SBrijesh Singh  * @oca_len: length of OCA certificate
1239d8ad114SBrijesh Singh  */
1249d8ad114SBrijesh Singh struct sev_user_data_pek_cert_import {
1259d8ad114SBrijesh Singh 	__u64 pek_cert_address;			/* In */
1269d8ad114SBrijesh Singh 	__u32 pek_cert_len;			/* In */
1279d8ad114SBrijesh Singh 	__u64 oca_cert_address;			/* In */
1289d8ad114SBrijesh Singh 	__u32 oca_cert_len;			/* In */
1299d8ad114SBrijesh Singh } __attribute__((packed));
1309d8ad114SBrijesh Singh 
1319d8ad114SBrijesh Singh /**
1329d8ad114SBrijesh Singh  * struct sev_user_data_pdh_cert_export - PDH_CERT_EXPORT command parameters
1339d8ad114SBrijesh Singh  *
1349d8ad114SBrijesh Singh  * @pdh_address: PDH certificate address
1359d8ad114SBrijesh Singh  * @pdh_len: length of PDH certificate
1369d8ad114SBrijesh Singh  * @cert_chain_address: PDH certificate chain
1379d8ad114SBrijesh Singh  * @cert_chain_len: length of PDH certificate chain
1389d8ad114SBrijesh Singh  */
1399d8ad114SBrijesh Singh struct sev_user_data_pdh_cert_export {
1409d8ad114SBrijesh Singh 	__u64 pdh_cert_address;			/* In */
1419d8ad114SBrijesh Singh 	__u32 pdh_cert_len;			/* In/Out */
1429d8ad114SBrijesh Singh 	__u64 cert_chain_address;		/* In */
1439d8ad114SBrijesh Singh 	__u32 cert_chain_len;			/* In/Out */
1449d8ad114SBrijesh Singh } __attribute__((packed));
1459d8ad114SBrijesh Singh 
1469d8ad114SBrijesh Singh /**
147d9cb4336SCornelia Huck  * struct sev_user_data_get_id - GET_ID command parameters (deprecated)
14877d361b1SEric Auger  *
14977d361b1SEric Auger  * @socket1: Buffer to pass unique ID of first socket
15077d361b1SEric Auger  * @socket2: Buffer to pass unique ID of second socket
15177d361b1SEric Auger  */
15277d361b1SEric Auger struct sev_user_data_get_id {
15377d361b1SEric Auger 	__u8 socket1[64];			/* Out */
15477d361b1SEric Auger 	__u8 socket2[64];			/* Out */
15577d361b1SEric Auger } __attribute__((packed));
15677d361b1SEric Auger 
15777d361b1SEric Auger /**
158d9cb4336SCornelia Huck  * struct sev_user_data_get_id2 - GET_ID command parameters
159d9cb4336SCornelia Huck  * @address: Buffer to store unique ID
160d9cb4336SCornelia Huck  * @length: length of the unique ID
161d9cb4336SCornelia Huck  */
162d9cb4336SCornelia Huck struct sev_user_data_get_id2 {
163d9cb4336SCornelia Huck 	__u64 address;				/* In */
164d9cb4336SCornelia Huck 	__u32 length;				/* In/Out */
165d9cb4336SCornelia Huck } __attribute__((packed));
166d9cb4336SCornelia Huck 
167d9cb4336SCornelia Huck /**
168*ab0c7fb2SPaolo Bonzini  * struct sev_user_data_snp_status - SNP status
169*ab0c7fb2SPaolo Bonzini  *
170*ab0c7fb2SPaolo Bonzini  * @api_major: API major version
171*ab0c7fb2SPaolo Bonzini  * @api_minor: API minor version
172*ab0c7fb2SPaolo Bonzini  * @state: current platform state
173*ab0c7fb2SPaolo Bonzini  * @is_rmp_initialized: whether RMP is initialized or not
174*ab0c7fb2SPaolo Bonzini  * @rsvd: reserved
175*ab0c7fb2SPaolo Bonzini  * @build_id: firmware build id for the API version
176*ab0c7fb2SPaolo Bonzini  * @mask_chip_id: whether chip id is present in attestation reports or not
177*ab0c7fb2SPaolo Bonzini  * @mask_chip_key: whether attestation reports are signed or not
178*ab0c7fb2SPaolo Bonzini  * @vlek_en: VLEK (Version Loaded Endorsement Key) hashstick is loaded
179*ab0c7fb2SPaolo Bonzini  * @rsvd1: reserved
180*ab0c7fb2SPaolo Bonzini  * @guest_count: the number of guest currently managed by the firmware
181*ab0c7fb2SPaolo Bonzini  * @current_tcb_version: current TCB version
182*ab0c7fb2SPaolo Bonzini  * @reported_tcb_version: reported TCB version
183*ab0c7fb2SPaolo Bonzini  */
184*ab0c7fb2SPaolo Bonzini struct sev_user_data_snp_status {
185*ab0c7fb2SPaolo Bonzini 	__u8 api_major;			/* Out */
186*ab0c7fb2SPaolo Bonzini 	__u8 api_minor;			/* Out */
187*ab0c7fb2SPaolo Bonzini 	__u8 state;			/* Out */
188*ab0c7fb2SPaolo Bonzini 	__u8 is_rmp_initialized:1;	/* Out */
189*ab0c7fb2SPaolo Bonzini 	__u8 rsvd:7;
190*ab0c7fb2SPaolo Bonzini 	__u32 build_id;			/* Out */
191*ab0c7fb2SPaolo Bonzini 	__u32 mask_chip_id:1;		/* Out */
192*ab0c7fb2SPaolo Bonzini 	__u32 mask_chip_key:1;		/* Out */
193*ab0c7fb2SPaolo Bonzini 	__u32 vlek_en:1;		/* Out */
194*ab0c7fb2SPaolo Bonzini 	__u32 rsvd1:29;
195*ab0c7fb2SPaolo Bonzini 	__u32 guest_count;		/* Out */
196*ab0c7fb2SPaolo Bonzini 	__u64 current_tcb_version;	/* Out */
197*ab0c7fb2SPaolo Bonzini 	__u64 reported_tcb_version;	/* Out */
198*ab0c7fb2SPaolo Bonzini } __attribute__((packed));
199*ab0c7fb2SPaolo Bonzini 
200*ab0c7fb2SPaolo Bonzini /**
201*ab0c7fb2SPaolo Bonzini  * struct sev_user_data_snp_config - system wide configuration value for SNP.
202*ab0c7fb2SPaolo Bonzini  *
203*ab0c7fb2SPaolo Bonzini  * @reported_tcb: the TCB version to report in the guest attestation report.
204*ab0c7fb2SPaolo Bonzini  * @mask_chip_id: whether chip id is present in attestation reports or not
205*ab0c7fb2SPaolo Bonzini  * @mask_chip_key: whether attestation reports are signed or not
206*ab0c7fb2SPaolo Bonzini  * @rsvd: reserved
207*ab0c7fb2SPaolo Bonzini  * @rsvd1: reserved
208*ab0c7fb2SPaolo Bonzini  */
209*ab0c7fb2SPaolo Bonzini struct sev_user_data_snp_config {
210*ab0c7fb2SPaolo Bonzini 	__u64 reported_tcb  ;   /* In */
211*ab0c7fb2SPaolo Bonzini 	__u32 mask_chip_id:1;   /* In */
212*ab0c7fb2SPaolo Bonzini 	__u32 mask_chip_key:1;  /* In */
213*ab0c7fb2SPaolo Bonzini 	__u32 rsvd:30;          /* In */
214*ab0c7fb2SPaolo Bonzini 	__u8 rsvd1[52];
215*ab0c7fb2SPaolo Bonzini } __attribute__((packed));
216*ab0c7fb2SPaolo Bonzini 
217*ab0c7fb2SPaolo Bonzini /**
2189d8ad114SBrijesh Singh  * struct sev_issue_cmd - SEV ioctl parameters
2199d8ad114SBrijesh Singh  *
2209d8ad114SBrijesh Singh  * @cmd: SEV commands to execute
2219d8ad114SBrijesh Singh  * @opaque: pointer to the command structure
2229d8ad114SBrijesh Singh  * @error: SEV FW return code on failure
2239d8ad114SBrijesh Singh  */
2249d8ad114SBrijesh Singh struct sev_issue_cmd {
2259d8ad114SBrijesh Singh 	__u32 cmd;				/* In */
2269d8ad114SBrijesh Singh 	__u64 data;				/* In */
2279d8ad114SBrijesh Singh 	__u32 error;				/* Out */
2289d8ad114SBrijesh Singh } __attribute__((packed));
2299d8ad114SBrijesh Singh 
2309d8ad114SBrijesh Singh #define SEV_IOC_TYPE		'S'
2319d8ad114SBrijesh Singh #define SEV_ISSUE_CMD	_IOWR(SEV_IOC_TYPE, 0x0, struct sev_issue_cmd)
2329d8ad114SBrijesh Singh 
2339d8ad114SBrijesh Singh #endif /* __PSP_USER_SEV_H */
234