1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Secure Encrypted Virtualization (SEV) interface
5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
7 * Author: Brijesh Singh <brijesh.singh@amd.com>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/kthread.h>
13 #include <linux/sched.h>
14 #include <linux/interrupt.h>
15 #include <linux/spinlock.h>
16 #include <linux/spinlock_types.h>
17 #include <linux/types.h>
18 #include <linux/mutex.h>
19 #include <linux/delay.h>
20 #include <linux/hw_random.h>
21 #include <linux/ccp.h>
22 #include <linux/firmware.h>
23 #include <linux/gfp.h>
24 #include <linux/cpufeature.h>
31 #define DEVICE_NAME "sev"
32 #define SEV_FW_FILE "amd/sev.fw"
33 #define SEV_FW_NAME_SIZE 64
35 static DEFINE_MUTEX(sev_cmd_mutex);
36 static struct sev_misc_dev *misc_dev;
38 static int psp_cmd_timeout = 100;
39 module_param(psp_cmd_timeout, int, 0644);
40 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
42 static int psp_probe_timeout = 5;
43 module_param(psp_probe_timeout, int, 0644);
44 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
47 static int psp_timeout;
49 /* Trusted Memory Region (TMR):
50 * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator
51 * to allocate the memory, which will return aligned memory for the specified
54 #define SEV_ES_TMR_SIZE (1024 * 1024)
55 static void *sev_es_tmr;
57 static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
59 struct sev_device *sev = psp_master->sev_data;
61 if (sev->api_major > maj)
64 if (sev->api_major == maj && sev->api_minor >= min)
70 static void sev_irq_handler(int irq, void *data, unsigned int status)
72 struct sev_device *sev = data;
75 /* Check if it is command completion: */
76 if (!(status & SEV_CMD_COMPLETE))
79 /* Check if it is SEV command completion: */
80 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
81 if (reg & PSP_CMDRESP_RESP) {
83 wake_up(&sev->int_queue);
87 static int sev_wait_cmd_ioc(struct sev_device *sev,
88 unsigned int *reg, unsigned int timeout)
92 ret = wait_event_timeout(sev->int_queue,
93 sev->int_rcvd, timeout * HZ);
97 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
102 static int sev_cmd_buffer_len(int cmd)
105 case SEV_CMD_INIT: return sizeof(struct sev_data_init);
106 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
107 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
108 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
109 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
110 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
111 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
112 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
113 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
114 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
115 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
116 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
117 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
118 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
119 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
120 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
121 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
122 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
123 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
124 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
125 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
126 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
127 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
128 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
129 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
130 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
131 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
132 case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report);
139 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
141 struct psp_device *psp = psp_master;
142 struct sev_device *sev;
143 unsigned int phys_lsb, phys_msb;
144 unsigned int reg, ret = 0;
146 if (!psp || !psp->sev_data)
154 /* Get the physical address of the command buffer */
155 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
156 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
158 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
159 cmd, phys_msb, phys_lsb, psp_timeout);
161 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
162 sev_cmd_buffer_len(cmd), false);
164 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
165 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
170 reg <<= SEV_CMDRESP_CMD_SHIFT;
171 reg |= SEV_CMDRESP_IOC;
172 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
174 /* wait for command completion */
175 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout);
180 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
186 psp_timeout = psp_cmd_timeout;
189 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
191 if (reg & PSP_CMDRESP_ERR_MASK) {
192 dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n",
193 cmd, reg & PSP_CMDRESP_ERR_MASK);
197 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
198 sev_cmd_buffer_len(cmd), false);
203 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
207 mutex_lock(&sev_cmd_mutex);
208 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
209 mutex_unlock(&sev_cmd_mutex);
214 static int __sev_platform_init_locked(int *error)
216 struct psp_device *psp = psp_master;
217 struct sev_device *sev;
220 if (!psp || !psp->sev_data)
225 if (sev->state == SEV_STATE_INIT)
232 * Do not include the encryption mask on the physical
233 * address of the TMR (firmware should clear it anyway).
235 tmr_pa = __pa(sev_es_tmr);
237 sev->init_cmd_buf.flags |= SEV_INIT_FLAGS_SEV_ES;
238 sev->init_cmd_buf.tmr_address = tmr_pa;
239 sev->init_cmd_buf.tmr_len = SEV_ES_TMR_SIZE;
242 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error);
246 sev->state = SEV_STATE_INIT;
248 /* Prepare for first SEV guest launch after INIT */
249 wbinvd_on_all_cpus();
250 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
254 dev_dbg(sev->dev, "SEV firmware initialized\n");
259 int sev_platform_init(int *error)
263 mutex_lock(&sev_cmd_mutex);
264 rc = __sev_platform_init_locked(error);
265 mutex_unlock(&sev_cmd_mutex);
269 EXPORT_SYMBOL_GPL(sev_platform_init);
271 static int __sev_platform_shutdown_locked(int *error)
273 struct sev_device *sev = psp_master->sev_data;
276 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
280 sev->state = SEV_STATE_UNINIT;
281 dev_dbg(sev->dev, "SEV firmware shutdown\n");
286 static int sev_platform_shutdown(int *error)
290 mutex_lock(&sev_cmd_mutex);
291 rc = __sev_platform_shutdown_locked(NULL);
292 mutex_unlock(&sev_cmd_mutex);
297 static int sev_get_platform_state(int *state, int *error)
299 struct sev_device *sev = psp_master->sev_data;
302 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
303 &sev->status_cmd_buf, error);
307 *state = sev->status_cmd_buf.state;
311 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
319 * The SEV spec requires that FACTORY_RESET must be issued in
320 * UNINIT state. Before we go further lets check if any guest is
323 * If FW is in WORKING state then deny the request otherwise issue
324 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
327 rc = sev_get_platform_state(&state, &argp->error);
331 if (state == SEV_STATE_WORKING)
334 if (state == SEV_STATE_INIT) {
335 rc = __sev_platform_shutdown_locked(&argp->error);
340 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
343 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
345 struct sev_device *sev = psp_master->sev_data;
346 struct sev_user_data_status *data = &sev->status_cmd_buf;
349 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
353 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
359 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
361 struct sev_device *sev = psp_master->sev_data;
367 if (sev->state == SEV_STATE_UNINIT) {
368 rc = __sev_platform_init_locked(&argp->error);
373 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
376 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
378 struct sev_device *sev = psp_master->sev_data;
379 struct sev_user_data_pek_csr input;
380 struct sev_data_pek_csr *data;
381 void __user *input_address;
388 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
391 data = kzalloc(sizeof(*data), GFP_KERNEL);
395 /* userspace wants to query CSR length */
396 if (!input.address || !input.length)
399 /* allocate a physically contiguous buffer to store the CSR blob */
400 input_address = (void __user *)input.address;
401 if (input.length > SEV_FW_BLOB_MAX_SIZE) {
406 blob = kmalloc(input.length, GFP_KERNEL);
412 data->address = __psp_pa(blob);
413 data->len = input.length;
416 if (sev->state == SEV_STATE_UNINIT) {
417 ret = __sev_platform_init_locked(&argp->error);
422 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
424 /* If we query the CSR length, FW responded with expected data. */
425 input.length = data->len;
427 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
433 if (copy_to_user(input_address, blob, input.length))
444 void *psp_copy_user_blob(u64 uaddr, u32 len)
447 return ERR_PTR(-EINVAL);
449 /* verify that blob length does not exceed our limit */
450 if (len > SEV_FW_BLOB_MAX_SIZE)
451 return ERR_PTR(-EINVAL);
453 return memdup_user((void __user *)uaddr, len);
455 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
457 static int sev_get_api_version(void)
459 struct sev_device *sev = psp_master->sev_data;
460 struct sev_user_data_status *status;
463 status = &sev->status_cmd_buf;
464 ret = sev_platform_status(status, &error);
467 "SEV: failed to get status. Error: %#x\n", error);
471 sev->api_major = status->api_major;
472 sev->api_minor = status->api_minor;
473 sev->build = status->build;
474 sev->state = status->state;
479 static int sev_get_firmware(struct device *dev,
480 const struct firmware **firmware)
482 char fw_name_specific[SEV_FW_NAME_SIZE];
483 char fw_name_subset[SEV_FW_NAME_SIZE];
485 snprintf(fw_name_specific, sizeof(fw_name_specific),
486 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
487 boot_cpu_data.x86, boot_cpu_data.x86_model);
489 snprintf(fw_name_subset, sizeof(fw_name_subset),
490 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
491 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
493 /* Check for SEV FW for a particular model.
494 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
498 * Check for SEV FW common to a subset of models.
499 * Ex. amd_sev_fam17h_model0xh.sbin for
500 * Family 17h Model 00h -- Family 17h Model 0Fh
504 * Fall-back to using generic name: sev.fw
506 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
507 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
508 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
514 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
515 static int sev_update_firmware(struct device *dev)
517 struct sev_data_download_firmware *data;
518 const struct firmware *firmware;
519 int ret, error, order;
523 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
524 dev_dbg(dev, "No SEV firmware file present\n");
529 * SEV FW expects the physical address given to it to be 32
530 * byte aligned. Memory allocated has structure placed at the
531 * beginning followed by the firmware being passed to the SEV
532 * FW. Allocate enough memory for data structure + alignment
535 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
537 order = get_order(firmware->size + data_size);
538 p = alloc_pages(GFP_KERNEL, order);
545 * Copy firmware data to a kernel allocated contiguous
548 data = page_address(p);
549 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
551 data->address = __psp_pa(page_address(p) + data_size);
552 data->len = firmware->size;
554 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
556 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
558 dev_info(dev, "SEV firmware update successful\n");
560 __free_pages(p, order);
563 release_firmware(firmware);
568 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
570 struct sev_device *sev = psp_master->sev_data;
571 struct sev_user_data_pek_cert_import input;
572 struct sev_data_pek_cert_import *data;
573 void *pek_blob, *oca_blob;
579 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
582 data = kzalloc(sizeof(*data), GFP_KERNEL);
586 /* copy PEK certificate blobs from userspace */
587 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
588 if (IS_ERR(pek_blob)) {
589 ret = PTR_ERR(pek_blob);
593 data->pek_cert_address = __psp_pa(pek_blob);
594 data->pek_cert_len = input.pek_cert_len;
596 /* copy PEK certificate blobs from userspace */
597 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
598 if (IS_ERR(oca_blob)) {
599 ret = PTR_ERR(oca_blob);
603 data->oca_cert_address = __psp_pa(oca_blob);
604 data->oca_cert_len = input.oca_cert_len;
606 /* If platform is not in INIT state then transition it to INIT */
607 if (sev->state != SEV_STATE_INIT) {
608 ret = __sev_platform_init_locked(&argp->error);
613 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
624 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
626 struct sev_user_data_get_id2 input;
627 struct sev_data_get_id *data;
628 void __user *input_address;
629 void *id_blob = NULL;
632 /* SEV GET_ID is available from SEV API v0.16 and up */
633 if (!sev_version_greater_or_equal(0, 16))
636 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
639 input_address = (void __user *)input.address;
641 data = kzalloc(sizeof(*data), GFP_KERNEL);
645 if (input.address && input.length) {
646 id_blob = kmalloc(input.length, GFP_KERNEL);
652 data->address = __psp_pa(id_blob);
653 data->len = input.length;
656 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
659 * Firmware will return the length of the ID value (either the minimum
660 * required length or the actual length written), return it to the user.
662 input.length = data->len;
664 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
670 if (copy_to_user(input_address, id_blob, data->len)) {
683 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
685 struct sev_data_get_id *data;
686 u64 data_size, user_size;
690 /* SEV GET_ID available from SEV API v0.16 and up */
691 if (!sev_version_greater_or_equal(0, 16))
694 /* SEV FW expects the buffer it fills with the ID to be
695 * 8-byte aligned. Memory allocated should be enough to
696 * hold data structure + alignment padding + memory
697 * where SEV FW writes the ID.
699 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
700 user_size = sizeof(struct sev_user_data_get_id);
702 mem = kzalloc(data_size + user_size, GFP_KERNEL);
707 id_blob = mem + data_size;
709 data->address = __psp_pa(id_blob);
710 data->len = user_size;
712 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
714 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
723 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
725 struct sev_device *sev = psp_master->sev_data;
726 struct sev_user_data_pdh_cert_export input;
727 void *pdh_blob = NULL, *cert_blob = NULL;
728 struct sev_data_pdh_cert_export *data;
729 void __user *input_cert_chain_address;
730 void __user *input_pdh_cert_address;
733 /* If platform is not in INIT state then transition it to INIT. */
734 if (sev->state != SEV_STATE_INIT) {
738 ret = __sev_platform_init_locked(&argp->error);
743 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
746 data = kzalloc(sizeof(*data), GFP_KERNEL);
750 /* Userspace wants to query the certificate length. */
751 if (!input.pdh_cert_address ||
752 !input.pdh_cert_len ||
753 !input.cert_chain_address)
756 input_pdh_cert_address = (void __user *)input.pdh_cert_address;
757 input_cert_chain_address = (void __user *)input.cert_chain_address;
759 /* Allocate a physically contiguous buffer to store the PDH blob. */
760 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) {
765 /* Allocate a physically contiguous buffer to store the cert chain blob. */
766 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) {
771 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
777 data->pdh_cert_address = __psp_pa(pdh_blob);
778 data->pdh_cert_len = input.pdh_cert_len;
780 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
786 data->cert_chain_address = __psp_pa(cert_blob);
787 data->cert_chain_len = input.cert_chain_len;
790 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
792 /* If we query the length, FW responded with expected data. */
793 input.cert_chain_len = data->cert_chain_len;
794 input.pdh_cert_len = data->pdh_cert_len;
796 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
802 if (copy_to_user(input_pdh_cert_address,
803 pdh_blob, input.pdh_cert_len)) {
810 if (copy_to_user(input_cert_chain_address,
811 cert_blob, input.cert_chain_len))
824 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
826 void __user *argp = (void __user *)arg;
827 struct sev_issue_cmd input;
829 bool writable = file->f_mode & FMODE_WRITE;
831 if (!psp_master || !psp_master->sev_data)
834 if (ioctl != SEV_ISSUE_CMD)
837 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
840 if (input.cmd > SEV_MAX)
843 mutex_lock(&sev_cmd_mutex);
847 case SEV_FACTORY_RESET:
848 ret = sev_ioctl_do_reset(&input, writable);
850 case SEV_PLATFORM_STATUS:
851 ret = sev_ioctl_do_platform_status(&input);
854 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
857 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
860 ret = sev_ioctl_do_pek_csr(&input, writable);
862 case SEV_PEK_CERT_IMPORT:
863 ret = sev_ioctl_do_pek_import(&input, writable);
865 case SEV_PDH_CERT_EXPORT:
866 ret = sev_ioctl_do_pdh_export(&input, writable);
869 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
870 ret = sev_ioctl_do_get_id(&input);
873 ret = sev_ioctl_do_get_id2(&input);
880 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
883 mutex_unlock(&sev_cmd_mutex);
888 static const struct file_operations sev_fops = {
889 .owner = THIS_MODULE,
890 .unlocked_ioctl = sev_ioctl,
893 int sev_platform_status(struct sev_user_data_status *data, int *error)
895 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
897 EXPORT_SYMBOL_GPL(sev_platform_status);
899 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
901 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
903 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
905 int sev_guest_activate(struct sev_data_activate *data, int *error)
907 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
909 EXPORT_SYMBOL_GPL(sev_guest_activate);
911 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
913 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
915 EXPORT_SYMBOL_GPL(sev_guest_decommission);
917 int sev_guest_df_flush(int *error)
919 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
921 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
923 static void sev_exit(struct kref *ref)
925 misc_deregister(&misc_dev->misc);
930 static int sev_misc_init(struct sev_device *sev)
932 struct device *dev = sev->dev;
936 * SEV feature support can be detected on multiple devices but the SEV
937 * FW commands must be issued on the master. During probe, we do not
938 * know the master hence we create /dev/sev on the first device probe.
939 * sev_do_cmd() finds the right master device to which to issue the
940 * command to the firmware.
943 struct miscdevice *misc;
945 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
949 misc = &misc_dev->misc;
950 misc->minor = MISC_DYNAMIC_MINOR;
951 misc->name = DEVICE_NAME;
952 misc->fops = &sev_fops;
954 ret = misc_register(misc);
958 kref_init(&misc_dev->refcount);
960 kref_get(&misc_dev->refcount);
963 init_waitqueue_head(&sev->int_queue);
964 sev->misc = misc_dev;
965 dev_dbg(dev, "registered SEV device\n");
970 int sev_dev_init(struct psp_device *psp)
972 struct device *dev = psp->dev;
973 struct sev_device *sev;
976 if (!boot_cpu_has(X86_FEATURE_SEV)) {
977 dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n");
981 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
990 sev->io_regs = psp->io_regs;
992 sev->vdata = (struct sev_vdata *)psp->vdata->sev;
995 dev_err(dev, "sev: missing driver data\n");
999 psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
1001 ret = sev_misc_init(sev);
1005 dev_notice(dev, "sev enabled\n");
1010 psp_clear_sev_irq_handler(psp);
1012 psp->sev_data = NULL;
1014 dev_notice(dev, "sev initialization failed\n");
1019 void sev_dev_destroy(struct psp_device *psp)
1021 struct sev_device *sev = psp->sev_data;
1027 kref_put(&misc_dev->refcount, sev_exit);
1029 psp_clear_sev_irq_handler(psp);
1032 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1033 void *data, int *error)
1035 if (!filep || filep->f_op != &sev_fops)
1038 return sev_do_cmd(cmd, data, error);
1040 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1042 void sev_pci_init(void)
1044 struct sev_device *sev = psp_master->sev_data;
1045 struct page *tmr_page;
1051 psp_timeout = psp_probe_timeout;
1053 if (sev_get_api_version())
1057 * If platform is not in UNINIT state then firmware upgrade and/or
1058 * platform INIT command will fail. These command require UNINIT state.
1060 * In a normal boot we should never run into case where the firmware
1061 * is not in UNINIT state on boot. But in case of kexec boot, a reboot
1062 * may not go through a typical shutdown sequence and may leave the
1063 * firmware in INIT or WORKING state.
1066 if (sev->state != SEV_STATE_UNINIT) {
1067 sev_platform_shutdown(NULL);
1068 sev->state = SEV_STATE_UNINIT;
1071 if (sev_version_greater_or_equal(0, 15) &&
1072 sev_update_firmware(sev->dev) == 0)
1073 sev_get_api_version();
1075 /* Obtain the TMR memory area for SEV-ES use */
1076 tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE));
1078 sev_es_tmr = page_address(tmr_page);
1082 "SEV: TMR allocation failed, SEV-ES support unavailable\n");
1085 /* Initialize the platform */
1086 rc = sev_platform_init(&error);
1087 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) {
1089 * INIT command returned an integrity check failure
1090 * status code, meaning that firmware load and
1091 * validation of SEV related persistent data has
1092 * failed and persistent state has been erased.
1093 * Retrying INIT command here should succeed.
1095 dev_dbg(sev->dev, "SEV: retrying INIT command");
1096 rc = sev_platform_init(&error);
1100 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error);
1104 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
1105 sev->api_minor, sev->build);
1110 psp_master->sev_data = NULL;
1113 void sev_pci_exit(void)
1115 if (!psp_master->sev_data)
1118 sev_platform_shutdown(NULL);
1121 /* The TMR area was encrypted, flush it from the cache */
1122 wbinvd_on_all_cpus();
1124 free_pages((unsigned long)sev_es_tmr,
1125 get_order(SEV_ES_TMR_SIZE));