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>
30 #define DEVICE_NAME "sev"
31 #define SEV_FW_FILE "/*(DEBLOBBED)*/"
32 #define SEV_FW_NAME_SIZE 64
34 static DEFINE_MUTEX(sev_cmd_mutex);
35 static struct sev_misc_dev *misc_dev;
37 static int psp_cmd_timeout = 100;
38 module_param(psp_cmd_timeout, int, 0644);
39 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
41 static int psp_probe_timeout = 5;
42 module_param(psp_probe_timeout, int, 0644);
43 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
45 /*(DEBLOBBED)*/ /* 1st gen EPYC */
46 /*(DEBLOBBED)*/ /* 2nd gen EPYC */
47 /*(DEBLOBBED)*/ /* 3rd gen EPYC */
50 static int psp_timeout;
52 /* Trusted Memory Region (TMR):
53 * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator
54 * to allocate the memory, which will return aligned memory for the specified
57 #define SEV_ES_TMR_SIZE (1024 * 1024)
58 static void *sev_es_tmr;
60 static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
62 struct sev_device *sev = psp_master->sev_data;
64 if (sev->api_major > maj)
67 if (sev->api_major == maj && sev->api_minor >= min)
73 static void sev_irq_handler(int irq, void *data, unsigned int status)
75 struct sev_device *sev = data;
78 /* Check if it is command completion: */
79 if (!(status & SEV_CMD_COMPLETE))
82 /* Check if it is SEV command completion: */
83 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
84 if (reg & PSP_CMDRESP_RESP) {
86 wake_up(&sev->int_queue);
90 static int sev_wait_cmd_ioc(struct sev_device *sev,
91 unsigned int *reg, unsigned int timeout)
95 ret = wait_event_timeout(sev->int_queue,
96 sev->int_rcvd, timeout * HZ);
100 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
105 static int sev_cmd_buffer_len(int cmd)
108 case SEV_CMD_INIT: return sizeof(struct sev_data_init);
109 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
110 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
111 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
112 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
113 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
114 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
115 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
116 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
117 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
118 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
119 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
120 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
121 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
122 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
123 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
124 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
125 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
126 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
127 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
128 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
129 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
130 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
131 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
132 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
133 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
134 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
141 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
143 struct psp_device *psp = psp_master;
144 struct sev_device *sev;
145 unsigned int phys_lsb, phys_msb;
146 unsigned int reg, ret = 0;
148 if (!psp || !psp->sev_data)
156 if (data && WARN_ON_ONCE(!virt_addr_valid(data)))
159 /* Get the physical address of the command buffer */
160 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
161 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
163 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
164 cmd, phys_msb, phys_lsb, psp_timeout);
166 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
167 sev_cmd_buffer_len(cmd), false);
169 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
170 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
175 reg <<= SEV_CMDRESP_CMD_SHIFT;
176 reg |= SEV_CMDRESP_IOC;
177 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
179 /* wait for command completion */
180 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout);
185 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
191 psp_timeout = psp_cmd_timeout;
194 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
196 if (reg & PSP_CMDRESP_ERR_MASK) {
197 dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n",
198 cmd, reg & PSP_CMDRESP_ERR_MASK);
202 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
203 sev_cmd_buffer_len(cmd), false);
208 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
212 mutex_lock(&sev_cmd_mutex);
213 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
214 mutex_unlock(&sev_cmd_mutex);
219 static int __sev_platform_init_locked(int *error)
221 struct psp_device *psp = psp_master;
222 struct sev_device *sev;
225 if (!psp || !psp->sev_data)
230 if (sev->state == SEV_STATE_INIT)
237 * Do not include the encryption mask on the physical
238 * address of the TMR (firmware should clear it anyway).
240 tmr_pa = __pa(sev_es_tmr);
242 sev->init_cmd_buf.flags |= SEV_INIT_FLAGS_SEV_ES;
243 sev->init_cmd_buf.tmr_address = tmr_pa;
244 sev->init_cmd_buf.tmr_len = SEV_ES_TMR_SIZE;
247 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error);
251 sev->state = SEV_STATE_INIT;
253 /* Prepare for first SEV guest launch after INIT */
254 wbinvd_on_all_cpus();
255 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
259 dev_dbg(sev->dev, "SEV firmware initialized\n");
264 int sev_platform_init(int *error)
268 mutex_lock(&sev_cmd_mutex);
269 rc = __sev_platform_init_locked(error);
270 mutex_unlock(&sev_cmd_mutex);
274 EXPORT_SYMBOL_GPL(sev_platform_init);
276 static int __sev_platform_shutdown_locked(int *error)
278 struct sev_device *sev = psp_master->sev_data;
281 if (!sev || sev->state == SEV_STATE_UNINIT)
284 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
288 sev->state = SEV_STATE_UNINIT;
289 dev_dbg(sev->dev, "SEV firmware shutdown\n");
294 static int sev_platform_shutdown(int *error)
298 mutex_lock(&sev_cmd_mutex);
299 rc = __sev_platform_shutdown_locked(NULL);
300 mutex_unlock(&sev_cmd_mutex);
305 static int sev_get_platform_state(int *state, int *error)
307 struct sev_device *sev = psp_master->sev_data;
310 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
311 &sev->status_cmd_buf, error);
315 *state = sev->status_cmd_buf.state;
319 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
327 * The SEV spec requires that FACTORY_RESET must be issued in
328 * UNINIT state. Before we go further lets check if any guest is
331 * If FW is in WORKING state then deny the request otherwise issue
332 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
335 rc = sev_get_platform_state(&state, &argp->error);
339 if (state == SEV_STATE_WORKING)
342 if (state == SEV_STATE_INIT) {
343 rc = __sev_platform_shutdown_locked(&argp->error);
348 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
351 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
353 struct sev_device *sev = psp_master->sev_data;
354 struct sev_user_data_status *data = &sev->status_cmd_buf;
357 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
361 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
367 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
369 struct sev_device *sev = psp_master->sev_data;
375 if (sev->state == SEV_STATE_UNINIT) {
376 rc = __sev_platform_init_locked(&argp->error);
381 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
384 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
386 struct sev_device *sev = psp_master->sev_data;
387 struct sev_user_data_pek_csr input;
388 struct sev_data_pek_csr *data;
389 void __user *input_address;
396 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
399 data = kzalloc(sizeof(*data), GFP_KERNEL);
403 /* userspace wants to query CSR length */
404 if (!input.address || !input.length)
407 /* allocate a physically contiguous buffer to store the CSR blob */
408 input_address = (void __user *)input.address;
409 if (input.length > SEV_FW_BLOB_MAX_SIZE) {
414 blob = kmalloc(input.length, GFP_KERNEL);
420 data->address = __psp_pa(blob);
421 data->len = input.length;
424 if (sev->state == SEV_STATE_UNINIT) {
425 ret = __sev_platform_init_locked(&argp->error);
430 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
432 /* If we query the CSR length, FW responded with expected data. */
433 input.length = data->len;
435 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
441 if (copy_to_user(input_address, blob, input.length))
452 void *psp_copy_user_blob(u64 uaddr, u32 len)
455 return ERR_PTR(-EINVAL);
457 /* verify that blob length does not exceed our limit */
458 if (len > SEV_FW_BLOB_MAX_SIZE)
459 return ERR_PTR(-EINVAL);
461 return memdup_user((void __user *)uaddr, len);
463 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
465 static int sev_get_api_version(void)
467 struct sev_device *sev = psp_master->sev_data;
468 struct sev_user_data_status *status;
471 status = &sev->status_cmd_buf;
472 ret = sev_platform_status(status, &error);
475 "SEV: failed to get status. Error: %#x\n", error);
479 sev->api_major = status->api_major;
480 sev->api_minor = status->api_minor;
481 sev->build = status->build;
482 sev->state = status->state;
487 static int sev_get_firmware(struct device *dev,
488 const struct firmware **firmware)
490 char fw_name_specific[SEV_FW_NAME_SIZE];
491 char fw_name_subset[SEV_FW_NAME_SIZE];
493 snprintf(fw_name_specific, sizeof(fw_name_specific),
494 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
495 boot_cpu_data.x86, boot_cpu_data.x86_model);
497 snprintf(fw_name_subset, sizeof(fw_name_subset),
498 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
499 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
501 /* Check for SEV FW for a particular model.
502 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
506 * Check for SEV FW common to a subset of models.
507 * Ex. amd_sev_fam17h_model0xh.sbin for
508 * Family 17h Model 00h -- Family 17h Model 0Fh
512 * Fall-back to using generic name: sev.fw
514 if ((firmware_reject_nowarn(firmware, fw_name_specific, dev) >= 0) ||
515 (firmware_reject_nowarn(firmware, fw_name_subset, dev) >= 0) ||
516 (firmware_reject_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
522 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
523 static int sev_update_firmware(struct device *dev)
525 struct sev_data_download_firmware *data;
526 const struct firmware *firmware;
527 int ret, error, order;
531 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
532 dev_dbg(dev, "No SEV firmware file present\n");
537 * SEV FW expects the physical address given to it to be 32
538 * byte aligned. Memory allocated has structure placed at the
539 * beginning followed by the firmware being passed to the SEV
540 * FW. Allocate enough memory for data structure + alignment
543 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
545 order = get_order(firmware->size + data_size);
546 p = alloc_pages(GFP_KERNEL, order);
553 * Copy firmware data to a kernel allocated contiguous
556 data = page_address(p);
557 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
559 data->address = __psp_pa(page_address(p) + data_size);
560 data->len = firmware->size;
562 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
564 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
566 dev_info(dev, "SEV firmware update successful\n");
568 __free_pages(p, order);
571 release_firmware(firmware);
576 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
578 struct sev_device *sev = psp_master->sev_data;
579 struct sev_user_data_pek_cert_import input;
580 struct sev_data_pek_cert_import *data;
581 void *pek_blob, *oca_blob;
587 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
590 data = kzalloc(sizeof(*data), GFP_KERNEL);
594 /* copy PEK certificate blobs from userspace */
595 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
596 if (IS_ERR(pek_blob)) {
597 ret = PTR_ERR(pek_blob);
601 data->pek_cert_address = __psp_pa(pek_blob);
602 data->pek_cert_len = input.pek_cert_len;
604 /* copy PEK certificate blobs from userspace */
605 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
606 if (IS_ERR(oca_blob)) {
607 ret = PTR_ERR(oca_blob);
611 data->oca_cert_address = __psp_pa(oca_blob);
612 data->oca_cert_len = input.oca_cert_len;
614 /* If platform is not in INIT state then transition it to INIT */
615 if (sev->state != SEV_STATE_INIT) {
616 ret = __sev_platform_init_locked(&argp->error);
621 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
632 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
634 struct sev_user_data_get_id2 input;
635 struct sev_data_get_id *data;
636 void __user *input_address;
637 void *id_blob = NULL;
640 /* SEV GET_ID is available from SEV API v0.16 and up */
641 if (!sev_version_greater_or_equal(0, 16))
644 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
647 input_address = (void __user *)input.address;
649 data = kzalloc(sizeof(*data), GFP_KERNEL);
653 if (input.address && input.length) {
654 id_blob = kmalloc(input.length, GFP_KERNEL);
660 data->address = __psp_pa(id_blob);
661 data->len = input.length;
664 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
667 * Firmware will return the length of the ID value (either the minimum
668 * required length or the actual length written), return it to the user.
670 input.length = data->len;
672 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
678 if (copy_to_user(input_address, id_blob, data->len)) {
691 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
693 struct sev_data_get_id *data;
694 u64 data_size, user_size;
698 /* SEV GET_ID available from SEV API v0.16 and up */
699 if (!sev_version_greater_or_equal(0, 16))
702 /* SEV FW expects the buffer it fills with the ID to be
703 * 8-byte aligned. Memory allocated should be enough to
704 * hold data structure + alignment padding + memory
705 * where SEV FW writes the ID.
707 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
708 user_size = sizeof(struct sev_user_data_get_id);
710 mem = kzalloc(data_size + user_size, GFP_KERNEL);
715 id_blob = mem + data_size;
717 data->address = __psp_pa(id_blob);
718 data->len = user_size;
720 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
722 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
731 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
733 struct sev_device *sev = psp_master->sev_data;
734 struct sev_user_data_pdh_cert_export input;
735 void *pdh_blob = NULL, *cert_blob = NULL;
736 struct sev_data_pdh_cert_export *data;
737 void __user *input_cert_chain_address;
738 void __user *input_pdh_cert_address;
741 /* If platform is not in INIT state then transition it to INIT. */
742 if (sev->state != SEV_STATE_INIT) {
746 ret = __sev_platform_init_locked(&argp->error);
751 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
754 data = kzalloc(sizeof(*data), GFP_KERNEL);
758 /* Userspace wants to query the certificate length. */
759 if (!input.pdh_cert_address ||
760 !input.pdh_cert_len ||
761 !input.cert_chain_address)
764 input_pdh_cert_address = (void __user *)input.pdh_cert_address;
765 input_cert_chain_address = (void __user *)input.cert_chain_address;
767 /* Allocate a physically contiguous buffer to store the PDH blob. */
768 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) {
773 /* Allocate a physically contiguous buffer to store the cert chain blob. */
774 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) {
779 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
785 data->pdh_cert_address = __psp_pa(pdh_blob);
786 data->pdh_cert_len = input.pdh_cert_len;
788 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
794 data->cert_chain_address = __psp_pa(cert_blob);
795 data->cert_chain_len = input.cert_chain_len;
798 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
800 /* If we query the length, FW responded with expected data. */
801 input.cert_chain_len = data->cert_chain_len;
802 input.pdh_cert_len = data->pdh_cert_len;
804 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
810 if (copy_to_user(input_pdh_cert_address,
811 pdh_blob, input.pdh_cert_len)) {
818 if (copy_to_user(input_cert_chain_address,
819 cert_blob, input.cert_chain_len))
832 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
834 void __user *argp = (void __user *)arg;
835 struct sev_issue_cmd input;
837 bool writable = file->f_mode & FMODE_WRITE;
839 if (!psp_master || !psp_master->sev_data)
842 if (ioctl != SEV_ISSUE_CMD)
845 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
848 if (input.cmd > SEV_MAX)
851 mutex_lock(&sev_cmd_mutex);
855 case SEV_FACTORY_RESET:
856 ret = sev_ioctl_do_reset(&input, writable);
858 case SEV_PLATFORM_STATUS:
859 ret = sev_ioctl_do_platform_status(&input);
862 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
865 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
868 ret = sev_ioctl_do_pek_csr(&input, writable);
870 case SEV_PEK_CERT_IMPORT:
871 ret = sev_ioctl_do_pek_import(&input, writable);
873 case SEV_PDH_CERT_EXPORT:
874 ret = sev_ioctl_do_pdh_export(&input, writable);
877 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
878 ret = sev_ioctl_do_get_id(&input);
881 ret = sev_ioctl_do_get_id2(&input);
888 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
891 mutex_unlock(&sev_cmd_mutex);
896 static const struct file_operations sev_fops = {
897 .owner = THIS_MODULE,
898 .unlocked_ioctl = sev_ioctl,
901 int sev_platform_status(struct sev_user_data_status *data, int *error)
903 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
905 EXPORT_SYMBOL_GPL(sev_platform_status);
907 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
909 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
911 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
913 int sev_guest_activate(struct sev_data_activate *data, int *error)
915 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
917 EXPORT_SYMBOL_GPL(sev_guest_activate);
919 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
921 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
923 EXPORT_SYMBOL_GPL(sev_guest_decommission);
925 int sev_guest_df_flush(int *error)
927 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
929 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
931 static void sev_exit(struct kref *ref)
933 misc_deregister(&misc_dev->misc);
938 static int sev_misc_init(struct sev_device *sev)
940 struct device *dev = sev->dev;
944 * SEV feature support can be detected on multiple devices but the SEV
945 * FW commands must be issued on the master. During probe, we do not
946 * know the master hence we create /dev/sev on the first device probe.
947 * sev_do_cmd() finds the right master device to which to issue the
948 * command to the firmware.
951 struct miscdevice *misc;
953 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
957 misc = &misc_dev->misc;
958 misc->minor = MISC_DYNAMIC_MINOR;
959 misc->name = DEVICE_NAME;
960 misc->fops = &sev_fops;
962 ret = misc_register(misc);
966 kref_init(&misc_dev->refcount);
968 kref_get(&misc_dev->refcount);
971 init_waitqueue_head(&sev->int_queue);
972 sev->misc = misc_dev;
973 dev_dbg(dev, "registered SEV device\n");
978 int sev_dev_init(struct psp_device *psp)
980 struct device *dev = psp->dev;
981 struct sev_device *sev;
984 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
993 sev->io_regs = psp->io_regs;
995 sev->vdata = (struct sev_vdata *)psp->vdata->sev;
998 dev_err(dev, "sev: missing driver data\n");
1002 psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
1004 ret = sev_misc_init(sev);
1008 dev_notice(dev, "sev enabled\n");
1013 psp_clear_sev_irq_handler(psp);
1015 devm_kfree(dev, sev);
1017 psp->sev_data = NULL;
1019 dev_notice(dev, "sev initialization failed\n");
1024 static void sev_firmware_shutdown(struct sev_device *sev)
1026 sev_platform_shutdown(NULL);
1029 /* The TMR area was encrypted, flush it from the cache */
1030 wbinvd_on_all_cpus();
1032 free_pages((unsigned long)sev_es_tmr,
1033 get_order(SEV_ES_TMR_SIZE));
1038 void sev_dev_destroy(struct psp_device *psp)
1040 struct sev_device *sev = psp->sev_data;
1045 sev_firmware_shutdown(sev);
1048 kref_put(&misc_dev->refcount, sev_exit);
1050 psp_clear_sev_irq_handler(psp);
1053 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1054 void *data, int *error)
1056 if (!filep || filep->f_op != &sev_fops)
1059 return sev_do_cmd(cmd, data, error);
1061 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1063 void sev_pci_init(void)
1065 struct sev_device *sev = psp_master->sev_data;
1066 struct page *tmr_page;
1072 psp_timeout = psp_probe_timeout;
1074 if (sev_get_api_version())
1077 if (sev_version_greater_or_equal(0, 15) &&
1078 sev_update_firmware(sev->dev) == 0)
1079 sev_get_api_version();
1081 /* Obtain the TMR memory area for SEV-ES use */
1082 tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE));
1084 sev_es_tmr = page_address(tmr_page);
1088 "SEV: TMR allocation failed, SEV-ES support unavailable\n");
1091 /* Initialize the platform */
1092 rc = sev_platform_init(&error);
1093 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) {
1095 * INIT command returned an integrity check failure
1096 * status code, meaning that firmware load and
1097 * validation of SEV related persistent data has
1098 * failed and persistent state has been erased.
1099 * Retrying INIT command here should succeed.
1101 dev_dbg(sev->dev, "SEV: retrying INIT command");
1102 rc = sev_platform_init(&error);
1106 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error);
1110 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
1111 sev->api_minor, sev->build);
1116 psp_master->sev_data = NULL;
1119 void sev_pci_exit(void)
1121 struct sev_device *sev = psp_master->sev_data;
1126 sev_firmware_shutdown(sev);