xref: /openbmc/linux/drivers/crypto/ccp/sev-dev.c (revision 88aa493f)
19b67d08dSRijo Thomas // SPDX-License-Identifier: GPL-2.0-only
29b67d08dSRijo Thomas /*
39b67d08dSRijo Thomas  * AMD Secure Encrypted Virtualization (SEV) interface
49b67d08dSRijo Thomas  *
59b67d08dSRijo Thomas  * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
69b67d08dSRijo Thomas  *
79b67d08dSRijo Thomas  * Author: Brijesh Singh <brijesh.singh@amd.com>
89b67d08dSRijo Thomas  */
99b67d08dSRijo Thomas 
101c5c1dafSMario Limonciello #include <linux/bitfield.h>
119b67d08dSRijo Thomas #include <linux/module.h>
129b67d08dSRijo Thomas #include <linux/kernel.h>
139b67d08dSRijo Thomas #include <linux/kthread.h>
149b67d08dSRijo Thomas #include <linux/sched.h>
159b67d08dSRijo Thomas #include <linux/interrupt.h>
169b67d08dSRijo Thomas #include <linux/spinlock.h>
179b67d08dSRijo Thomas #include <linux/spinlock_types.h>
189b67d08dSRijo Thomas #include <linux/types.h>
199b67d08dSRijo Thomas #include <linux/mutex.h>
209b67d08dSRijo Thomas #include <linux/delay.h>
219b67d08dSRijo Thomas #include <linux/hw_random.h>
229b67d08dSRijo Thomas #include <linux/ccp.h>
239b67d08dSRijo Thomas #include <linux/firmware.h>
2497f9ac3dSTom Lendacky #include <linux/gfp.h>
251877c73bSTom Lendacky #include <linux/cpufeature.h>
263d725965SDavid Rientjes #include <linux/fs.h>
2705def5caSJacky Li #include <linux/fs_struct.h>
28ae7d45fbSMario Limonciello #include <linux/psp.h>
299b67d08dSRijo Thomas 
309b67d08dSRijo Thomas #include <asm/smp.h>
3146a334a9STom Lendacky #include <asm/cacheflush.h>
329b67d08dSRijo Thomas 
33b93566f1SRijo Thomas #include "psp-dev.h"
349b67d08dSRijo Thomas #include "sev-dev.h"
359b67d08dSRijo Thomas 
369b67d08dSRijo Thomas #define DEVICE_NAME		"sev"
379b67d08dSRijo Thomas #define SEV_FW_FILE		"amd/sev.fw"
389b67d08dSRijo Thomas #define SEV_FW_NAME_SIZE	64
399b67d08dSRijo Thomas 
409b67d08dSRijo Thomas static DEFINE_MUTEX(sev_cmd_mutex);
419b67d08dSRijo Thomas static struct sev_misc_dev *misc_dev;
429b67d08dSRijo Thomas 
439b67d08dSRijo Thomas static int psp_cmd_timeout = 100;
449b67d08dSRijo Thomas module_param(psp_cmd_timeout, int, 0644);
459b67d08dSRijo Thomas MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
469b67d08dSRijo Thomas 
479b67d08dSRijo Thomas static int psp_probe_timeout = 5;
489b67d08dSRijo Thomas module_param(psp_probe_timeout, int, 0644);
499b67d08dSRijo Thomas MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
509b67d08dSRijo Thomas 
513d725965SDavid Rientjes static char *init_ex_path;
523d725965SDavid Rientjes module_param(init_ex_path, charp, 0444);
533d725965SDavid Rientjes MODULE_PARM_DESC(init_ex_path, " Path for INIT_EX data; if set try INIT_EX");
543d725965SDavid Rientjes 
55b64fa5fcSPeter Gonda static bool psp_init_on_probe = true;
56b64fa5fcSPeter Gonda module_param(psp_init_on_probe, bool, 0444);
57b64fa5fcSPeter Gonda MODULE_PARM_DESC(psp_init_on_probe, "  if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
58b64fa5fcSPeter Gonda 
59c8671c7dSJoerg Roedel MODULE_FIRMWARE("amd/amd_sev_fam17h_model0xh.sbin"); /* 1st gen EPYC */
60c8671c7dSJoerg Roedel MODULE_FIRMWARE("amd/amd_sev_fam17h_model3xh.sbin"); /* 2nd gen EPYC */
61c8671c7dSJoerg Roedel MODULE_FIRMWARE("amd/amd_sev_fam19h_model0xh.sbin"); /* 3rd gen EPYC */
624fc790d7STom Lendacky MODULE_FIRMWARE("amd/amd_sev_fam19h_model1xh.sbin"); /* 4th gen EPYC */
63c8671c7dSJoerg Roedel 
649b67d08dSRijo Thomas static bool psp_dead;
659b67d08dSRijo Thomas static int psp_timeout;
669b67d08dSRijo Thomas 
6797f9ac3dSTom Lendacky /* Trusted Memory Region (TMR):
6897f9ac3dSTom Lendacky  *   The TMR is a 1MB area that must be 1MB aligned.  Use the page allocator
6997f9ac3dSTom Lendacky  *   to allocate the memory, which will return aligned memory for the specified
7097f9ac3dSTom Lendacky  *   allocation order.
7197f9ac3dSTom Lendacky  */
7297f9ac3dSTom Lendacky #define SEV_ES_TMR_SIZE		(1024 * 1024)
7397f9ac3dSTom Lendacky static void *sev_es_tmr;
7497f9ac3dSTom Lendacky 
753d725965SDavid Rientjes /* INIT_EX NV Storage:
763d725965SDavid Rientjes  *   The NV Storage is a 32Kb area and must be 4Kb page aligned.  Use the page
773d725965SDavid Rientjes  *   allocator to allocate the memory, which will return aligned memory for the
783d725965SDavid Rientjes  *   specified allocation order.
793d725965SDavid Rientjes  */
803d725965SDavid Rientjes #define NV_LENGTH (32 * 1024)
813d725965SDavid Rientjes static void *sev_init_ex_buffer;
823d725965SDavid Rientjes 
sev_version_greater_or_equal(u8 maj,u8 min)839b67d08dSRijo Thomas static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
849b67d08dSRijo Thomas {
85b93566f1SRijo Thomas 	struct sev_device *sev = psp_master->sev_data;
86b93566f1SRijo Thomas 
87b93566f1SRijo Thomas 	if (sev->api_major > maj)
889b67d08dSRijo Thomas 		return true;
89b93566f1SRijo Thomas 
90b93566f1SRijo Thomas 	if (sev->api_major == maj && sev->api_minor >= min)
919b67d08dSRijo Thomas 		return true;
92b93566f1SRijo Thomas 
939b67d08dSRijo Thomas 	return false;
949b67d08dSRijo Thomas }
959b67d08dSRijo Thomas 
sev_irq_handler(int irq,void * data,unsigned int status)96b93566f1SRijo Thomas static void sev_irq_handler(int irq, void *data, unsigned int status)
979b67d08dSRijo Thomas {
98b93566f1SRijo Thomas 	struct sev_device *sev = data;
999b67d08dSRijo Thomas 	int reg;
1009b67d08dSRijo Thomas 
1019b67d08dSRijo Thomas 	/* Check if it is command completion: */
102b93566f1SRijo Thomas 	if (!(status & SEV_CMD_COMPLETE))
103b93566f1SRijo Thomas 		return;
1049b67d08dSRijo Thomas 
1059b67d08dSRijo Thomas 	/* Check if it is SEV command completion: */
1066eb0cc72SRijo Thomas 	reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
1071c5c1dafSMario Limonciello 	if (FIELD_GET(PSP_CMDRESP_RESP, reg)) {
108b93566f1SRijo Thomas 		sev->int_rcvd = 1;
109b93566f1SRijo Thomas 		wake_up(&sev->int_queue);
110b93566f1SRijo Thomas 	}
1119b67d08dSRijo Thomas }
1129b67d08dSRijo Thomas 
sev_wait_cmd_ioc(struct sev_device * sev,unsigned int * reg,unsigned int timeout)113b93566f1SRijo Thomas static int sev_wait_cmd_ioc(struct sev_device *sev,
1149b67d08dSRijo Thomas 			    unsigned int *reg, unsigned int timeout)
1159b67d08dSRijo Thomas {
1169b67d08dSRijo Thomas 	int ret;
1179b67d08dSRijo Thomas 
118b93566f1SRijo Thomas 	ret = wait_event_timeout(sev->int_queue,
119b93566f1SRijo Thomas 			sev->int_rcvd, timeout * HZ);
1209b67d08dSRijo Thomas 	if (!ret)
1219b67d08dSRijo Thomas 		return -ETIMEDOUT;
1229b67d08dSRijo Thomas 
1236eb0cc72SRijo Thomas 	*reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
1249b67d08dSRijo Thomas 
1259b67d08dSRijo Thomas 	return 0;
1269b67d08dSRijo Thomas }
1279b67d08dSRijo Thomas 
sev_cmd_buffer_len(int cmd)1289b67d08dSRijo Thomas static int sev_cmd_buffer_len(int cmd)
1299b67d08dSRijo Thomas {
1309b67d08dSRijo Thomas 	switch (cmd) {
1319b67d08dSRijo Thomas 	case SEV_CMD_INIT:			return sizeof(struct sev_data_init);
1323d725965SDavid Rientjes 	case SEV_CMD_INIT_EX:                   return sizeof(struct sev_data_init_ex);
1339b67d08dSRijo Thomas 	case SEV_CMD_PLATFORM_STATUS:		return sizeof(struct sev_user_data_status);
1349b67d08dSRijo Thomas 	case SEV_CMD_PEK_CSR:			return sizeof(struct sev_data_pek_csr);
1359b67d08dSRijo Thomas 	case SEV_CMD_PEK_CERT_IMPORT:		return sizeof(struct sev_data_pek_cert_import);
1369b67d08dSRijo Thomas 	case SEV_CMD_PDH_CERT_EXPORT:		return sizeof(struct sev_data_pdh_cert_export);
1379b67d08dSRijo Thomas 	case SEV_CMD_LAUNCH_START:		return sizeof(struct sev_data_launch_start);
1389b67d08dSRijo Thomas 	case SEV_CMD_LAUNCH_UPDATE_DATA:	return sizeof(struct sev_data_launch_update_data);
1399b67d08dSRijo Thomas 	case SEV_CMD_LAUNCH_UPDATE_VMSA:	return sizeof(struct sev_data_launch_update_vmsa);
1409b67d08dSRijo Thomas 	case SEV_CMD_LAUNCH_FINISH:		return sizeof(struct sev_data_launch_finish);
1419b67d08dSRijo Thomas 	case SEV_CMD_LAUNCH_MEASURE:		return sizeof(struct sev_data_launch_measure);
1429b67d08dSRijo Thomas 	case SEV_CMD_ACTIVATE:			return sizeof(struct sev_data_activate);
1439b67d08dSRijo Thomas 	case SEV_CMD_DEACTIVATE:		return sizeof(struct sev_data_deactivate);
1449b67d08dSRijo Thomas 	case SEV_CMD_DECOMMISSION:		return sizeof(struct sev_data_decommission);
1459b67d08dSRijo Thomas 	case SEV_CMD_GUEST_STATUS:		return sizeof(struct sev_data_guest_status);
1469b67d08dSRijo Thomas 	case SEV_CMD_DBG_DECRYPT:		return sizeof(struct sev_data_dbg);
1479b67d08dSRijo Thomas 	case SEV_CMD_DBG_ENCRYPT:		return sizeof(struct sev_data_dbg);
1489b67d08dSRijo Thomas 	case SEV_CMD_SEND_START:		return sizeof(struct sev_data_send_start);
1499b67d08dSRijo Thomas 	case SEV_CMD_SEND_UPDATE_DATA:		return sizeof(struct sev_data_send_update_data);
1509b67d08dSRijo Thomas 	case SEV_CMD_SEND_UPDATE_VMSA:		return sizeof(struct sev_data_send_update_vmsa);
1519b67d08dSRijo Thomas 	case SEV_CMD_SEND_FINISH:		return sizeof(struct sev_data_send_finish);
1529b67d08dSRijo Thomas 	case SEV_CMD_RECEIVE_START:		return sizeof(struct sev_data_receive_start);
1539b67d08dSRijo Thomas 	case SEV_CMD_RECEIVE_FINISH:		return sizeof(struct sev_data_receive_finish);
1549b67d08dSRijo Thomas 	case SEV_CMD_RECEIVE_UPDATE_DATA:	return sizeof(struct sev_data_receive_update_data);
1559b67d08dSRijo Thomas 	case SEV_CMD_RECEIVE_UPDATE_VMSA:	return sizeof(struct sev_data_receive_update_vmsa);
1569b67d08dSRijo Thomas 	case SEV_CMD_LAUNCH_UPDATE_SECRET:	return sizeof(struct sev_data_launch_secret);
1579b67d08dSRijo Thomas 	case SEV_CMD_DOWNLOAD_FIRMWARE:		return sizeof(struct sev_data_download_firmware);
1589b67d08dSRijo Thomas 	case SEV_CMD_GET_ID:			return sizeof(struct sev_data_get_id);
1592c07ded0SBrijesh Singh 	case SEV_CMD_ATTESTATION_REPORT:	return sizeof(struct sev_data_attestation_report);
1605569e2e7SSteve Rutherford 	case SEV_CMD_SEND_CANCEL:		return sizeof(struct sev_data_send_cancel);
1619b67d08dSRijo Thomas 	default:				return 0;
1629b67d08dSRijo Thomas 	}
1639b67d08dSRijo Thomas 
1649b67d08dSRijo Thomas 	return 0;
1659b67d08dSRijo Thomas }
1669b67d08dSRijo Thomas 
sev_fw_alloc(unsigned long len)167cc17982dSPeter Gonda static void *sev_fw_alloc(unsigned long len)
168cc17982dSPeter Gonda {
169cc17982dSPeter Gonda 	struct page *page;
170cc17982dSPeter Gonda 
171cc17982dSPeter Gonda 	page = alloc_pages(GFP_KERNEL, get_order(len));
172cc17982dSPeter Gonda 	if (!page)
173cc17982dSPeter Gonda 		return NULL;
174cc17982dSPeter Gonda 
175cc17982dSPeter Gonda 	return page_address(page);
176cc17982dSPeter Gonda }
177cc17982dSPeter Gonda 
open_file_as_root(const char * filename,int flags,umode_t mode)17805def5caSJacky Li static struct file *open_file_as_root(const char *filename, int flags, umode_t mode)
17905def5caSJacky Li {
18005def5caSJacky Li 	struct file *fp;
18105def5caSJacky Li 	struct path root;
18205def5caSJacky Li 	struct cred *cred;
18305def5caSJacky Li 	const struct cred *old_cred;
18405def5caSJacky Li 
18505def5caSJacky Li 	task_lock(&init_task);
18605def5caSJacky Li 	get_fs_root(init_task.fs, &root);
18705def5caSJacky Li 	task_unlock(&init_task);
18805def5caSJacky Li 
18905def5caSJacky Li 	cred = prepare_creds();
19005def5caSJacky Li 	if (!cred)
19105def5caSJacky Li 		return ERR_PTR(-ENOMEM);
19205def5caSJacky Li 	cred->fsuid = GLOBAL_ROOT_UID;
19305def5caSJacky Li 	old_cred = override_creds(cred);
19405def5caSJacky Li 
19505def5caSJacky Li 	fp = file_open_root(&root, filename, flags, mode);
19605def5caSJacky Li 	path_put(&root);
19705def5caSJacky Li 
19805def5caSJacky Li 	revert_creds(old_cred);
19905def5caSJacky Li 
20005def5caSJacky Li 	return fp;
20105def5caSJacky Li }
20205def5caSJacky Li 
sev_read_init_ex_file(void)2033d725965SDavid Rientjes static int sev_read_init_ex_file(void)
2043d725965SDavid Rientjes {
2053d725965SDavid Rientjes 	struct sev_device *sev = psp_master->sev_data;
2063d725965SDavid Rientjes 	struct file *fp;
2073d725965SDavid Rientjes 	ssize_t nread;
2083d725965SDavid Rientjes 
2093d725965SDavid Rientjes 	lockdep_assert_held(&sev_cmd_mutex);
2103d725965SDavid Rientjes 
2113d725965SDavid Rientjes 	if (!sev_init_ex_buffer)
2123d725965SDavid Rientjes 		return -EOPNOTSUPP;
2133d725965SDavid Rientjes 
21405def5caSJacky Li 	fp = open_file_as_root(init_ex_path, O_RDONLY, 0);
2153d725965SDavid Rientjes 	if (IS_ERR(fp)) {
2163d725965SDavid Rientjes 		int ret = PTR_ERR(fp);
2173d725965SDavid Rientjes 
218d8da2da2SJacky Li 		if (ret == -ENOENT) {
219d8da2da2SJacky Li 			dev_info(sev->dev,
220d8da2da2SJacky Li 				"SEV: %s does not exist and will be created later.\n",
221d8da2da2SJacky Li 				init_ex_path);
222d8da2da2SJacky Li 			ret = 0;
223d8da2da2SJacky Li 		} else {
2243d725965SDavid Rientjes 			dev_err(sev->dev,
2253d725965SDavid Rientjes 				"SEV: could not open %s for read, error %d\n",
2263d725965SDavid Rientjes 				init_ex_path, ret);
227d8da2da2SJacky Li 		}
2283d725965SDavid Rientjes 		return ret;
2293d725965SDavid Rientjes 	}
2303d725965SDavid Rientjes 
2313d725965SDavid Rientjes 	nread = kernel_read(fp, sev_init_ex_buffer, NV_LENGTH, NULL);
2323d725965SDavid Rientjes 	if (nread != NV_LENGTH) {
233d8da2da2SJacky Li 		dev_info(sev->dev,
234d8da2da2SJacky Li 			"SEV: could not read %u bytes to non volatile memory area, ret %ld\n",
2353d725965SDavid Rientjes 			NV_LENGTH, nread);
2363d725965SDavid Rientjes 	}
2373d725965SDavid Rientjes 
2383d725965SDavid Rientjes 	dev_dbg(sev->dev, "SEV: read %ld bytes from NV file\n", nread);
2393d725965SDavid Rientjes 	filp_close(fp, NULL);
2403d725965SDavid Rientjes 
2413d725965SDavid Rientjes 	return 0;
2423d725965SDavid Rientjes }
2433d725965SDavid Rientjes 
sev_write_init_ex_file(void)244efb4b01cSJacky Li static int sev_write_init_ex_file(void)
2453d725965SDavid Rientjes {
2463d725965SDavid Rientjes 	struct sev_device *sev = psp_master->sev_data;
2473d725965SDavid Rientjes 	struct file *fp;
2483d725965SDavid Rientjes 	loff_t offset = 0;
2493d725965SDavid Rientjes 	ssize_t nwrite;
2503d725965SDavid Rientjes 
2513d725965SDavid Rientjes 	lockdep_assert_held(&sev_cmd_mutex);
2523d725965SDavid Rientjes 
2533d725965SDavid Rientjes 	if (!sev_init_ex_buffer)
254efb4b01cSJacky Li 		return 0;
2553d725965SDavid Rientjes 
25605def5caSJacky Li 	fp = open_file_as_root(init_ex_path, O_CREAT | O_WRONLY, 0600);
2573d725965SDavid Rientjes 	if (IS_ERR(fp)) {
258efb4b01cSJacky Li 		int ret = PTR_ERR(fp);
259efb4b01cSJacky Li 
2603d725965SDavid Rientjes 		dev_err(sev->dev,
261efb4b01cSJacky Li 			"SEV: could not open file for write, error %d\n",
262efb4b01cSJacky Li 			ret);
263efb4b01cSJacky Li 		return ret;
2643d725965SDavid Rientjes 	}
2653d725965SDavid Rientjes 
2663d725965SDavid Rientjes 	nwrite = kernel_write(fp, sev_init_ex_buffer, NV_LENGTH, &offset);
2673d725965SDavid Rientjes 	vfs_fsync(fp, 0);
2683d725965SDavid Rientjes 	filp_close(fp, NULL);
2693d725965SDavid Rientjes 
2703d725965SDavid Rientjes 	if (nwrite != NV_LENGTH) {
2713d725965SDavid Rientjes 		dev_err(sev->dev,
2723d725965SDavid Rientjes 			"SEV: failed to write %u bytes to non volatile memory area, ret %ld\n",
2733d725965SDavid Rientjes 			NV_LENGTH, nwrite);
274efb4b01cSJacky Li 		return -EIO;
2753d725965SDavid Rientjes 	}
2763d725965SDavid Rientjes 
2773d725965SDavid Rientjes 	dev_dbg(sev->dev, "SEV: write successful to NV file\n");
278efb4b01cSJacky Li 
279efb4b01cSJacky Li 	return 0;
2803d725965SDavid Rientjes }
2813d725965SDavid Rientjes 
sev_write_init_ex_file_if_required(int cmd_id)282efb4b01cSJacky Li static int sev_write_init_ex_file_if_required(int cmd_id)
2833d725965SDavid Rientjes {
2843d725965SDavid Rientjes 	lockdep_assert_held(&sev_cmd_mutex);
2853d725965SDavid Rientjes 
2863d725965SDavid Rientjes 	if (!sev_init_ex_buffer)
287efb4b01cSJacky Li 		return 0;
2883d725965SDavid Rientjes 
2893d725965SDavid Rientjes 	/*
2903d725965SDavid Rientjes 	 * Only a few platform commands modify the SPI/NV area, but none of the
2913d725965SDavid Rientjes 	 * non-platform commands do. Only INIT(_EX), PLATFORM_RESET, PEK_GEN,
2923d725965SDavid Rientjes 	 * PEK_CERT_IMPORT, and PDH_GEN do.
2933d725965SDavid Rientjes 	 */
2943d725965SDavid Rientjes 	switch (cmd_id) {
2953d725965SDavid Rientjes 	case SEV_CMD_FACTORY_RESET:
2963d725965SDavid Rientjes 	case SEV_CMD_INIT_EX:
2973d725965SDavid Rientjes 	case SEV_CMD_PDH_GEN:
2983d725965SDavid Rientjes 	case SEV_CMD_PEK_CERT_IMPORT:
2993d725965SDavid Rientjes 	case SEV_CMD_PEK_GEN:
3003d725965SDavid Rientjes 		break;
3013d725965SDavid Rientjes 	default:
302efb4b01cSJacky Li 		return 0;
303ef4d8914SYang Li 	}
3043d725965SDavid Rientjes 
305efb4b01cSJacky Li 	return sev_write_init_ex_file();
3063d725965SDavid Rientjes }
3073d725965SDavid Rientjes 
__sev_do_cmd_locked(int cmd,void * data,int * psp_ret)3089b67d08dSRijo Thomas static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
3099b67d08dSRijo Thomas {
3109b67d08dSRijo Thomas 	struct psp_device *psp = psp_master;
311b93566f1SRijo Thomas 	struct sev_device *sev;
3129b67d08dSRijo Thomas 	unsigned int phys_lsb, phys_msb;
3139b67d08dSRijo Thomas 	unsigned int reg, ret = 0;
314d5760deeSSean Christopherson 	int buf_len;
3159b67d08dSRijo Thomas 
316b93566f1SRijo Thomas 	if (!psp || !psp->sev_data)
3179b67d08dSRijo Thomas 		return -ENODEV;
3189b67d08dSRijo Thomas 
3199b67d08dSRijo Thomas 	if (psp_dead)
3209b67d08dSRijo Thomas 		return -EBUSY;
3219b67d08dSRijo Thomas 
322b93566f1SRijo Thomas 	sev = psp->sev_data;
323b93566f1SRijo Thomas 
324d5760deeSSean Christopherson 	buf_len = sev_cmd_buffer_len(cmd);
325d5760deeSSean Christopherson 	if (WARN_ON_ONCE(!data != !buf_len))
326d5760deeSSean Christopherson 		return -EINVAL;
327d5760deeSSean Christopherson 
3288347b994SSean Christopherson 	/*
3298347b994SSean Christopherson 	 * Copy the incoming data to driver's scratch buffer as __pa() will not
3308347b994SSean Christopherson 	 * work for some memory, e.g. vmalloc'd addresses, and @data may not be
3318347b994SSean Christopherson 	 * physically contiguous.
3328347b994SSean Christopherson 	 */
3338347b994SSean Christopherson 	if (data)
3348347b994SSean Christopherson 		memcpy(sev->cmd_buf, data, buf_len);
33574c1f136SSean Christopherson 
3369b67d08dSRijo Thomas 	/* Get the physical address of the command buffer */
3378347b994SSean Christopherson 	phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0;
3388347b994SSean Christopherson 	phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0;
3399b67d08dSRijo Thomas 
340b93566f1SRijo Thomas 	dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
3419b67d08dSRijo Thomas 		cmd, phys_msb, phys_lsb, psp_timeout);
3429b67d08dSRijo Thomas 
3439b67d08dSRijo Thomas 	print_hex_dump_debug("(in):  ", DUMP_PREFIX_OFFSET, 16, 2, data,
344d5760deeSSean Christopherson 			     buf_len, false);
3459b67d08dSRijo Thomas 
3466eb0cc72SRijo Thomas 	iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
3476eb0cc72SRijo Thomas 	iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
3489b67d08dSRijo Thomas 
349b93566f1SRijo Thomas 	sev->int_rcvd = 0;
3509b67d08dSRijo Thomas 
3511c5c1dafSMario Limonciello 	reg = FIELD_PREP(SEV_CMDRESP_CMD, cmd) | SEV_CMDRESP_IOC;
3526eb0cc72SRijo Thomas 	iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
3539b67d08dSRijo Thomas 
3549b67d08dSRijo Thomas 	/* wait for command completion */
355b93566f1SRijo Thomas 	ret = sev_wait_cmd_ioc(sev, &reg, psp_timeout);
3569b67d08dSRijo Thomas 	if (ret) {
3579b67d08dSRijo Thomas 		if (psp_ret)
3589b67d08dSRijo Thomas 			*psp_ret = 0;
3599b67d08dSRijo Thomas 
360b93566f1SRijo Thomas 		dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
3619b67d08dSRijo Thomas 		psp_dead = true;
3629b67d08dSRijo Thomas 
3639b67d08dSRijo Thomas 		return ret;
3649b67d08dSRijo Thomas 	}
3659b67d08dSRijo Thomas 
3669b67d08dSRijo Thomas 	psp_timeout = psp_cmd_timeout;
3679b67d08dSRijo Thomas 
3689b67d08dSRijo Thomas 	if (psp_ret)
3691c5c1dafSMario Limonciello 		*psp_ret = FIELD_GET(PSP_CMDRESP_STS, reg);
3709b67d08dSRijo Thomas 
3711c5c1dafSMario Limonciello 	if (FIELD_GET(PSP_CMDRESP_STS, reg)) {
3721c5c1dafSMario Limonciello 		dev_dbg(sev->dev, "sev command %#x failed (%#010lx)\n",
3731c5c1dafSMario Limonciello 			cmd, FIELD_GET(PSP_CMDRESP_STS, reg));
3749b67d08dSRijo Thomas 		ret = -EIO;
3753d725965SDavid Rientjes 	} else {
376efb4b01cSJacky Li 		ret = sev_write_init_ex_file_if_required(cmd);
3779b67d08dSRijo Thomas 	}
3789b67d08dSRijo Thomas 
3799b67d08dSRijo Thomas 	print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
380d5760deeSSean Christopherson 			     buf_len, false);
3819b67d08dSRijo Thomas 
3828347b994SSean Christopherson 	/*
3838347b994SSean Christopherson 	 * Copy potential output from the PSP back to data.  Do this even on
3848347b994SSean Christopherson 	 * failure in case the caller wants to glean something from the error.
3858347b994SSean Christopherson 	 */
3868347b994SSean Christopherson 	if (data)
3878347b994SSean Christopherson 		memcpy(data, sev->cmd_buf, buf_len);
3889b67d08dSRijo Thomas 
3899b67d08dSRijo Thomas 	return ret;
3909b67d08dSRijo Thomas }
3919b67d08dSRijo Thomas 
sev_do_cmd(int cmd,void * data,int * psp_ret)3929b67d08dSRijo Thomas static int sev_do_cmd(int cmd, void *data, int *psp_ret)
3939b67d08dSRijo Thomas {
3949b67d08dSRijo Thomas 	int rc;
3959b67d08dSRijo Thomas 
3969b67d08dSRijo Thomas 	mutex_lock(&sev_cmd_mutex);
3979b67d08dSRijo Thomas 	rc = __sev_do_cmd_locked(cmd, data, psp_ret);
3989b67d08dSRijo Thomas 	mutex_unlock(&sev_cmd_mutex);
3999b67d08dSRijo Thomas 
4009b67d08dSRijo Thomas 	return rc;
4019b67d08dSRijo Thomas }
4029b67d08dSRijo Thomas 
__sev_init_locked(int * error)4033d725965SDavid Rientjes static int __sev_init_locked(int *error)
4043d725965SDavid Rientjes {
4053d725965SDavid Rientjes 	struct sev_data_init data;
4063d725965SDavid Rientjes 
4073d725965SDavid Rientjes 	memset(&data, 0, sizeof(data));
4083d725965SDavid Rientjes 	if (sev_es_tmr) {
4093d725965SDavid Rientjes 		/*
4103d725965SDavid Rientjes 		 * Do not include the encryption mask on the physical
4113d725965SDavid Rientjes 		 * address of the TMR (firmware should clear it anyway).
4123d725965SDavid Rientjes 		 */
4133d725965SDavid Rientjes 		data.tmr_address = __pa(sev_es_tmr);
4143d725965SDavid Rientjes 
4153d725965SDavid Rientjes 		data.flags |= SEV_INIT_FLAGS_SEV_ES;
4163d725965SDavid Rientjes 		data.tmr_len = SEV_ES_TMR_SIZE;
4173d725965SDavid Rientjes 	}
4183d725965SDavid Rientjes 
4193d725965SDavid Rientjes 	return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error);
4203d725965SDavid Rientjes }
4213d725965SDavid Rientjes 
__sev_init_ex_locked(int * error)4223d725965SDavid Rientjes static int __sev_init_ex_locked(int *error)
4233d725965SDavid Rientjes {
4243d725965SDavid Rientjes 	struct sev_data_init_ex data;
4253d725965SDavid Rientjes 
4263d725965SDavid Rientjes 	memset(&data, 0, sizeof(data));
4273d725965SDavid Rientjes 	data.length = sizeof(data);
4283d725965SDavid Rientjes 	data.nv_address = __psp_pa(sev_init_ex_buffer);
4293d725965SDavid Rientjes 	data.nv_len = NV_LENGTH;
4303d725965SDavid Rientjes 
4313d725965SDavid Rientjes 	if (sev_es_tmr) {
4323d725965SDavid Rientjes 		/*
4333d725965SDavid Rientjes 		 * Do not include the encryption mask on the physical
4343d725965SDavid Rientjes 		 * address of the TMR (firmware should clear it anyway).
4353d725965SDavid Rientjes 		 */
4363d725965SDavid Rientjes 		data.tmr_address = __pa(sev_es_tmr);
4373d725965SDavid Rientjes 
4383d725965SDavid Rientjes 		data.flags |= SEV_INIT_FLAGS_SEV_ES;
4393d725965SDavid Rientjes 		data.tmr_len = SEV_ES_TMR_SIZE;
4403d725965SDavid Rientjes 	}
4413d725965SDavid Rientjes 
4423d725965SDavid Rientjes 	return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error);
4433d725965SDavid Rientjes }
4443d725965SDavid Rientjes 
__sev_do_init_locked(int * psp_ret)445dbf07b54SBorislav Petkov (AMD) static inline int __sev_do_init_locked(int *psp_ret)
446dbf07b54SBorislav Petkov (AMD) {
447dbf07b54SBorislav Petkov (AMD) 	if (sev_init_ex_buffer)
448dbf07b54SBorislav Petkov (AMD) 		return __sev_init_ex_locked(psp_ret);
449dbf07b54SBorislav Petkov (AMD) 	else
450dbf07b54SBorislav Petkov (AMD) 		return __sev_init_locked(psp_ret);
451dbf07b54SBorislav Petkov (AMD) }
452dbf07b54SBorislav Petkov (AMD) 
__sev_platform_init_locked(int * error)4539b67d08dSRijo Thomas static int __sev_platform_init_locked(int *error)
4549b67d08dSRijo Thomas {
455efb339a8SPeter Gonda 	int rc = 0, psp_ret = SEV_RET_NO_FW_CALL;
4569b67d08dSRijo Thomas 	struct psp_device *psp = psp_master;
457efb339a8SPeter Gonda 	struct sev_device *sev;
4589b67d08dSRijo Thomas 
459b93566f1SRijo Thomas 	if (!psp || !psp->sev_data)
4609b67d08dSRijo Thomas 		return -ENODEV;
4619b67d08dSRijo Thomas 
462b93566f1SRijo Thomas 	sev = psp->sev_data;
463b93566f1SRijo Thomas 
464b93566f1SRijo Thomas 	if (sev->state == SEV_STATE_INIT)
4659b67d08dSRijo Thomas 		return 0;
4669b67d08dSRijo Thomas 
467d8da2da2SJacky Li 	if (sev_init_ex_buffer) {
468d8da2da2SJacky Li 		rc = sev_read_init_ex_file();
469d8da2da2SJacky Li 		if (rc)
470d8da2da2SJacky Li 			return rc;
471d8da2da2SJacky Li 	}
472d8da2da2SJacky Li 
473dbf07b54SBorislav Petkov (AMD) 	rc = __sev_do_init_locked(&psp_ret);
474e423b9d7SPeter Gonda 	if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) {
475e423b9d7SPeter Gonda 		/*
476e423b9d7SPeter Gonda 		 * Initialization command returned an integrity check failure
477e423b9d7SPeter Gonda 		 * status code, meaning that firmware load and validation of SEV
478e423b9d7SPeter Gonda 		 * related persistent data has failed. Retrying the
479e423b9d7SPeter Gonda 		 * initialization function should succeed by replacing the state
480e423b9d7SPeter Gonda 		 * with a reset state.
481e423b9d7SPeter Gonda 		 */
482efb339a8SPeter Gonda 		dev_err(sev->dev,
483efb339a8SPeter Gonda "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state.");
484dbf07b54SBorislav Petkov (AMD) 		rc = __sev_do_init_locked(&psp_ret);
485e423b9d7SPeter Gonda 	}
486efb339a8SPeter Gonda 
487e423b9d7SPeter Gonda 	if (error)
488e423b9d7SPeter Gonda 		*error = psp_ret;
489e423b9d7SPeter Gonda 
4909b67d08dSRijo Thomas 	if (rc)
4919b67d08dSRijo Thomas 		return rc;
4929b67d08dSRijo Thomas 
493b93566f1SRijo Thomas 	sev->state = SEV_STATE_INIT;
4949b67d08dSRijo Thomas 
4959b67d08dSRijo Thomas 	/* Prepare for first SEV guest launch after INIT */
4969b67d08dSRijo Thomas 	wbinvd_on_all_cpus();
4979b67d08dSRijo Thomas 	rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
4989b67d08dSRijo Thomas 	if (rc)
4999b67d08dSRijo Thomas 		return rc;
5009b67d08dSRijo Thomas 
501b93566f1SRijo Thomas 	dev_dbg(sev->dev, "SEV firmware initialized\n");
5029b67d08dSRijo Thomas 
503b64fa5fcSPeter Gonda 	dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
504b64fa5fcSPeter Gonda 		 sev->api_minor, sev->build);
505b64fa5fcSPeter Gonda 
506b64fa5fcSPeter Gonda 	return 0;
5079b67d08dSRijo Thomas }
5089b67d08dSRijo Thomas 
sev_platform_init(int * error)5099b67d08dSRijo Thomas int sev_platform_init(int *error)
5109b67d08dSRijo Thomas {
5119b67d08dSRijo Thomas 	int rc;
5129b67d08dSRijo Thomas 
5139b67d08dSRijo Thomas 	mutex_lock(&sev_cmd_mutex);
5149b67d08dSRijo Thomas 	rc = __sev_platform_init_locked(error);
5159b67d08dSRijo Thomas 	mutex_unlock(&sev_cmd_mutex);
5169b67d08dSRijo Thomas 
5179b67d08dSRijo Thomas 	return rc;
5189b67d08dSRijo Thomas }
5199b67d08dSRijo Thomas EXPORT_SYMBOL_GPL(sev_platform_init);
5209b67d08dSRijo Thomas 
__sev_platform_shutdown_locked(int * error)5219b67d08dSRijo Thomas static int __sev_platform_shutdown_locked(int *error)
5229b67d08dSRijo Thomas {
523*88aa493fSKim Phillips 	struct psp_device *psp = psp_master;
524*88aa493fSKim Phillips 	struct sev_device *sev;
5259b67d08dSRijo Thomas 	int ret;
5269b67d08dSRijo Thomas 
527*88aa493fSKim Phillips 	if (!psp || !psp->sev_data)
528*88aa493fSKim Phillips 		return 0;
529*88aa493fSKim Phillips 
530*88aa493fSKim Phillips 	sev = psp->sev_data;
531*88aa493fSKim Phillips 
532*88aa493fSKim Phillips 	if (sev->state == SEV_STATE_UNINIT)
5335441a07aSBrijesh Singh 		return 0;
5345441a07aSBrijesh Singh 
5359b67d08dSRijo Thomas 	ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
5369b67d08dSRijo Thomas 	if (ret)
5379b67d08dSRijo Thomas 		return ret;
5389b67d08dSRijo Thomas 
539b93566f1SRijo Thomas 	sev->state = SEV_STATE_UNINIT;
540b93566f1SRijo Thomas 	dev_dbg(sev->dev, "SEV firmware shutdown\n");
5419b67d08dSRijo Thomas 
5429b67d08dSRijo Thomas 	return ret;
5439b67d08dSRijo Thomas }
5449b67d08dSRijo Thomas 
sev_platform_shutdown(int * error)5459b67d08dSRijo Thomas static int sev_platform_shutdown(int *error)
5469b67d08dSRijo Thomas {
5479b67d08dSRijo Thomas 	int rc;
5489b67d08dSRijo Thomas 
5499b67d08dSRijo Thomas 	mutex_lock(&sev_cmd_mutex);
5509b67d08dSRijo Thomas 	rc = __sev_platform_shutdown_locked(NULL);
5519b67d08dSRijo Thomas 	mutex_unlock(&sev_cmd_mutex);
5529b67d08dSRijo Thomas 
5539b67d08dSRijo Thomas 	return rc;
5549b67d08dSRijo Thomas }
5559b67d08dSRijo Thomas 
sev_get_platform_state(int * state,int * error)5569b67d08dSRijo Thomas static int sev_get_platform_state(int *state, int *error)
5579b67d08dSRijo Thomas {
55838103671SSean Christopherson 	struct sev_user_data_status data;
5599b67d08dSRijo Thomas 	int rc;
5609b67d08dSRijo Thomas 
56138103671SSean Christopherson 	rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, error);
5629b67d08dSRijo Thomas 	if (rc)
5639b67d08dSRijo Thomas 		return rc;
5649b67d08dSRijo Thomas 
56538103671SSean Christopherson 	*state = data.state;
5669b67d08dSRijo Thomas 	return rc;
5679b67d08dSRijo Thomas }
5689b67d08dSRijo Thomas 
sev_ioctl_do_reset(struct sev_issue_cmd * argp,bool writable)569b6102813SConnor Kuehl static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
5709b67d08dSRijo Thomas {
5719b67d08dSRijo Thomas 	int state, rc;
5729b67d08dSRijo Thomas 
573b6102813SConnor Kuehl 	if (!writable)
5749b67d08dSRijo Thomas 		return -EPERM;
5759b67d08dSRijo Thomas 
5769b67d08dSRijo Thomas 	/*
5779b67d08dSRijo Thomas 	 * The SEV spec requires that FACTORY_RESET must be issued in
5789b67d08dSRijo Thomas 	 * UNINIT state. Before we go further lets check if any guest is
5799b67d08dSRijo Thomas 	 * active.
5809b67d08dSRijo Thomas 	 *
5819b67d08dSRijo Thomas 	 * If FW is in WORKING state then deny the request otherwise issue
5829b67d08dSRijo Thomas 	 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
5839b67d08dSRijo Thomas 	 *
5849b67d08dSRijo Thomas 	 */
5859b67d08dSRijo Thomas 	rc = sev_get_platform_state(&state, &argp->error);
5869b67d08dSRijo Thomas 	if (rc)
5879b67d08dSRijo Thomas 		return rc;
5889b67d08dSRijo Thomas 
5899b67d08dSRijo Thomas 	if (state == SEV_STATE_WORKING)
5909b67d08dSRijo Thomas 		return -EBUSY;
5919b67d08dSRijo Thomas 
5929b67d08dSRijo Thomas 	if (state == SEV_STATE_INIT) {
5939b67d08dSRijo Thomas 		rc = __sev_platform_shutdown_locked(&argp->error);
5949b67d08dSRijo Thomas 		if (rc)
5959b67d08dSRijo Thomas 			return rc;
5969b67d08dSRijo Thomas 	}
5979b67d08dSRijo Thomas 
5989b67d08dSRijo Thomas 	return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
5999b67d08dSRijo Thomas }
6009b67d08dSRijo Thomas 
sev_ioctl_do_platform_status(struct sev_issue_cmd * argp)6019b67d08dSRijo Thomas static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
6029b67d08dSRijo Thomas {
60338103671SSean Christopherson 	struct sev_user_data_status data;
6049b67d08dSRijo Thomas 	int ret;
6059b67d08dSRijo Thomas 
60613dc15a3SJohn Allen 	memset(&data, 0, sizeof(data));
60713dc15a3SJohn Allen 
60838103671SSean Christopherson 	ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error);
6099b67d08dSRijo Thomas 	if (ret)
6109b67d08dSRijo Thomas 		return ret;
6119b67d08dSRijo Thomas 
61238103671SSean Christopherson 	if (copy_to_user((void __user *)argp->data, &data, sizeof(data)))
6139b67d08dSRijo Thomas 		ret = -EFAULT;
6149b67d08dSRijo Thomas 
6159b67d08dSRijo Thomas 	return ret;
6169b67d08dSRijo Thomas }
6179b67d08dSRijo Thomas 
sev_ioctl_do_pek_pdh_gen(int cmd,struct sev_issue_cmd * argp,bool writable)618b6102813SConnor Kuehl static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
6199b67d08dSRijo Thomas {
620b93566f1SRijo Thomas 	struct sev_device *sev = psp_master->sev_data;
6219b67d08dSRijo Thomas 	int rc;
6229b67d08dSRijo Thomas 
623b6102813SConnor Kuehl 	if (!writable)
6249b67d08dSRijo Thomas 		return -EPERM;
6259b67d08dSRijo Thomas 
626b93566f1SRijo Thomas 	if (sev->state == SEV_STATE_UNINIT) {
6279b67d08dSRijo Thomas 		rc = __sev_platform_init_locked(&argp->error);
6289b67d08dSRijo Thomas 		if (rc)
6299b67d08dSRijo Thomas 			return rc;
6309b67d08dSRijo Thomas 	}
6319b67d08dSRijo Thomas 
6329b67d08dSRijo Thomas 	return __sev_do_cmd_locked(cmd, NULL, &argp->error);
6339b67d08dSRijo Thomas }
6349b67d08dSRijo Thomas 
sev_ioctl_do_pek_csr(struct sev_issue_cmd * argp,bool writable)635b6102813SConnor Kuehl static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
6369b67d08dSRijo Thomas {
637b93566f1SRijo Thomas 	struct sev_device *sev = psp_master->sev_data;
6389b67d08dSRijo Thomas 	struct sev_user_data_pek_csr input;
639e4a9af79SSean Christopherson 	struct sev_data_pek_csr data;
640376bd28dSHerbert Xu 	void __user *input_address;
6419b67d08dSRijo Thomas 	void *blob = NULL;
6429b67d08dSRijo Thomas 	int ret;
6439b67d08dSRijo Thomas 
644b6102813SConnor Kuehl 	if (!writable)
6459b67d08dSRijo Thomas 		return -EPERM;
6469b67d08dSRijo Thomas 
6479b67d08dSRijo Thomas 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
6489b67d08dSRijo Thomas 		return -EFAULT;
6499b67d08dSRijo Thomas 
650e4a9af79SSean Christopherson 	memset(&data, 0, sizeof(data));
6519b67d08dSRijo Thomas 
6529b67d08dSRijo Thomas 	/* userspace wants to query CSR length */
6539b67d08dSRijo Thomas 	if (!input.address || !input.length)
6549b67d08dSRijo Thomas 		goto cmd;
6559b67d08dSRijo Thomas 
6569b67d08dSRijo Thomas 	/* allocate a physically contiguous buffer to store the CSR blob */
657376bd28dSHerbert Xu 	input_address = (void __user *)input.address;
658e4a9af79SSean Christopherson 	if (input.length > SEV_FW_BLOB_MAX_SIZE)
659e4a9af79SSean Christopherson 		return -EFAULT;
6609b67d08dSRijo Thomas 
66113dc15a3SJohn Allen 	blob = kzalloc(input.length, GFP_KERNEL);
662e4a9af79SSean Christopherson 	if (!blob)
663e4a9af79SSean Christopherson 		return -ENOMEM;
6649b67d08dSRijo Thomas 
665e4a9af79SSean Christopherson 	data.address = __psp_pa(blob);
666e4a9af79SSean Christopherson 	data.len = input.length;
6679b67d08dSRijo Thomas 
6689b67d08dSRijo Thomas cmd:
669b93566f1SRijo Thomas 	if (sev->state == SEV_STATE_UNINIT) {
6709b67d08dSRijo Thomas 		ret = __sev_platform_init_locked(&argp->error);
6719b67d08dSRijo Thomas 		if (ret)
6729b67d08dSRijo Thomas 			goto e_free_blob;
6739b67d08dSRijo Thomas 	}
6749b67d08dSRijo Thomas 
675e4a9af79SSean Christopherson 	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error);
6769b67d08dSRijo Thomas 
6779b67d08dSRijo Thomas 	 /* If we query the CSR length, FW responded with expected data. */
678e4a9af79SSean Christopherson 	input.length = data.len;
6799b67d08dSRijo Thomas 
6809b67d08dSRijo Thomas 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
6819b67d08dSRijo Thomas 		ret = -EFAULT;
6829b67d08dSRijo Thomas 		goto e_free_blob;
6839b67d08dSRijo Thomas 	}
6849b67d08dSRijo Thomas 
6859b67d08dSRijo Thomas 	if (blob) {
686376bd28dSHerbert Xu 		if (copy_to_user(input_address, blob, input.length))
6879b67d08dSRijo Thomas 			ret = -EFAULT;
6889b67d08dSRijo Thomas 	}
6899b67d08dSRijo Thomas 
6909b67d08dSRijo Thomas e_free_blob:
6919b67d08dSRijo Thomas 	kfree(blob);
6929b67d08dSRijo Thomas 	return ret;
6939b67d08dSRijo Thomas }
6949b67d08dSRijo Thomas 
psp_copy_user_blob(u64 uaddr,u32 len)695376bd28dSHerbert Xu void *psp_copy_user_blob(u64 uaddr, u32 len)
6969b67d08dSRijo Thomas {
6979b67d08dSRijo Thomas 	if (!uaddr || !len)
6989b67d08dSRijo Thomas 		return ERR_PTR(-EINVAL);
6999b67d08dSRijo Thomas 
7009b67d08dSRijo Thomas 	/* verify that blob length does not exceed our limit */
7019b67d08dSRijo Thomas 	if (len > SEV_FW_BLOB_MAX_SIZE)
7029b67d08dSRijo Thomas 		return ERR_PTR(-EINVAL);
7039b67d08dSRijo Thomas 
704376bd28dSHerbert Xu 	return memdup_user((void __user *)uaddr, len);
7059b67d08dSRijo Thomas }
7069b67d08dSRijo Thomas EXPORT_SYMBOL_GPL(psp_copy_user_blob);
7079b67d08dSRijo Thomas 
sev_get_api_version(void)7089b67d08dSRijo Thomas static int sev_get_api_version(void)
7099b67d08dSRijo Thomas {
710b93566f1SRijo Thomas 	struct sev_device *sev = psp_master->sev_data;
71138103671SSean Christopherson 	struct sev_user_data_status status;
7129b67d08dSRijo Thomas 	int error = 0, ret;
7139b67d08dSRijo Thomas 
71438103671SSean Christopherson 	ret = sev_platform_status(&status, &error);
7159b67d08dSRijo Thomas 	if (ret) {
716b93566f1SRijo Thomas 		dev_err(sev->dev,
7179b67d08dSRijo Thomas 			"SEV: failed to get status. Error: %#x\n", error);
7189b67d08dSRijo Thomas 		return 1;
7199b67d08dSRijo Thomas 	}
7209b67d08dSRijo Thomas 
72138103671SSean Christopherson 	sev->api_major = status.api_major;
72238103671SSean Christopherson 	sev->api_minor = status.api_minor;
72338103671SSean Christopherson 	sev->build = status.build;
72438103671SSean Christopherson 	sev->state = status.state;
7259b67d08dSRijo Thomas 
7269b67d08dSRijo Thomas 	return 0;
7279b67d08dSRijo Thomas }
7289b67d08dSRijo Thomas 
sev_get_firmware(struct device * dev,const struct firmware ** firmware)7299b67d08dSRijo Thomas static int sev_get_firmware(struct device *dev,
7309b67d08dSRijo Thomas 			    const struct firmware **firmware)
7319b67d08dSRijo Thomas {
7329b67d08dSRijo Thomas 	char fw_name_specific[SEV_FW_NAME_SIZE];
7339b67d08dSRijo Thomas 	char fw_name_subset[SEV_FW_NAME_SIZE];
7349b67d08dSRijo Thomas 
7359b67d08dSRijo Thomas 	snprintf(fw_name_specific, sizeof(fw_name_specific),
7369b67d08dSRijo Thomas 		 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
7379b67d08dSRijo Thomas 		 boot_cpu_data.x86, boot_cpu_data.x86_model);
7389b67d08dSRijo Thomas 
7399b67d08dSRijo Thomas 	snprintf(fw_name_subset, sizeof(fw_name_subset),
7409b67d08dSRijo Thomas 		 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
7419b67d08dSRijo Thomas 		 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
7429b67d08dSRijo Thomas 
7439b67d08dSRijo Thomas 	/* Check for SEV FW for a particular model.
7449b67d08dSRijo Thomas 	 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
7459b67d08dSRijo Thomas 	 *
7469b67d08dSRijo Thomas 	 * or
7479b67d08dSRijo Thomas 	 *
7489b67d08dSRijo Thomas 	 * Check for SEV FW common to a subset of models.
7499b67d08dSRijo Thomas 	 * Ex. amd_sev_fam17h_model0xh.sbin for
7509b67d08dSRijo Thomas 	 *     Family 17h Model 00h -- Family 17h Model 0Fh
7519b67d08dSRijo Thomas 	 *
7529b67d08dSRijo Thomas 	 * or
7539b67d08dSRijo Thomas 	 *
7549b67d08dSRijo Thomas 	 * Fall-back to using generic name: sev.fw
7559b67d08dSRijo Thomas 	 */
7569b67d08dSRijo Thomas 	if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
7579b67d08dSRijo Thomas 	    (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
7589b67d08dSRijo Thomas 	    (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
7599b67d08dSRijo Thomas 		return 0;
7609b67d08dSRijo Thomas 
7619b67d08dSRijo Thomas 	return -ENOENT;
7629b67d08dSRijo Thomas }
7639b67d08dSRijo Thomas 
7649b67d08dSRijo Thomas /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
sev_update_firmware(struct device * dev)7659b67d08dSRijo Thomas static int sev_update_firmware(struct device *dev)
7669b67d08dSRijo Thomas {
7679b67d08dSRijo Thomas 	struct sev_data_download_firmware *data;
7689b67d08dSRijo Thomas 	const struct firmware *firmware;
7699b67d08dSRijo Thomas 	int ret, error, order;
7709b67d08dSRijo Thomas 	struct page *p;
7719b67d08dSRijo Thomas 	u64 data_size;
7729b67d08dSRijo Thomas 
773b3b9fdf1SJarkko Sakkinen 	if (!sev_version_greater_or_equal(0, 15)) {
774b3b9fdf1SJarkko Sakkinen 		dev_dbg(dev, "DOWNLOAD_FIRMWARE not supported\n");
775b3b9fdf1SJarkko Sakkinen 		return -1;
776b3b9fdf1SJarkko Sakkinen 	}
777b3b9fdf1SJarkko Sakkinen 
7789b67d08dSRijo Thomas 	if (sev_get_firmware(dev, &firmware) == -ENOENT) {
7799b67d08dSRijo Thomas 		dev_dbg(dev, "No SEV firmware file present\n");
7809b67d08dSRijo Thomas 		return -1;
7819b67d08dSRijo Thomas 	}
7829b67d08dSRijo Thomas 
7839b67d08dSRijo Thomas 	/*
7849b67d08dSRijo Thomas 	 * SEV FW expects the physical address given to it to be 32
7859b67d08dSRijo Thomas 	 * byte aligned. Memory allocated has structure placed at the
7869b67d08dSRijo Thomas 	 * beginning followed by the firmware being passed to the SEV
7879b67d08dSRijo Thomas 	 * FW. Allocate enough memory for data structure + alignment
7889b67d08dSRijo Thomas 	 * padding + SEV FW.
7899b67d08dSRijo Thomas 	 */
7909b67d08dSRijo Thomas 	data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
7919b67d08dSRijo Thomas 
7929b67d08dSRijo Thomas 	order = get_order(firmware->size + data_size);
7939b67d08dSRijo Thomas 	p = alloc_pages(GFP_KERNEL, order);
7949b67d08dSRijo Thomas 	if (!p) {
7959b67d08dSRijo Thomas 		ret = -1;
7969b67d08dSRijo Thomas 		goto fw_err;
7979b67d08dSRijo Thomas 	}
7989b67d08dSRijo Thomas 
7999b67d08dSRijo Thomas 	/*
8009b67d08dSRijo Thomas 	 * Copy firmware data to a kernel allocated contiguous
8019b67d08dSRijo Thomas 	 * memory region.
8029b67d08dSRijo Thomas 	 */
8039b67d08dSRijo Thomas 	data = page_address(p);
8049b67d08dSRijo Thomas 	memcpy(page_address(p) + data_size, firmware->data, firmware->size);
8059b67d08dSRijo Thomas 
8069b67d08dSRijo Thomas 	data->address = __psp_pa(page_address(p) + data_size);
8079b67d08dSRijo Thomas 	data->len = firmware->size;
8089b67d08dSRijo Thomas 
8099b67d08dSRijo Thomas 	ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
810b3b9fdf1SJarkko Sakkinen 
811b3b9fdf1SJarkko Sakkinen 	/*
812b3b9fdf1SJarkko Sakkinen 	 * A quirk for fixing the committed TCB version, when upgrading from
813b3b9fdf1SJarkko Sakkinen 	 * earlier firmware version than 1.50.
814b3b9fdf1SJarkko Sakkinen 	 */
815b3b9fdf1SJarkko Sakkinen 	if (!ret && !sev_version_greater_or_equal(1, 50))
816b3b9fdf1SJarkko Sakkinen 		ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
817b3b9fdf1SJarkko Sakkinen 
8189b67d08dSRijo Thomas 	if (ret)
8199b67d08dSRijo Thomas 		dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
8209b67d08dSRijo Thomas 	else
8219b67d08dSRijo Thomas 		dev_info(dev, "SEV firmware update successful\n");
8229b67d08dSRijo Thomas 
8239b67d08dSRijo Thomas 	__free_pages(p, order);
8249b67d08dSRijo Thomas 
8259b67d08dSRijo Thomas fw_err:
8269b67d08dSRijo Thomas 	release_firmware(firmware);
8279b67d08dSRijo Thomas 
8289b67d08dSRijo Thomas 	return ret;
8299b67d08dSRijo Thomas }
8309b67d08dSRijo Thomas 
sev_ioctl_do_pek_import(struct sev_issue_cmd * argp,bool writable)831b6102813SConnor Kuehl static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
8329b67d08dSRijo Thomas {
833b93566f1SRijo Thomas 	struct sev_device *sev = psp_master->sev_data;
8349b67d08dSRijo Thomas 	struct sev_user_data_pek_cert_import input;
835e4a9af79SSean Christopherson 	struct sev_data_pek_cert_import data;
8369b67d08dSRijo Thomas 	void *pek_blob, *oca_blob;
8379b67d08dSRijo Thomas 	int ret;
8389b67d08dSRijo Thomas 
839b6102813SConnor Kuehl 	if (!writable)
8409b67d08dSRijo Thomas 		return -EPERM;
8419b67d08dSRijo Thomas 
8429b67d08dSRijo Thomas 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
8439b67d08dSRijo Thomas 		return -EFAULT;
8449b67d08dSRijo Thomas 
8459b67d08dSRijo Thomas 	/* copy PEK certificate blobs from userspace */
8469b67d08dSRijo Thomas 	pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
847e4a9af79SSean Christopherson 	if (IS_ERR(pek_blob))
848e4a9af79SSean Christopherson 		return PTR_ERR(pek_blob);
8499b67d08dSRijo Thomas 
850e4a9af79SSean Christopherson 	data.reserved = 0;
851e4a9af79SSean Christopherson 	data.pek_cert_address = __psp_pa(pek_blob);
852e4a9af79SSean Christopherson 	data.pek_cert_len = input.pek_cert_len;
8539b67d08dSRijo Thomas 
8549b67d08dSRijo Thomas 	/* copy PEK certificate blobs from userspace */
8559b67d08dSRijo Thomas 	oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
8569b67d08dSRijo Thomas 	if (IS_ERR(oca_blob)) {
8579b67d08dSRijo Thomas 		ret = PTR_ERR(oca_blob);
8589b67d08dSRijo Thomas 		goto e_free_pek;
8599b67d08dSRijo Thomas 	}
8609b67d08dSRijo Thomas 
861e4a9af79SSean Christopherson 	data.oca_cert_address = __psp_pa(oca_blob);
862e4a9af79SSean Christopherson 	data.oca_cert_len = input.oca_cert_len;
8639b67d08dSRijo Thomas 
8649b67d08dSRijo Thomas 	/* If platform is not in INIT state then transition it to INIT */
865b93566f1SRijo Thomas 	if (sev->state != SEV_STATE_INIT) {
8669b67d08dSRijo Thomas 		ret = __sev_platform_init_locked(&argp->error);
8679b67d08dSRijo Thomas 		if (ret)
8689b67d08dSRijo Thomas 			goto e_free_oca;
8699b67d08dSRijo Thomas 	}
8709b67d08dSRijo Thomas 
871e4a9af79SSean Christopherson 	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error);
8729b67d08dSRijo Thomas 
8739b67d08dSRijo Thomas e_free_oca:
8749b67d08dSRijo Thomas 	kfree(oca_blob);
8759b67d08dSRijo Thomas e_free_pek:
8769b67d08dSRijo Thomas 	kfree(pek_blob);
8779b67d08dSRijo Thomas 	return ret;
8789b67d08dSRijo Thomas }
8799b67d08dSRijo Thomas 
sev_ioctl_do_get_id2(struct sev_issue_cmd * argp)8809b67d08dSRijo Thomas static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
8819b67d08dSRijo Thomas {
8829b67d08dSRijo Thomas 	struct sev_user_data_get_id2 input;
883e4a9af79SSean Christopherson 	struct sev_data_get_id data;
884376bd28dSHerbert Xu 	void __user *input_address;
8859b67d08dSRijo Thomas 	void *id_blob = NULL;
8869b67d08dSRijo Thomas 	int ret;
8879b67d08dSRijo Thomas 
8889b67d08dSRijo Thomas 	/* SEV GET_ID is available from SEV API v0.16 and up */
8899b67d08dSRijo Thomas 	if (!sev_version_greater_or_equal(0, 16))
8909b67d08dSRijo Thomas 		return -ENOTSUPP;
8919b67d08dSRijo Thomas 
8929b67d08dSRijo Thomas 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
8939b67d08dSRijo Thomas 		return -EFAULT;
8949b67d08dSRijo Thomas 
895376bd28dSHerbert Xu 	input_address = (void __user *)input.address;
896376bd28dSHerbert Xu 
8979b67d08dSRijo Thomas 	if (input.address && input.length) {
89891dfd982SDavid Rientjes 		/*
89991dfd982SDavid Rientjes 		 * The length of the ID shouldn't be assumed by software since
90091dfd982SDavid Rientjes 		 * it may change in the future.  The allocation size is limited
90123baf831SKirill A. Shutemov 		 * to 1 << (PAGE_SHIFT + MAX_ORDER) by the page allocator.
90291dfd982SDavid Rientjes 		 * If the allocation fails, simply return ENOMEM rather than
90391dfd982SDavid Rientjes 		 * warning in the kernel log.
90491dfd982SDavid Rientjes 		 */
90591dfd982SDavid Rientjes 		id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);
906e4a9af79SSean Christopherson 		if (!id_blob)
9079b67d08dSRijo Thomas 			return -ENOMEM;
908e4a9af79SSean Christopherson 
909e4a9af79SSean Christopherson 		data.address = __psp_pa(id_blob);
910e4a9af79SSean Christopherson 		data.len = input.length;
911e4a9af79SSean Christopherson 	} else {
912e4a9af79SSean Christopherson 		data.address = 0;
913e4a9af79SSean Christopherson 		data.len = 0;
9149b67d08dSRijo Thomas 	}
9159b67d08dSRijo Thomas 
916e4a9af79SSean Christopherson 	ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error);
9179b67d08dSRijo Thomas 
9189b67d08dSRijo Thomas 	/*
9199b67d08dSRijo Thomas 	 * Firmware will return the length of the ID value (either the minimum
9209b67d08dSRijo Thomas 	 * required length or the actual length written), return it to the user.
9219b67d08dSRijo Thomas 	 */
922e4a9af79SSean Christopherson 	input.length = data.len;
9239b67d08dSRijo Thomas 
9249b67d08dSRijo Thomas 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
9259b67d08dSRijo Thomas 		ret = -EFAULT;
9269b67d08dSRijo Thomas 		goto e_free;
9279b67d08dSRijo Thomas 	}
9289b67d08dSRijo Thomas 
9299b67d08dSRijo Thomas 	if (id_blob) {
930e4a9af79SSean Christopherson 		if (copy_to_user(input_address, id_blob, data.len)) {
9319b67d08dSRijo Thomas 			ret = -EFAULT;
9329b67d08dSRijo Thomas 			goto e_free;
9339b67d08dSRijo Thomas 		}
9349b67d08dSRijo Thomas 	}
9359b67d08dSRijo Thomas 
9369b67d08dSRijo Thomas e_free:
9379b67d08dSRijo Thomas 	kfree(id_blob);
9389b67d08dSRijo Thomas 
9399b67d08dSRijo Thomas 	return ret;
9409b67d08dSRijo Thomas }
9419b67d08dSRijo Thomas 
sev_ioctl_do_get_id(struct sev_issue_cmd * argp)9429b67d08dSRijo Thomas static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
9439b67d08dSRijo Thomas {
9449b67d08dSRijo Thomas 	struct sev_data_get_id *data;
9459b67d08dSRijo Thomas 	u64 data_size, user_size;
9469b67d08dSRijo Thomas 	void *id_blob, *mem;
9479b67d08dSRijo Thomas 	int ret;
9489b67d08dSRijo Thomas 
9499b67d08dSRijo Thomas 	/* SEV GET_ID available from SEV API v0.16 and up */
9509b67d08dSRijo Thomas 	if (!sev_version_greater_or_equal(0, 16))
9519b67d08dSRijo Thomas 		return -ENOTSUPP;
9529b67d08dSRijo Thomas 
9539b67d08dSRijo Thomas 	/* SEV FW expects the buffer it fills with the ID to be
9549b67d08dSRijo Thomas 	 * 8-byte aligned. Memory allocated should be enough to
9559b67d08dSRijo Thomas 	 * hold data structure + alignment padding + memory
9569b67d08dSRijo Thomas 	 * where SEV FW writes the ID.
9579b67d08dSRijo Thomas 	 */
9589b67d08dSRijo Thomas 	data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
9599b67d08dSRijo Thomas 	user_size = sizeof(struct sev_user_data_get_id);
9609b67d08dSRijo Thomas 
9619b67d08dSRijo Thomas 	mem = kzalloc(data_size + user_size, GFP_KERNEL);
9629b67d08dSRijo Thomas 	if (!mem)
9639b67d08dSRijo Thomas 		return -ENOMEM;
9649b67d08dSRijo Thomas 
9659b67d08dSRijo Thomas 	data = mem;
9669b67d08dSRijo Thomas 	id_blob = mem + data_size;
9679b67d08dSRijo Thomas 
9689b67d08dSRijo Thomas 	data->address = __psp_pa(id_blob);
9699b67d08dSRijo Thomas 	data->len = user_size;
9709b67d08dSRijo Thomas 
9719b67d08dSRijo Thomas 	ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
9729b67d08dSRijo Thomas 	if (!ret) {
9739b67d08dSRijo Thomas 		if (copy_to_user((void __user *)argp->data, id_blob, data->len))
9749b67d08dSRijo Thomas 			ret = -EFAULT;
9759b67d08dSRijo Thomas 	}
9769b67d08dSRijo Thomas 
9779b67d08dSRijo Thomas 	kfree(mem);
9789b67d08dSRijo Thomas 
9799b67d08dSRijo Thomas 	return ret;
9809b67d08dSRijo Thomas }
9819b67d08dSRijo Thomas 
sev_ioctl_do_pdh_export(struct sev_issue_cmd * argp,bool writable)982b6102813SConnor Kuehl static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
9839b67d08dSRijo Thomas {
984b93566f1SRijo Thomas 	struct sev_device *sev = psp_master->sev_data;
9859b67d08dSRijo Thomas 	struct sev_user_data_pdh_cert_export input;
9869b67d08dSRijo Thomas 	void *pdh_blob = NULL, *cert_blob = NULL;
987e4a9af79SSean Christopherson 	struct sev_data_pdh_cert_export data;
988376bd28dSHerbert Xu 	void __user *input_cert_chain_address;
989376bd28dSHerbert Xu 	void __user *input_pdh_cert_address;
9909b67d08dSRijo Thomas 	int ret;
9919b67d08dSRijo Thomas 
9929b67d08dSRijo Thomas 	/* If platform is not in INIT state then transition it to INIT. */
993b93566f1SRijo Thomas 	if (sev->state != SEV_STATE_INIT) {
994b6102813SConnor Kuehl 		if (!writable)
9959b67d08dSRijo Thomas 			return -EPERM;
9969b67d08dSRijo Thomas 
9979b67d08dSRijo Thomas 		ret = __sev_platform_init_locked(&argp->error);
9989b67d08dSRijo Thomas 		if (ret)
9999b67d08dSRijo Thomas 			return ret;
10009b67d08dSRijo Thomas 	}
10019b67d08dSRijo Thomas 
10029b67d08dSRijo Thomas 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
10039b67d08dSRijo Thomas 		return -EFAULT;
10049b67d08dSRijo Thomas 
1005e4a9af79SSean Christopherson 	memset(&data, 0, sizeof(data));
10069b67d08dSRijo Thomas 
10079b67d08dSRijo Thomas 	/* Userspace wants to query the certificate length. */
10089b67d08dSRijo Thomas 	if (!input.pdh_cert_address ||
10099b67d08dSRijo Thomas 	    !input.pdh_cert_len ||
10109b67d08dSRijo Thomas 	    !input.cert_chain_address)
10119b67d08dSRijo Thomas 		goto cmd;
10129b67d08dSRijo Thomas 
1013376bd28dSHerbert Xu 	input_pdh_cert_address = (void __user *)input.pdh_cert_address;
1014376bd28dSHerbert Xu 	input_cert_chain_address = (void __user *)input.cert_chain_address;
1015376bd28dSHerbert Xu 
10169b67d08dSRijo Thomas 	/* Allocate a physically contiguous buffer to store the PDH blob. */
1017e4a9af79SSean Christopherson 	if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE)
1018e4a9af79SSean Christopherson 		return -EFAULT;
10199b67d08dSRijo Thomas 
10209b67d08dSRijo Thomas 	/* Allocate a physically contiguous buffer to store the cert chain blob. */
1021e4a9af79SSean Christopherson 	if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE)
1022e4a9af79SSean Christopherson 		return -EFAULT;
10239b67d08dSRijo Thomas 
102413dc15a3SJohn Allen 	pdh_blob = kzalloc(input.pdh_cert_len, GFP_KERNEL);
1025e4a9af79SSean Christopherson 	if (!pdh_blob)
1026e4a9af79SSean Christopherson 		return -ENOMEM;
10279b67d08dSRijo Thomas 
1028e4a9af79SSean Christopherson 	data.pdh_cert_address = __psp_pa(pdh_blob);
1029e4a9af79SSean Christopherson 	data.pdh_cert_len = input.pdh_cert_len;
10309b67d08dSRijo Thomas 
103113dc15a3SJohn Allen 	cert_blob = kzalloc(input.cert_chain_len, GFP_KERNEL);
10329b67d08dSRijo Thomas 	if (!cert_blob) {
10339b67d08dSRijo Thomas 		ret = -ENOMEM;
10349b67d08dSRijo Thomas 		goto e_free_pdh;
10359b67d08dSRijo Thomas 	}
10369b67d08dSRijo Thomas 
1037e4a9af79SSean Christopherson 	data.cert_chain_address = __psp_pa(cert_blob);
1038e4a9af79SSean Christopherson 	data.cert_chain_len = input.cert_chain_len;
10399b67d08dSRijo Thomas 
10409b67d08dSRijo Thomas cmd:
1041e4a9af79SSean Christopherson 	ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error);
10429b67d08dSRijo Thomas 
10439b67d08dSRijo Thomas 	/* If we query the length, FW responded with expected data. */
1044e4a9af79SSean Christopherson 	input.cert_chain_len = data.cert_chain_len;
1045e4a9af79SSean Christopherson 	input.pdh_cert_len = data.pdh_cert_len;
10469b67d08dSRijo Thomas 
10479b67d08dSRijo Thomas 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
10489b67d08dSRijo Thomas 		ret = -EFAULT;
10499b67d08dSRijo Thomas 		goto e_free_cert;
10509b67d08dSRijo Thomas 	}
10519b67d08dSRijo Thomas 
10529b67d08dSRijo Thomas 	if (pdh_blob) {
1053376bd28dSHerbert Xu 		if (copy_to_user(input_pdh_cert_address,
10549b67d08dSRijo Thomas 				 pdh_blob, input.pdh_cert_len)) {
10559b67d08dSRijo Thomas 			ret = -EFAULT;
10569b67d08dSRijo Thomas 			goto e_free_cert;
10579b67d08dSRijo Thomas 		}
10589b67d08dSRijo Thomas 	}
10599b67d08dSRijo Thomas 
10609b67d08dSRijo Thomas 	if (cert_blob) {
1061376bd28dSHerbert Xu 		if (copy_to_user(input_cert_chain_address,
10629b67d08dSRijo Thomas 				 cert_blob, input.cert_chain_len))
10639b67d08dSRijo Thomas 			ret = -EFAULT;
10649b67d08dSRijo Thomas 	}
10659b67d08dSRijo Thomas 
10669b67d08dSRijo Thomas e_free_cert:
10679b67d08dSRijo Thomas 	kfree(cert_blob);
10689b67d08dSRijo Thomas e_free_pdh:
10699b67d08dSRijo Thomas 	kfree(pdh_blob);
10709b67d08dSRijo Thomas 	return ret;
10719b67d08dSRijo Thomas }
10729b67d08dSRijo Thomas 
sev_ioctl(struct file * file,unsigned int ioctl,unsigned long arg)10739b67d08dSRijo Thomas static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
10749b67d08dSRijo Thomas {
10759b67d08dSRijo Thomas 	void __user *argp = (void __user *)arg;
10769b67d08dSRijo Thomas 	struct sev_issue_cmd input;
10779b67d08dSRijo Thomas 	int ret = -EFAULT;
1078b6102813SConnor Kuehl 	bool writable = file->f_mode & FMODE_WRITE;
10799b67d08dSRijo Thomas 
1080b93566f1SRijo Thomas 	if (!psp_master || !psp_master->sev_data)
10819b67d08dSRijo Thomas 		return -ENODEV;
10829b67d08dSRijo Thomas 
10839b67d08dSRijo Thomas 	if (ioctl != SEV_ISSUE_CMD)
10849b67d08dSRijo Thomas 		return -EINVAL;
10859b67d08dSRijo Thomas 
10869b67d08dSRijo Thomas 	if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
10879b67d08dSRijo Thomas 		return -EFAULT;
10889b67d08dSRijo Thomas 
10899b67d08dSRijo Thomas 	if (input.cmd > SEV_MAX)
10909b67d08dSRijo Thomas 		return -EINVAL;
10919b67d08dSRijo Thomas 
10929b67d08dSRijo Thomas 	mutex_lock(&sev_cmd_mutex);
10939b67d08dSRijo Thomas 
10949b67d08dSRijo Thomas 	switch (input.cmd) {
10959b67d08dSRijo Thomas 
10969b67d08dSRijo Thomas 	case SEV_FACTORY_RESET:
1097b6102813SConnor Kuehl 		ret = sev_ioctl_do_reset(&input, writable);
10989b67d08dSRijo Thomas 		break;
10999b67d08dSRijo Thomas 	case SEV_PLATFORM_STATUS:
11009b67d08dSRijo Thomas 		ret = sev_ioctl_do_platform_status(&input);
11019b67d08dSRijo Thomas 		break;
11029b67d08dSRijo Thomas 	case SEV_PEK_GEN:
1103b6102813SConnor Kuehl 		ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
11049b67d08dSRijo Thomas 		break;
11059b67d08dSRijo Thomas 	case SEV_PDH_GEN:
1106b6102813SConnor Kuehl 		ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
11079b67d08dSRijo Thomas 		break;
11089b67d08dSRijo Thomas 	case SEV_PEK_CSR:
1109b6102813SConnor Kuehl 		ret = sev_ioctl_do_pek_csr(&input, writable);
11109b67d08dSRijo Thomas 		break;
11119b67d08dSRijo Thomas 	case SEV_PEK_CERT_IMPORT:
1112b6102813SConnor Kuehl 		ret = sev_ioctl_do_pek_import(&input, writable);
11139b67d08dSRijo Thomas 		break;
11149b67d08dSRijo Thomas 	case SEV_PDH_CERT_EXPORT:
1115b6102813SConnor Kuehl 		ret = sev_ioctl_do_pdh_export(&input, writable);
11169b67d08dSRijo Thomas 		break;
11179b67d08dSRijo Thomas 	case SEV_GET_ID:
11189b67d08dSRijo Thomas 		pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
11199b67d08dSRijo Thomas 		ret = sev_ioctl_do_get_id(&input);
11209b67d08dSRijo Thomas 		break;
11219b67d08dSRijo Thomas 	case SEV_GET_ID2:
11229b67d08dSRijo Thomas 		ret = sev_ioctl_do_get_id2(&input);
11239b67d08dSRijo Thomas 		break;
11249b67d08dSRijo Thomas 	default:
11259b67d08dSRijo Thomas 		ret = -EINVAL;
11269b67d08dSRijo Thomas 		goto out;
11279b67d08dSRijo Thomas 	}
11289b67d08dSRijo Thomas 
11299b67d08dSRijo Thomas 	if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
11309b67d08dSRijo Thomas 		ret = -EFAULT;
11319b67d08dSRijo Thomas out:
11329b67d08dSRijo Thomas 	mutex_unlock(&sev_cmd_mutex);
11339b67d08dSRijo Thomas 
11349b67d08dSRijo Thomas 	return ret;
11359b67d08dSRijo Thomas }
11369b67d08dSRijo Thomas 
11379b67d08dSRijo Thomas static const struct file_operations sev_fops = {
11389b67d08dSRijo Thomas 	.owner	= THIS_MODULE,
11399b67d08dSRijo Thomas 	.unlocked_ioctl = sev_ioctl,
11409b67d08dSRijo Thomas };
11419b67d08dSRijo Thomas 
sev_platform_status(struct sev_user_data_status * data,int * error)11429b67d08dSRijo Thomas int sev_platform_status(struct sev_user_data_status *data, int *error)
11439b67d08dSRijo Thomas {
11449b67d08dSRijo Thomas 	return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
11459b67d08dSRijo Thomas }
11469b67d08dSRijo Thomas EXPORT_SYMBOL_GPL(sev_platform_status);
11479b67d08dSRijo Thomas 
sev_guest_deactivate(struct sev_data_deactivate * data,int * error)11489b67d08dSRijo Thomas int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
11499b67d08dSRijo Thomas {
11509b67d08dSRijo Thomas 	return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
11519b67d08dSRijo Thomas }
11529b67d08dSRijo Thomas EXPORT_SYMBOL_GPL(sev_guest_deactivate);
11539b67d08dSRijo Thomas 
sev_guest_activate(struct sev_data_activate * data,int * error)11549b67d08dSRijo Thomas int sev_guest_activate(struct sev_data_activate *data, int *error)
11559b67d08dSRijo Thomas {
11569b67d08dSRijo Thomas 	return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
11579b67d08dSRijo Thomas }
11589b67d08dSRijo Thomas EXPORT_SYMBOL_GPL(sev_guest_activate);
11599b67d08dSRijo Thomas 
sev_guest_decommission(struct sev_data_decommission * data,int * error)11609b67d08dSRijo Thomas int sev_guest_decommission(struct sev_data_decommission *data, int *error)
11619b67d08dSRijo Thomas {
11629b67d08dSRijo Thomas 	return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
11639b67d08dSRijo Thomas }
11649b67d08dSRijo Thomas EXPORT_SYMBOL_GPL(sev_guest_decommission);
11659b67d08dSRijo Thomas 
sev_guest_df_flush(int * error)11669b67d08dSRijo Thomas int sev_guest_df_flush(int *error)
11679b67d08dSRijo Thomas {
11689b67d08dSRijo Thomas 	return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
11699b67d08dSRijo Thomas }
11709b67d08dSRijo Thomas EXPORT_SYMBOL_GPL(sev_guest_df_flush);
11719b67d08dSRijo Thomas 
sev_exit(struct kref * ref)11729b67d08dSRijo Thomas static void sev_exit(struct kref *ref)
11739b67d08dSRijo Thomas {
11749b67d08dSRijo Thomas 	misc_deregister(&misc_dev->misc);
11751f14b57fSJohn Allen 	kfree(misc_dev);
11761f14b57fSJohn Allen 	misc_dev = NULL;
11779b67d08dSRijo Thomas }
11789b67d08dSRijo Thomas 
sev_misc_init(struct sev_device * sev)1179b93566f1SRijo Thomas static int sev_misc_init(struct sev_device *sev)
11809b67d08dSRijo Thomas {
1181b93566f1SRijo Thomas 	struct device *dev = sev->dev;
11829b67d08dSRijo Thomas 	int ret;
11839b67d08dSRijo Thomas 
11849b67d08dSRijo Thomas 	/*
11859b67d08dSRijo Thomas 	 * SEV feature support can be detected on multiple devices but the SEV
11869b67d08dSRijo Thomas 	 * FW commands must be issued on the master. During probe, we do not
11879b67d08dSRijo Thomas 	 * know the master hence we create /dev/sev on the first device probe.
11889b67d08dSRijo Thomas 	 * sev_do_cmd() finds the right master device to which to issue the
11899b67d08dSRijo Thomas 	 * command to the firmware.
11909b67d08dSRijo Thomas 	 */
11919b67d08dSRijo Thomas 	if (!misc_dev) {
11929b67d08dSRijo Thomas 		struct miscdevice *misc;
11939b67d08dSRijo Thomas 
11941f14b57fSJohn Allen 		misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
11959b67d08dSRijo Thomas 		if (!misc_dev)
11969b67d08dSRijo Thomas 			return -ENOMEM;
11979b67d08dSRijo Thomas 
11989b67d08dSRijo Thomas 		misc = &misc_dev->misc;
11999b67d08dSRijo Thomas 		misc->minor = MISC_DYNAMIC_MINOR;
12009b67d08dSRijo Thomas 		misc->name = DEVICE_NAME;
12019b67d08dSRijo Thomas 		misc->fops = &sev_fops;
12029b67d08dSRijo Thomas 
12039b67d08dSRijo Thomas 		ret = misc_register(misc);
12049b67d08dSRijo Thomas 		if (ret)
12059b67d08dSRijo Thomas 			return ret;
12069b67d08dSRijo Thomas 
12079b67d08dSRijo Thomas 		kref_init(&misc_dev->refcount);
12089b67d08dSRijo Thomas 	} else {
12099b67d08dSRijo Thomas 		kref_get(&misc_dev->refcount);
12109b67d08dSRijo Thomas 	}
12119b67d08dSRijo Thomas 
1212b93566f1SRijo Thomas 	init_waitqueue_head(&sev->int_queue);
1213b93566f1SRijo Thomas 	sev->misc = misc_dev;
12149b67d08dSRijo Thomas 	dev_dbg(dev, "registered SEV device\n");
12159b67d08dSRijo Thomas 
12169b67d08dSRijo Thomas 	return 0;
12179b67d08dSRijo Thomas }
12189b67d08dSRijo Thomas 
sev_dev_init(struct psp_device * psp)1219b93566f1SRijo Thomas int sev_dev_init(struct psp_device *psp)
12209b67d08dSRijo Thomas {
1221b93566f1SRijo Thomas 	struct device *dev = psp->dev;
1222b93566f1SRijo Thomas 	struct sev_device *sev;
1223b93566f1SRijo Thomas 	int ret = -ENOMEM;
12249b67d08dSRijo Thomas 
12251877c73bSTom Lendacky 	if (!boot_cpu_has(X86_FEATURE_SEV)) {
12261877c73bSTom Lendacky 		dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n");
12271877c73bSTom Lendacky 		return 0;
12281877c73bSTom Lendacky 	}
12291877c73bSTom Lendacky 
1230b93566f1SRijo Thomas 	sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
1231b93566f1SRijo Thomas 	if (!sev)
12329b67d08dSRijo Thomas 		goto e_err;
12339b67d08dSRijo Thomas 
12348347b994SSean Christopherson 	sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
12358347b994SSean Christopherson 	if (!sev->cmd_buf)
12368347b994SSean Christopherson 		goto e_sev;
12378347b994SSean Christopherson 
1238b93566f1SRijo Thomas 	psp->sev_data = sev;
12399b67d08dSRijo Thomas 
1240b93566f1SRijo Thomas 	sev->dev = dev;
1241b93566f1SRijo Thomas 	sev->psp = psp;
12429b67d08dSRijo Thomas 
1243b93566f1SRijo Thomas 	sev->io_regs = psp->io_regs;
12449b67d08dSRijo Thomas 
12456eb0cc72SRijo Thomas 	sev->vdata = (struct sev_vdata *)psp->vdata->sev;
12466eb0cc72SRijo Thomas 	if (!sev->vdata) {
12476eb0cc72SRijo Thomas 		ret = -ENODEV;
12486eb0cc72SRijo Thomas 		dev_err(dev, "sev: missing driver data\n");
12498347b994SSean Christopherson 		goto e_buf;
12506eb0cc72SRijo Thomas 	}
12516eb0cc72SRijo Thomas 
1252b93566f1SRijo Thomas 	psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
12539b67d08dSRijo Thomas 
1254b93566f1SRijo Thomas 	ret = sev_misc_init(sev);
12559b67d08dSRijo Thomas 	if (ret)
12569b67d08dSRijo Thomas 		goto e_irq;
12579b67d08dSRijo Thomas 
1258b93566f1SRijo Thomas 	dev_notice(dev, "sev enabled\n");
12599b67d08dSRijo Thomas 
12609b67d08dSRijo Thomas 	return 0;
12619b67d08dSRijo Thomas 
12629b67d08dSRijo Thomas e_irq:
1263b93566f1SRijo Thomas 	psp_clear_sev_irq_handler(psp);
12648347b994SSean Christopherson e_buf:
12658347b994SSean Christopherson 	devm_free_pages(dev, (unsigned long)sev->cmd_buf);
1266b61a9071SSean Christopherson e_sev:
1267b61a9071SSean Christopherson 	devm_kfree(dev, sev);
12689b67d08dSRijo Thomas e_err:
1269b93566f1SRijo Thomas 	psp->sev_data = NULL;
12709b67d08dSRijo Thomas 
1271b93566f1SRijo Thomas 	dev_notice(dev, "sev initialization failed\n");
12729b67d08dSRijo Thomas 
12739b67d08dSRijo Thomas 	return ret;
12749b67d08dSRijo Thomas }
12759b67d08dSRijo Thomas 
sev_firmware_shutdown(struct sev_device * sev)12765441a07aSBrijesh Singh static void sev_firmware_shutdown(struct sev_device *sev)
12775441a07aSBrijesh Singh {
12785441a07aSBrijesh Singh 	sev_platform_shutdown(NULL);
12795441a07aSBrijesh Singh 
12805441a07aSBrijesh Singh 	if (sev_es_tmr) {
12815441a07aSBrijesh Singh 		/* The TMR area was encrypted, flush it from the cache */
12825441a07aSBrijesh Singh 		wbinvd_on_all_cpus();
12835441a07aSBrijesh Singh 
12845441a07aSBrijesh Singh 		free_pages((unsigned long)sev_es_tmr,
12855441a07aSBrijesh Singh 			   get_order(SEV_ES_TMR_SIZE));
12865441a07aSBrijesh Singh 		sev_es_tmr = NULL;
12875441a07aSBrijesh Singh 	}
12883d725965SDavid Rientjes 
12893d725965SDavid Rientjes 	if (sev_init_ex_buffer) {
12903d725965SDavid Rientjes 		free_pages((unsigned long)sev_init_ex_buffer,
12913d725965SDavid Rientjes 			   get_order(NV_LENGTH));
12923d725965SDavid Rientjes 		sev_init_ex_buffer = NULL;
12933d725965SDavid Rientjes 	}
12945441a07aSBrijesh Singh }
12955441a07aSBrijesh Singh 
sev_dev_destroy(struct psp_device * psp)1296b93566f1SRijo Thomas void sev_dev_destroy(struct psp_device *psp)
12979b67d08dSRijo Thomas {
1298b93566f1SRijo Thomas 	struct sev_device *sev = psp->sev_data;
12999b67d08dSRijo Thomas 
1300b93566f1SRijo Thomas 	if (!sev)
13019b67d08dSRijo Thomas 		return;
13029b67d08dSRijo Thomas 
13035441a07aSBrijesh Singh 	sev_firmware_shutdown(sev);
13045441a07aSBrijesh Singh 
1305b93566f1SRijo Thomas 	if (sev->misc)
13069b67d08dSRijo Thomas 		kref_put(&misc_dev->refcount, sev_exit);
13079b67d08dSRijo Thomas 
1308b93566f1SRijo Thomas 	psp_clear_sev_irq_handler(psp);
13099b67d08dSRijo Thomas }
13109b67d08dSRijo Thomas 
sev_issue_cmd_external_user(struct file * filep,unsigned int cmd,void * data,int * error)13119b67d08dSRijo Thomas int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
13129b67d08dSRijo Thomas 				void *data, int *error)
13139b67d08dSRijo Thomas {
13149b67d08dSRijo Thomas 	if (!filep || filep->f_op != &sev_fops)
13159b67d08dSRijo Thomas 		return -EBADF;
13169b67d08dSRijo Thomas 
13179b67d08dSRijo Thomas 	return sev_do_cmd(cmd, data, error);
13189b67d08dSRijo Thomas }
13199b67d08dSRijo Thomas EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
13209b67d08dSRijo Thomas 
sev_pci_init(void)1321b93566f1SRijo Thomas void sev_pci_init(void)
13229b67d08dSRijo Thomas {
1323b93566f1SRijo Thomas 	struct sev_device *sev = psp_master->sev_data;
13249b67d08dSRijo Thomas 	int error, rc;
13259b67d08dSRijo Thomas 
1326b93566f1SRijo Thomas 	if (!sev)
13279b67d08dSRijo Thomas 		return;
13289b67d08dSRijo Thomas 
13299b67d08dSRijo Thomas 	psp_timeout = psp_probe_timeout;
13309b67d08dSRijo Thomas 
13319b67d08dSRijo Thomas 	if (sev_get_api_version())
13329b67d08dSRijo Thomas 		goto err;
13339b67d08dSRijo Thomas 
1334b3b9fdf1SJarkko Sakkinen 	if (sev_update_firmware(sev->dev) == 0)
13359b67d08dSRijo Thomas 		sev_get_api_version();
13369b67d08dSRijo Thomas 
13373d725965SDavid Rientjes 	/* If an init_ex_path is provided rely on INIT_EX for PSP initialization
13383d725965SDavid Rientjes 	 * instead of INIT.
13393d725965SDavid Rientjes 	 */
13403d725965SDavid Rientjes 	if (init_ex_path) {
13413d725965SDavid Rientjes 		sev_init_ex_buffer = sev_fw_alloc(NV_LENGTH);
13423d725965SDavid Rientjes 		if (!sev_init_ex_buffer) {
13433d725965SDavid Rientjes 			dev_err(sev->dev,
13443d725965SDavid Rientjes 				"SEV: INIT_EX NV memory allocation failed\n");
13453d725965SDavid Rientjes 			goto err;
13463d725965SDavid Rientjes 		}
13473d725965SDavid Rientjes 	}
13483d725965SDavid Rientjes 
134997f9ac3dSTom Lendacky 	/* Obtain the TMR memory area for SEV-ES use */
1350cc17982dSPeter Gonda 	sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE);
135146a334a9STom Lendacky 	if (sev_es_tmr)
135246a334a9STom Lendacky 		/* Must flush the cache before giving it to the firmware */
135346a334a9STom Lendacky 		clflush_cache_range(sev_es_tmr, SEV_ES_TMR_SIZE);
135446a334a9STom Lendacky 	else
135597f9ac3dSTom Lendacky 		dev_warn(sev->dev,
135697f9ac3dSTom Lendacky 			 "SEV: TMR allocation failed, SEV-ES support unavailable\n");
135797f9ac3dSTom Lendacky 
1358b64fa5fcSPeter Gonda 	if (!psp_init_on_probe)
1359b64fa5fcSPeter Gonda 		return;
1360b64fa5fcSPeter Gonda 
13619b67d08dSRijo Thomas 	/* Initialize the platform */
13629b67d08dSRijo Thomas 	rc = sev_platform_init(&error);
1363b64fa5fcSPeter Gonda 	if (rc)
1364c8341ac6SPeter Gonda 		dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n",
1365c8341ac6SPeter Gonda 			error, rc);
13669b67d08dSRijo Thomas 
13679b67d08dSRijo Thomas 	return;
13689b67d08dSRijo Thomas 
13699b67d08dSRijo Thomas err:
1370b93566f1SRijo Thomas 	psp_master->sev_data = NULL;
13719b67d08dSRijo Thomas }
13729b67d08dSRijo Thomas 
sev_pci_exit(void)1373b93566f1SRijo Thomas void sev_pci_exit(void)
13749b67d08dSRijo Thomas {
13755441a07aSBrijesh Singh 	struct sev_device *sev = psp_master->sev_data;
13765441a07aSBrijesh Singh 
13775441a07aSBrijesh Singh 	if (!sev)
13789b67d08dSRijo Thomas 		return;
13799b67d08dSRijo Thomas 
13805441a07aSBrijesh Singh 	sev_firmware_shutdown(sev);
13819b67d08dSRijo Thomas }
1382