1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Secure Encrypted Virtualization (SEV) interface
5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
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>
29 #define DEVICE_NAME "sev"
30 #define SEV_FW_FILE "amd/sev.fw"
31 #define SEV_FW_NAME_SIZE 64
33 static DEFINE_MUTEX(sev_cmd_mutex);
34 static struct sev_misc_dev *misc_dev;
36 static int psp_cmd_timeout = 100;
37 module_param(psp_cmd_timeout, int, 0644);
38 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
40 static int psp_probe_timeout = 5;
41 module_param(psp_probe_timeout, int, 0644);
42 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
45 static int psp_timeout;
47 static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
49 struct sev_device *sev = psp_master->sev_data;
51 if (sev->api_major > maj)
54 if (sev->api_major == maj && sev->api_minor >= min)
60 static void sev_irq_handler(int irq, void *data, unsigned int status)
62 struct sev_device *sev = data;
65 /* Check if it is command completion: */
66 if (!(status & SEV_CMD_COMPLETE))
69 /* Check if it is SEV command completion: */
70 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
71 if (reg & PSP_CMDRESP_RESP) {
73 wake_up(&sev->int_queue);
77 static int sev_wait_cmd_ioc(struct sev_device *sev,
78 unsigned int *reg, unsigned int timeout)
82 ret = wait_event_timeout(sev->int_queue,
83 sev->int_rcvd, timeout * HZ);
87 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
92 static int sev_cmd_buffer_len(int cmd)
95 case SEV_CMD_INIT: return sizeof(struct sev_data_init);
96 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
97 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
98 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
99 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
100 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
101 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
102 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
103 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
104 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
105 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
106 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
107 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
108 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
109 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
110 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
111 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
112 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
113 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
114 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
115 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
116 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
117 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
118 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
119 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
120 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
121 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
128 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
130 struct psp_device *psp = psp_master;
131 struct sev_device *sev;
132 unsigned int phys_lsb, phys_msb;
133 unsigned int reg, ret = 0;
135 if (!psp || !psp->sev_data)
143 /* Get the physical address of the command buffer */
144 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
145 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
147 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
148 cmd, phys_msb, phys_lsb, psp_timeout);
150 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
151 sev_cmd_buffer_len(cmd), false);
153 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
154 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
159 reg <<= SEV_CMDRESP_CMD_SHIFT;
160 reg |= SEV_CMDRESP_IOC;
161 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
163 /* wait for command completion */
164 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout);
169 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
175 psp_timeout = psp_cmd_timeout;
178 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
180 if (reg & PSP_CMDRESP_ERR_MASK) {
181 dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n",
182 cmd, reg & PSP_CMDRESP_ERR_MASK);
186 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
187 sev_cmd_buffer_len(cmd), false);
192 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
196 mutex_lock(&sev_cmd_mutex);
197 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
198 mutex_unlock(&sev_cmd_mutex);
203 static int __sev_platform_init_locked(int *error)
205 struct psp_device *psp = psp_master;
206 struct sev_device *sev;
209 if (!psp || !psp->sev_data)
214 if (sev->state == SEV_STATE_INIT)
217 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error);
221 sev->state = SEV_STATE_INIT;
223 /* Prepare for first SEV guest launch after INIT */
224 wbinvd_on_all_cpus();
225 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
229 dev_dbg(sev->dev, "SEV firmware initialized\n");
234 int sev_platform_init(int *error)
238 mutex_lock(&sev_cmd_mutex);
239 rc = __sev_platform_init_locked(error);
240 mutex_unlock(&sev_cmd_mutex);
244 EXPORT_SYMBOL_GPL(sev_platform_init);
246 static int __sev_platform_shutdown_locked(int *error)
248 struct sev_device *sev = psp_master->sev_data;
251 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
255 sev->state = SEV_STATE_UNINIT;
256 dev_dbg(sev->dev, "SEV firmware shutdown\n");
261 static int sev_platform_shutdown(int *error)
265 mutex_lock(&sev_cmd_mutex);
266 rc = __sev_platform_shutdown_locked(NULL);
267 mutex_unlock(&sev_cmd_mutex);
272 static int sev_get_platform_state(int *state, int *error)
274 struct sev_device *sev = psp_master->sev_data;
277 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
278 &sev->status_cmd_buf, error);
282 *state = sev->status_cmd_buf.state;
286 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp)
290 if (!capable(CAP_SYS_ADMIN))
294 * The SEV spec requires that FACTORY_RESET must be issued in
295 * UNINIT state. Before we go further lets check if any guest is
298 * If FW is in WORKING state then deny the request otherwise issue
299 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
302 rc = sev_get_platform_state(&state, &argp->error);
306 if (state == SEV_STATE_WORKING)
309 if (state == SEV_STATE_INIT) {
310 rc = __sev_platform_shutdown_locked(&argp->error);
315 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
318 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
320 struct sev_device *sev = psp_master->sev_data;
321 struct sev_user_data_status *data = &sev->status_cmd_buf;
324 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
328 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
334 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)
336 struct sev_device *sev = psp_master->sev_data;
339 if (!capable(CAP_SYS_ADMIN))
342 if (sev->state == SEV_STATE_UNINIT) {
343 rc = __sev_platform_init_locked(&argp->error);
348 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
351 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)
353 struct sev_device *sev = psp_master->sev_data;
354 struct sev_user_data_pek_csr input;
355 struct sev_data_pek_csr *data;
359 if (!capable(CAP_SYS_ADMIN))
362 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
365 data = kzalloc(sizeof(*data), GFP_KERNEL);
369 /* userspace wants to query CSR length */
370 if (!input.address || !input.length)
373 /* allocate a physically contiguous buffer to store the CSR blob */
374 if (!access_ok(input.address, input.length) ||
375 input.length > SEV_FW_BLOB_MAX_SIZE) {
380 blob = kmalloc(input.length, GFP_KERNEL);
386 data->address = __psp_pa(blob);
387 data->len = input.length;
390 if (sev->state == SEV_STATE_UNINIT) {
391 ret = __sev_platform_init_locked(&argp->error);
396 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
398 /* If we query the CSR length, FW responded with expected data. */
399 input.length = data->len;
401 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
407 if (copy_to_user((void __user *)input.address, blob, input.length))
418 void *psp_copy_user_blob(u64 __user uaddr, u32 len)
421 return ERR_PTR(-EINVAL);
423 /* verify that blob length does not exceed our limit */
424 if (len > SEV_FW_BLOB_MAX_SIZE)
425 return ERR_PTR(-EINVAL);
427 return memdup_user((void __user *)(uintptr_t)uaddr, len);
429 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
431 static int sev_get_api_version(void)
433 struct sev_device *sev = psp_master->sev_data;
434 struct sev_user_data_status *status;
437 status = &sev->status_cmd_buf;
438 ret = sev_platform_status(status, &error);
441 "SEV: failed to get status. Error: %#x\n", error);
445 sev->api_major = status->api_major;
446 sev->api_minor = status->api_minor;
447 sev->build = status->build;
448 sev->state = status->state;
453 static int sev_get_firmware(struct device *dev,
454 const struct firmware **firmware)
456 char fw_name_specific[SEV_FW_NAME_SIZE];
457 char fw_name_subset[SEV_FW_NAME_SIZE];
459 snprintf(fw_name_specific, sizeof(fw_name_specific),
460 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
461 boot_cpu_data.x86, boot_cpu_data.x86_model);
463 snprintf(fw_name_subset, sizeof(fw_name_subset),
464 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
465 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
467 /* Check for SEV FW for a particular model.
468 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
472 * Check for SEV FW common to a subset of models.
473 * Ex. amd_sev_fam17h_model0xh.sbin for
474 * Family 17h Model 00h -- Family 17h Model 0Fh
478 * Fall-back to using generic name: sev.fw
480 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
481 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
482 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
488 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
489 static int sev_update_firmware(struct device *dev)
491 struct sev_data_download_firmware *data;
492 const struct firmware *firmware;
493 int ret, error, order;
497 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
498 dev_dbg(dev, "No SEV firmware file present\n");
503 * SEV FW expects the physical address given to it to be 32
504 * byte aligned. Memory allocated has structure placed at the
505 * beginning followed by the firmware being passed to the SEV
506 * FW. Allocate enough memory for data structure + alignment
509 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
511 order = get_order(firmware->size + data_size);
512 p = alloc_pages(GFP_KERNEL, order);
519 * Copy firmware data to a kernel allocated contiguous
522 data = page_address(p);
523 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
525 data->address = __psp_pa(page_address(p) + data_size);
526 data->len = firmware->size;
528 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
530 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
532 dev_info(dev, "SEV firmware update successful\n");
534 __free_pages(p, order);
537 release_firmware(firmware);
542 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp)
544 struct sev_device *sev = psp_master->sev_data;
545 struct sev_user_data_pek_cert_import input;
546 struct sev_data_pek_cert_import *data;
547 void *pek_blob, *oca_blob;
550 if (!capable(CAP_SYS_ADMIN))
553 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
556 data = kzalloc(sizeof(*data), GFP_KERNEL);
560 /* copy PEK certificate blobs from userspace */
561 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
562 if (IS_ERR(pek_blob)) {
563 ret = PTR_ERR(pek_blob);
567 data->pek_cert_address = __psp_pa(pek_blob);
568 data->pek_cert_len = input.pek_cert_len;
570 /* copy PEK certificate blobs from userspace */
571 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
572 if (IS_ERR(oca_blob)) {
573 ret = PTR_ERR(oca_blob);
577 data->oca_cert_address = __psp_pa(oca_blob);
578 data->oca_cert_len = input.oca_cert_len;
580 /* If platform is not in INIT state then transition it to INIT */
581 if (sev->state != SEV_STATE_INIT) {
582 ret = __sev_platform_init_locked(&argp->error);
587 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
598 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
600 struct sev_user_data_get_id2 input;
601 struct sev_data_get_id *data;
602 void *id_blob = NULL;
605 /* SEV GET_ID is available from SEV API v0.16 and up */
606 if (!sev_version_greater_or_equal(0, 16))
609 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
612 /* Check if we have write access to the userspace buffer */
615 !access_ok(input.address, input.length))
618 data = kzalloc(sizeof(*data), GFP_KERNEL);
622 if (input.address && input.length) {
623 id_blob = kmalloc(input.length, GFP_KERNEL);
629 data->address = __psp_pa(id_blob);
630 data->len = input.length;
633 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
636 * Firmware will return the length of the ID value (either the minimum
637 * required length or the actual length written), return it to the user.
639 input.length = data->len;
641 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
647 if (copy_to_user((void __user *)input.address,
648 id_blob, data->len)) {
661 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
663 struct sev_data_get_id *data;
664 u64 data_size, user_size;
668 /* SEV GET_ID available from SEV API v0.16 and up */
669 if (!sev_version_greater_or_equal(0, 16))
672 /* SEV FW expects the buffer it fills with the ID to be
673 * 8-byte aligned. Memory allocated should be enough to
674 * hold data structure + alignment padding + memory
675 * where SEV FW writes the ID.
677 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
678 user_size = sizeof(struct sev_user_data_get_id);
680 mem = kzalloc(data_size + user_size, GFP_KERNEL);
685 id_blob = mem + data_size;
687 data->address = __psp_pa(id_blob);
688 data->len = user_size;
690 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
692 if (copy_to_user((void __user *)argp->data, id_blob, data->len))
701 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp)
703 struct sev_device *sev = psp_master->sev_data;
704 struct sev_user_data_pdh_cert_export input;
705 void *pdh_blob = NULL, *cert_blob = NULL;
706 struct sev_data_pdh_cert_export *data;
709 /* If platform is not in INIT state then transition it to INIT. */
710 if (sev->state != SEV_STATE_INIT) {
711 if (!capable(CAP_SYS_ADMIN))
714 ret = __sev_platform_init_locked(&argp->error);
719 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
722 data = kzalloc(sizeof(*data), GFP_KERNEL);
726 /* Userspace wants to query the certificate length. */
727 if (!input.pdh_cert_address ||
728 !input.pdh_cert_len ||
729 !input.cert_chain_address)
732 /* Allocate a physically contiguous buffer to store the PDH blob. */
733 if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
734 !access_ok(input.pdh_cert_address, input.pdh_cert_len)) {
739 /* Allocate a physically contiguous buffer to store the cert chain blob. */
740 if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
741 !access_ok(input.cert_chain_address, input.cert_chain_len)) {
746 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
752 data->pdh_cert_address = __psp_pa(pdh_blob);
753 data->pdh_cert_len = input.pdh_cert_len;
755 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
761 data->cert_chain_address = __psp_pa(cert_blob);
762 data->cert_chain_len = input.cert_chain_len;
765 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
767 /* If we query the length, FW responded with expected data. */
768 input.cert_chain_len = data->cert_chain_len;
769 input.pdh_cert_len = data->pdh_cert_len;
771 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
777 if (copy_to_user((void __user *)input.pdh_cert_address,
778 pdh_blob, input.pdh_cert_len)) {
785 if (copy_to_user((void __user *)input.cert_chain_address,
786 cert_blob, input.cert_chain_len))
799 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
801 void __user *argp = (void __user *)arg;
802 struct sev_issue_cmd input;
805 if (!psp_master || !psp_master->sev_data)
808 if (ioctl != SEV_ISSUE_CMD)
811 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
814 if (input.cmd > SEV_MAX)
817 mutex_lock(&sev_cmd_mutex);
821 case SEV_FACTORY_RESET:
822 ret = sev_ioctl_do_reset(&input);
824 case SEV_PLATFORM_STATUS:
825 ret = sev_ioctl_do_platform_status(&input);
828 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input);
831 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input);
834 ret = sev_ioctl_do_pek_csr(&input);
836 case SEV_PEK_CERT_IMPORT:
837 ret = sev_ioctl_do_pek_import(&input);
839 case SEV_PDH_CERT_EXPORT:
840 ret = sev_ioctl_do_pdh_export(&input);
843 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
844 ret = sev_ioctl_do_get_id(&input);
847 ret = sev_ioctl_do_get_id2(&input);
854 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
857 mutex_unlock(&sev_cmd_mutex);
862 static const struct file_operations sev_fops = {
863 .owner = THIS_MODULE,
864 .unlocked_ioctl = sev_ioctl,
867 int sev_platform_status(struct sev_user_data_status *data, int *error)
869 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
871 EXPORT_SYMBOL_GPL(sev_platform_status);
873 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
875 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
877 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
879 int sev_guest_activate(struct sev_data_activate *data, int *error)
881 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
883 EXPORT_SYMBOL_GPL(sev_guest_activate);
885 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
887 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
889 EXPORT_SYMBOL_GPL(sev_guest_decommission);
891 int sev_guest_df_flush(int *error)
893 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
895 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
897 static void sev_exit(struct kref *ref)
899 struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
901 misc_deregister(&misc_dev->misc);
904 static int sev_misc_init(struct sev_device *sev)
906 struct device *dev = sev->dev;
910 * SEV feature support can be detected on multiple devices but the SEV
911 * FW commands must be issued on the master. During probe, we do not
912 * know the master hence we create /dev/sev on the first device probe.
913 * sev_do_cmd() finds the right master device to which to issue the
914 * command to the firmware.
917 struct miscdevice *misc;
919 misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
923 misc = &misc_dev->misc;
924 misc->minor = MISC_DYNAMIC_MINOR;
925 misc->name = DEVICE_NAME;
926 misc->fops = &sev_fops;
928 ret = misc_register(misc);
932 kref_init(&misc_dev->refcount);
934 kref_get(&misc_dev->refcount);
937 init_waitqueue_head(&sev->int_queue);
938 sev->misc = misc_dev;
939 dev_dbg(dev, "registered SEV device\n");
944 int sev_dev_init(struct psp_device *psp)
946 struct device *dev = psp->dev;
947 struct sev_device *sev;
950 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
959 sev->io_regs = psp->io_regs;
961 sev->vdata = (struct sev_vdata *)psp->vdata->sev;
964 dev_err(dev, "sev: missing driver data\n");
968 psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
970 ret = sev_misc_init(sev);
974 dev_notice(dev, "sev enabled\n");
979 psp_clear_sev_irq_handler(psp);
981 psp->sev_data = NULL;
983 dev_notice(dev, "sev initialization failed\n");
988 void sev_dev_destroy(struct psp_device *psp)
990 struct sev_device *sev = psp->sev_data;
996 kref_put(&misc_dev->refcount, sev_exit);
998 psp_clear_sev_irq_handler(psp);
1001 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1002 void *data, int *error)
1004 if (!filep || filep->f_op != &sev_fops)
1007 return sev_do_cmd(cmd, data, error);
1009 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1011 void sev_pci_init(void)
1013 struct sev_device *sev = psp_master->sev_data;
1019 psp_timeout = psp_probe_timeout;
1021 if (sev_get_api_version())
1025 * If platform is not in UNINIT state then firmware upgrade and/or
1026 * platform INIT command will fail. These command require UNINIT state.
1028 * In a normal boot we should never run into case where the firmware
1029 * is not in UNINIT state on boot. But in case of kexec boot, a reboot
1030 * may not go through a typical shutdown sequence and may leave the
1031 * firmware in INIT or WORKING state.
1034 if (sev->state != SEV_STATE_UNINIT) {
1035 sev_platform_shutdown(NULL);
1036 sev->state = SEV_STATE_UNINIT;
1039 if (sev_version_greater_or_equal(0, 15) &&
1040 sev_update_firmware(sev->dev) == 0)
1041 sev_get_api_version();
1043 /* Initialize the platform */
1044 rc = sev_platform_init(&error);
1045 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) {
1047 * INIT command returned an integrity check failure
1048 * status code, meaning that firmware load and
1049 * validation of SEV related persistent data has
1050 * failed and persistent state has been erased.
1051 * Retrying INIT command here should succeed.
1053 dev_dbg(sev->dev, "SEV: retrying INIT command");
1054 rc = sev_platform_init(&error);
1058 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error);
1062 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
1063 sev->api_minor, sev->build);
1068 psp_master->sev_data = NULL;
1071 void sev_pci_exit(void)
1073 if (!psp_master->sev_data)
1076 sev_platform_shutdown(NULL);