1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * AMD Secure Encrypted Virtualization (SEV) driver interface
5 * Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
9 * SEV API spec is available at https://developer.amd.com/sev
15 #include <uapi/linux/psp-sev.h>
18 #include <linux/mem_encrypt.h>
20 #define __psp_pa(x) __sme_pa(x)
22 #define __psp_pa(x) __pa(x)
25 #define SEV_FW_BLOB_MAX_SIZE 0x4000 /* 16KB */
31 SEV_STATE_UNINIT = 0x0,
33 SEV_STATE_WORKING = 0x2,
39 * SEV platform and guest management commands
42 /* platform commands */
44 SEV_CMD_SHUTDOWN = 0x002,
45 SEV_CMD_FACTORY_RESET = 0x003,
46 SEV_CMD_PLATFORM_STATUS = 0x004,
47 SEV_CMD_PEK_GEN = 0x005,
48 SEV_CMD_PEK_CSR = 0x006,
49 SEV_CMD_PEK_CERT_IMPORT = 0x007,
50 SEV_CMD_PDH_CERT_EXPORT = 0x008,
51 SEV_CMD_PDH_GEN = 0x009,
52 SEV_CMD_DF_FLUSH = 0x00A,
53 SEV_CMD_DOWNLOAD_FIRMWARE = 0x00B,
54 SEV_CMD_GET_ID = 0x00C,
57 SEV_CMD_DECOMMISSION = 0x020,
58 SEV_CMD_ACTIVATE = 0x021,
59 SEV_CMD_DEACTIVATE = 0x022,
60 SEV_CMD_GUEST_STATUS = 0x023,
62 /* Guest launch commands */
63 SEV_CMD_LAUNCH_START = 0x030,
64 SEV_CMD_LAUNCH_UPDATE_DATA = 0x031,
65 SEV_CMD_LAUNCH_UPDATE_VMSA = 0x032,
66 SEV_CMD_LAUNCH_MEASURE = 0x033,
67 SEV_CMD_LAUNCH_UPDATE_SECRET = 0x034,
68 SEV_CMD_LAUNCH_FINISH = 0x035,
70 /* Guest migration commands (outgoing) */
71 SEV_CMD_SEND_START = 0x040,
72 SEV_CMD_SEND_UPDATE_DATA = 0x041,
73 SEV_CMD_SEND_UPDATE_VMSA = 0x042,
74 SEV_CMD_SEND_FINISH = 0x043,
76 /* Guest migration commands (incoming) */
77 SEV_CMD_RECEIVE_START = 0x050,
78 SEV_CMD_RECEIVE_UPDATE_DATA = 0x051,
79 SEV_CMD_RECEIVE_UPDATE_VMSA = 0x052,
80 SEV_CMD_RECEIVE_FINISH = 0x053,
82 /* Guest debug commands */
83 SEV_CMD_DBG_DECRYPT = 0x060,
84 SEV_CMD_DBG_ENCRYPT = 0x061,
90 * struct sev_data_init - INIT command parameters
92 * @flags: processing flags
93 * @tmr_address: system physical address used for SEV-ES
94 * @tmr_len: len of tmr_address
96 struct sev_data_init {
98 u32 reserved; /* In */
99 u64 tmr_address; /* In */
100 u32 tmr_len; /* In */
103 #define SEV_INIT_FLAGS_SEV_ES 0x01
106 * struct sev_data_pek_csr - PEK_CSR command parameters
108 * @address: PEK certificate chain
109 * @len: len of certificate
111 struct sev_data_pek_csr {
112 u64 address; /* In */
113 u32 len; /* In/Out */
117 * struct sev_data_cert_import - PEK_CERT_IMPORT command parameters
119 * @pek_address: PEK certificate chain
120 * @pek_len: len of PEK certificate
121 * @oca_address: OCA certificate chain
122 * @oca_len: len of OCA certificate
124 struct sev_data_pek_cert_import {
125 u64 pek_cert_address; /* In */
126 u32 pek_cert_len; /* In */
127 u32 reserved; /* In */
128 u64 oca_cert_address; /* In */
129 u32 oca_cert_len; /* In */
133 * struct sev_data_download_firmware - DOWNLOAD_FIRMWARE command parameters
135 * @address: physical address of firmware image
136 * @len: len of the firmware image
138 struct sev_data_download_firmware {
139 u64 address; /* In */
144 * struct sev_data_get_id - GET_ID command parameters
146 * @address: physical address of region to place unique CPU ID(s)
147 * @len: len of the region
149 struct sev_data_get_id {
150 u64 address; /* In */
151 u32 len; /* In/Out */
154 * struct sev_data_pdh_cert_export - PDH_CERT_EXPORT command parameters
156 * @pdh_address: PDH certificate address
157 * @pdh_len: len of PDH certificate
158 * @cert_chain_address: PDH certificate chain
159 * @cert_chain_len: len of PDH certificate chain
161 struct sev_data_pdh_cert_export {
162 u64 pdh_cert_address; /* In */
163 u32 pdh_cert_len; /* In/Out */
164 u32 reserved; /* In */
165 u64 cert_chain_address; /* In */
166 u32 cert_chain_len; /* In/Out */
170 * struct sev_data_decommission - DECOMMISSION command parameters
172 * @handle: handle of the VM to decommission
174 struct sev_data_decommission {
179 * struct sev_data_activate - ACTIVATE command parameters
181 * @handle: handle of the VM to activate
182 * @asid: asid assigned to the VM
184 struct sev_data_activate {
190 * struct sev_data_deactivate - DEACTIVATE command parameters
192 * @handle: handle of the VM to deactivate
194 struct sev_data_deactivate {
199 * struct sev_data_guest_status - SEV GUEST_STATUS command parameters
201 * @handle: handle of the VM to retrieve status
202 * @policy: policy information for the VM
203 * @asid: current ASID of the VM
204 * @state: current state of the VM
206 struct sev_data_guest_status {
208 u32 policy; /* Out */
214 * struct sev_data_launch_start - LAUNCH_START command parameters
216 * @handle: handle assigned to the VM
217 * @policy: guest launch policy
218 * @dh_cert_address: physical address of DH certificate blob
219 * @dh_cert_len: len of DH certificate blob
220 * @session_address: physical address of session parameters
221 * @session_len: len of session parameters
223 struct sev_data_launch_start {
224 u32 handle; /* In/Out */
226 u64 dh_cert_address; /* In */
227 u32 dh_cert_len; /* In */
228 u32 reserved; /* In */
229 u64 session_address; /* In */
230 u32 session_len; /* In */
234 * struct sev_data_launch_update_data - LAUNCH_UPDATE_DATA command parameter
236 * @handle: handle of the VM to update
237 * @len: len of memory to be encrypted
238 * @address: physical address of memory region to encrypt
240 struct sev_data_launch_update_data {
243 u64 address; /* In */
248 * struct sev_data_launch_update_vmsa - LAUNCH_UPDATE_VMSA command
250 * @handle: handle of the VM
251 * @address: physical address of memory region to encrypt
252 * @len: len of memory region to encrypt
254 struct sev_data_launch_update_vmsa {
257 u64 address; /* In */
262 * struct sev_data_launch_measure - LAUNCH_MEASURE command parameters
264 * @handle: handle of the VM to process
265 * @address: physical address containing the measurement blob
266 * @len: len of measurement blob
268 struct sev_data_launch_measure {
271 u64 address; /* In */
272 u32 len; /* In/Out */
276 * struct sev_data_launch_secret - LAUNCH_SECRET command parameters
278 * @handle: handle of the VM to process
279 * @hdr_address: physical address containing the packet header
280 * @hdr_len: len of packet header
281 * @guest_address: system physical address of guest memory region
282 * @guest_len: len of guest_paddr
283 * @trans_address: physical address of transport memory buffer
284 * @trans_len: len of transport memory buffer
286 struct sev_data_launch_secret {
289 u64 hdr_address; /* In */
290 u32 hdr_len; /* In */
292 u64 guest_address; /* In */
293 u32 guest_len; /* In */
295 u64 trans_address; /* In */
296 u32 trans_len; /* In */
300 * struct sev_data_launch_finish - LAUNCH_FINISH command parameters
302 * @handle: handle of the VM to process
304 struct sev_data_launch_finish {
309 * struct sev_data_send_start - SEND_START command parameters
311 * @handle: handle of the VM to process
312 * @policy: policy information for the VM
313 * @pdh_cert_address: physical address containing PDH certificate
314 * @pdh_cert_len: len of PDH certificate
315 * @plat_certs_address: physical address containing platform certificate
316 * @plat_certs_len: len of platform certificate
317 * @amd_certs_address: physical address containing AMD certificate
318 * @amd_certs_len: len of AMD certificate
319 * @session_address: physical address containing Session data
320 * @session_len: len of session data
322 struct sev_data_send_start {
324 u32 policy; /* Out */
325 u64 pdh_cert_address; /* In */
326 u32 pdh_cert_len; /* In */
328 u64 plat_cert_address; /* In */
329 u32 plat_cert_len; /* In */
331 u64 amd_cert_address; /* In */
332 u32 amd_cert_len; /* In */
334 u64 session_address; /* In */
335 u32 session_len; /* In/Out */
339 * struct sev_data_send_update - SEND_UPDATE_DATA command
341 * @handle: handle of the VM to process
342 * @hdr_address: physical address containing packet header
343 * @hdr_len: len of packet header
344 * @guest_address: physical address of guest memory region to send
345 * @guest_len: len of guest memory region to send
346 * @trans_address: physical address of host memory region
347 * @trans_len: len of host memory region
349 struct sev_data_send_update_data {
352 u64 hdr_address; /* In */
353 u32 hdr_len; /* In/Out */
355 u64 guest_address; /* In */
356 u32 guest_len; /* In */
358 u64 trans_address; /* In */
359 u32 trans_len; /* In */
363 * struct sev_data_send_update - SEND_UPDATE_VMSA command
365 * @handle: handle of the VM to process
366 * @hdr_address: physical address containing packet header
367 * @hdr_len: len of packet header
368 * @guest_address: physical address of guest memory region to send
369 * @guest_len: len of guest memory region to send
370 * @trans_address: physical address of host memory region
371 * @trans_len: len of host memory region
373 struct sev_data_send_update_vmsa {
375 u64 hdr_address; /* In */
376 u32 hdr_len; /* In/Out */
378 u64 guest_address; /* In */
379 u32 guest_len; /* In */
381 u64 trans_address; /* In */
382 u32 trans_len; /* In */
386 * struct sev_data_send_finish - SEND_FINISH command parameters
388 * @handle: handle of the VM to process
390 struct sev_data_send_finish {
395 * struct sev_data_receive_start - RECEIVE_START command parameters
397 * @handle: handle of the VM to perform receive operation
398 * @pdh_cert_address: system physical address containing PDH certificate blob
399 * @pdh_cert_len: len of PDH certificate blob
400 * @session_address: system physical address containing session blob
401 * @session_len: len of session blob
403 struct sev_data_receive_start {
404 u32 handle; /* In/Out */
406 u64 pdh_cert_address; /* In */
407 u32 pdh_cert_len; /* In */
409 u64 session_address; /* In */
410 u32 session_len; /* In */
414 * struct sev_data_receive_update_data - RECEIVE_UPDATE_DATA command parameters
416 * @handle: handle of the VM to update
417 * @hdr_address: physical address containing packet header blob
418 * @hdr_len: len of packet header
419 * @guest_address: system physical address of guest memory region
420 * @guest_len: len of guest memory region
421 * @trans_address: system physical address of transport buffer
422 * @trans_len: len of transport buffer
424 struct sev_data_receive_update_data {
427 u64 hdr_address; /* In */
428 u32 hdr_len; /* In */
430 u64 guest_address; /* In */
431 u32 guest_len; /* In */
433 u64 trans_address; /* In */
434 u32 trans_len; /* In */
438 * struct sev_data_receive_update_vmsa - RECEIVE_UPDATE_VMSA command parameters
440 * @handle: handle of the VM to update
441 * @hdr_address: physical address containing packet header blob
442 * @hdr_len: len of packet header
443 * @guest_address: system physical address of guest memory region
444 * @guest_len: len of guest memory region
445 * @trans_address: system physical address of transport buffer
446 * @trans_len: len of transport buffer
448 struct sev_data_receive_update_vmsa {
451 u64 hdr_address; /* In */
452 u32 hdr_len; /* In */
454 u64 guest_address; /* In */
455 u32 guest_len; /* In */
457 u64 trans_address; /* In */
458 u32 trans_len; /* In */
462 * struct sev_data_receive_finish - RECEIVE_FINISH command parameters
464 * @handle: handle of the VM to finish
466 struct sev_data_receive_finish {
471 * struct sev_data_dbg - DBG_ENCRYPT/DBG_DECRYPT command parameters
473 * @handle: handle of the VM to perform debug operation
474 * @src_addr: source address of data to operate on
475 * @dst_addr: destination address of data to operate on
476 * @len: len of data to operate on
478 struct sev_data_dbg {
481 u64 src_addr; /* In */
482 u64 dst_addr; /* In */
486 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
489 * sev_platform_init - perform SEV INIT command
491 * @error: SEV command return code
494 * 0 if the SEV successfully processed the command
495 * -%ENODEV if the SEV device is not available
496 * -%ENOTSUPP if the SEV does not support SEV
497 * -%ETIMEDOUT if the SEV command timed out
498 * -%EIO if the SEV returned a non-zero return code
500 int sev_platform_init(int *error);
503 * sev_platform_status - perform SEV PLATFORM_STATUS command
505 * @status: sev_user_data_status structure to be processed
506 * @error: SEV command return code
509 * 0 if the SEV successfully processed the command
510 * -%ENODEV if the SEV device is not available
511 * -%ENOTSUPP if the SEV does not support SEV
512 * -%ETIMEDOUT if the SEV command timed out
513 * -%EIO if the SEV returned a non-zero return code
515 int sev_platform_status(struct sev_user_data_status *status, int *error);
518 * sev_issue_cmd_external_user - issue SEV command by other driver with a file
521 * This function can be used by other drivers to issue a SEV command on
522 * behalf of userspace. The caller must pass a valid SEV file descriptor
523 * so that we know that it has access to SEV device.
525 * @filep - SEV device file pointer
526 * @cmd - command to issue
527 * @data - command buffer
528 * @error: SEV command return code
531 * 0 if the SEV successfully processed the command
532 * -%ENODEV if the SEV device is not available
533 * -%ENOTSUPP if the SEV does not support SEV
534 * -%ETIMEDOUT if the SEV command timed out
535 * -%EIO if the SEV returned a non-zero return code
536 * -%EINVAL if the SEV file descriptor is not valid
538 int sev_issue_cmd_external_user(struct file *filep, unsigned int id,
539 void *data, int *error);
542 * sev_guest_deactivate - perform SEV DEACTIVATE command
544 * @deactivate: sev_data_deactivate structure to be processed
545 * @sev_ret: sev command return code
548 * 0 if the sev successfully processed the command
549 * -%ENODEV if the sev device is not available
550 * -%ENOTSUPP if the sev does not support SEV
551 * -%ETIMEDOUT if the sev command timed out
552 * -%EIO if the sev returned a non-zero return code
554 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error);
557 * sev_guest_activate - perform SEV ACTIVATE command
559 * @activate: sev_data_activate structure to be processed
560 * @sev_ret: sev command return code
563 * 0 if the sev successfully processed the command
564 * -%ENODEV if the sev device is not available
565 * -%ENOTSUPP if the sev does not support SEV
566 * -%ETIMEDOUT if the sev command timed out
567 * -%EIO if the sev returned a non-zero return code
569 int sev_guest_activate(struct sev_data_activate *data, int *error);
572 * sev_guest_df_flush - perform SEV DF_FLUSH command
574 * @sev_ret: sev command return code
577 * 0 if the sev successfully processed the command
578 * -%ENODEV if the sev device is not available
579 * -%ENOTSUPP if the sev does not support SEV
580 * -%ETIMEDOUT if the sev command timed out
581 * -%EIO if the sev returned a non-zero return code
583 int sev_guest_df_flush(int *error);
586 * sev_guest_decommission - perform SEV DECOMMISSION command
588 * @decommission: sev_data_decommission structure to be processed
589 * @sev_ret: sev command return code
592 * 0 if the sev successfully processed the command
593 * -%ENODEV if the sev device is not available
594 * -%ENOTSUPP if the sev does not support SEV
595 * -%ETIMEDOUT if the sev command timed out
596 * -%EIO if the sev returned a non-zero return code
598 int sev_guest_decommission(struct sev_data_decommission *data, int *error);
600 void *psp_copy_user_blob(u64 uaddr, u32 len);
602 #else /* !CONFIG_CRYPTO_DEV_SP_PSP */
605 sev_platform_status(struct sev_user_data_status *status, int *error) { return -ENODEV; }
607 static inline int sev_platform_init(int *error) { return -ENODEV; }
610 sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENODEV; }
613 sev_guest_decommission(struct sev_data_decommission *data, int *error) { return -ENODEV; }
616 sev_guest_activate(struct sev_data_activate *data, int *error) { return -ENODEV; }
618 static inline int sev_guest_df_flush(int *error) { return -ENODEV; }
621 sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int *error) { return -ENODEV; }
623 static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_PTR(-EINVAL); }
625 #endif /* CONFIG_CRYPTO_DEV_SP_PSP */
627 #endif /* __PSP_SEV_H__ */