xref: /linux-6.15/drivers/crypto/ccp/sev-dev.c (revision 90cb3ca2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AMD Secure Encrypted Virtualization (SEV) interface
4  *
5  * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
6  *
7  * Author: Brijesh Singh <[email protected]>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/kthread.h>
13 #include <linux/sched.h>
14 #include <linux/interrupt.h>
15 #include <linux/spinlock.h>
16 #include <linux/spinlock_types.h>
17 #include <linux/types.h>
18 #include <linux/mutex.h>
19 #include <linux/delay.h>
20 #include <linux/hw_random.h>
21 #include <linux/ccp.h>
22 #include <linux/firmware.h>
23 #include <linux/gfp.h>
24 #include <linux/cpufeature.h>
25 #include <linux/fs.h>
26 #include <linux/fs_struct.h>
27 
28 #include <asm/smp.h>
29 
30 #include "psp-dev.h"
31 #include "sev-dev.h"
32 
33 #define DEVICE_NAME		"sev"
34 #define SEV_FW_FILE		"amd/sev.fw"
35 #define SEV_FW_NAME_SIZE	64
36 
37 static DEFINE_MUTEX(sev_cmd_mutex);
38 static struct sev_misc_dev *misc_dev;
39 
40 static int psp_cmd_timeout = 100;
41 module_param(psp_cmd_timeout, int, 0644);
42 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
43 
44 static int psp_probe_timeout = 5;
45 module_param(psp_probe_timeout, int, 0644);
46 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
47 
48 static char *init_ex_path;
49 module_param(init_ex_path, charp, 0444);
50 MODULE_PARM_DESC(init_ex_path, " Path for INIT_EX data; if set try INIT_EX");
51 
52 static bool psp_init_on_probe = true;
53 module_param(psp_init_on_probe, bool, 0444);
54 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");
55 
56 MODULE_FIRMWARE("amd/amd_sev_fam17h_model0xh.sbin"); /* 1st gen EPYC */
57 MODULE_FIRMWARE("amd/amd_sev_fam17h_model3xh.sbin"); /* 2nd gen EPYC */
58 MODULE_FIRMWARE("amd/amd_sev_fam19h_model0xh.sbin"); /* 3rd gen EPYC */
59 
60 static bool psp_dead;
61 static int psp_timeout;
62 
63 /* Trusted Memory Region (TMR):
64  *   The TMR is a 1MB area that must be 1MB aligned.  Use the page allocator
65  *   to allocate the memory, which will return aligned memory for the specified
66  *   allocation order.
67  */
68 #define SEV_ES_TMR_SIZE		(1024 * 1024)
69 static void *sev_es_tmr;
70 
71 /* INIT_EX NV Storage:
72  *   The NV Storage is a 32Kb area and must be 4Kb page aligned.  Use the page
73  *   allocator to allocate the memory, which will return aligned memory for the
74  *   specified allocation order.
75  */
76 #define NV_LENGTH (32 * 1024)
77 static void *sev_init_ex_buffer;
78 
79 static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
80 {
81 	struct sev_device *sev = psp_master->sev_data;
82 
83 	if (sev->api_major > maj)
84 		return true;
85 
86 	if (sev->api_major == maj && sev->api_minor >= min)
87 		return true;
88 
89 	return false;
90 }
91 
92 static void sev_irq_handler(int irq, void *data, unsigned int status)
93 {
94 	struct sev_device *sev = data;
95 	int reg;
96 
97 	/* Check if it is command completion: */
98 	if (!(status & SEV_CMD_COMPLETE))
99 		return;
100 
101 	/* Check if it is SEV command completion: */
102 	reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
103 	if (reg & PSP_CMDRESP_RESP) {
104 		sev->int_rcvd = 1;
105 		wake_up(&sev->int_queue);
106 	}
107 }
108 
109 static int sev_wait_cmd_ioc(struct sev_device *sev,
110 			    unsigned int *reg, unsigned int timeout)
111 {
112 	int ret;
113 
114 	ret = wait_event_timeout(sev->int_queue,
115 			sev->int_rcvd, timeout * HZ);
116 	if (!ret)
117 		return -ETIMEDOUT;
118 
119 	*reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
120 
121 	return 0;
122 }
123 
124 static int sev_cmd_buffer_len(int cmd)
125 {
126 	switch (cmd) {
127 	case SEV_CMD_INIT:			return sizeof(struct sev_data_init);
128 	case SEV_CMD_INIT_EX:                   return sizeof(struct sev_data_init_ex);
129 	case SEV_CMD_PLATFORM_STATUS:		return sizeof(struct sev_user_data_status);
130 	case SEV_CMD_PEK_CSR:			return sizeof(struct sev_data_pek_csr);
131 	case SEV_CMD_PEK_CERT_IMPORT:		return sizeof(struct sev_data_pek_cert_import);
132 	case SEV_CMD_PDH_CERT_EXPORT:		return sizeof(struct sev_data_pdh_cert_export);
133 	case SEV_CMD_LAUNCH_START:		return sizeof(struct sev_data_launch_start);
134 	case SEV_CMD_LAUNCH_UPDATE_DATA:	return sizeof(struct sev_data_launch_update_data);
135 	case SEV_CMD_LAUNCH_UPDATE_VMSA:	return sizeof(struct sev_data_launch_update_vmsa);
136 	case SEV_CMD_LAUNCH_FINISH:		return sizeof(struct sev_data_launch_finish);
137 	case SEV_CMD_LAUNCH_MEASURE:		return sizeof(struct sev_data_launch_measure);
138 	case SEV_CMD_ACTIVATE:			return sizeof(struct sev_data_activate);
139 	case SEV_CMD_DEACTIVATE:		return sizeof(struct sev_data_deactivate);
140 	case SEV_CMD_DECOMMISSION:		return sizeof(struct sev_data_decommission);
141 	case SEV_CMD_GUEST_STATUS:		return sizeof(struct sev_data_guest_status);
142 	case SEV_CMD_DBG_DECRYPT:		return sizeof(struct sev_data_dbg);
143 	case SEV_CMD_DBG_ENCRYPT:		return sizeof(struct sev_data_dbg);
144 	case SEV_CMD_SEND_START:		return sizeof(struct sev_data_send_start);
145 	case SEV_CMD_SEND_UPDATE_DATA:		return sizeof(struct sev_data_send_update_data);
146 	case SEV_CMD_SEND_UPDATE_VMSA:		return sizeof(struct sev_data_send_update_vmsa);
147 	case SEV_CMD_SEND_FINISH:		return sizeof(struct sev_data_send_finish);
148 	case SEV_CMD_RECEIVE_START:		return sizeof(struct sev_data_receive_start);
149 	case SEV_CMD_RECEIVE_FINISH:		return sizeof(struct sev_data_receive_finish);
150 	case SEV_CMD_RECEIVE_UPDATE_DATA:	return sizeof(struct sev_data_receive_update_data);
151 	case SEV_CMD_RECEIVE_UPDATE_VMSA:	return sizeof(struct sev_data_receive_update_vmsa);
152 	case SEV_CMD_LAUNCH_UPDATE_SECRET:	return sizeof(struct sev_data_launch_secret);
153 	case SEV_CMD_DOWNLOAD_FIRMWARE:		return sizeof(struct sev_data_download_firmware);
154 	case SEV_CMD_GET_ID:			return sizeof(struct sev_data_get_id);
155 	case SEV_CMD_ATTESTATION_REPORT:	return sizeof(struct sev_data_attestation_report);
156 	case SEV_CMD_SEND_CANCEL:		return sizeof(struct sev_data_send_cancel);
157 	default:				return 0;
158 	}
159 
160 	return 0;
161 }
162 
163 static void *sev_fw_alloc(unsigned long len)
164 {
165 	struct page *page;
166 
167 	page = alloc_pages(GFP_KERNEL, get_order(len));
168 	if (!page)
169 		return NULL;
170 
171 	return page_address(page);
172 }
173 
174 static struct file *open_file_as_root(const char *filename, int flags, umode_t mode)
175 {
176 	struct file *fp;
177 	struct path root;
178 	struct cred *cred;
179 	const struct cred *old_cred;
180 
181 	task_lock(&init_task);
182 	get_fs_root(init_task.fs, &root);
183 	task_unlock(&init_task);
184 
185 	cred = prepare_creds();
186 	if (!cred)
187 		return ERR_PTR(-ENOMEM);
188 	cred->fsuid = GLOBAL_ROOT_UID;
189 	old_cred = override_creds(cred);
190 
191 	fp = file_open_root(&root, filename, flags, mode);
192 	path_put(&root);
193 
194 	revert_creds(old_cred);
195 
196 	return fp;
197 }
198 
199 static int sev_read_init_ex_file(void)
200 {
201 	struct sev_device *sev = psp_master->sev_data;
202 	struct file *fp;
203 	ssize_t nread;
204 
205 	lockdep_assert_held(&sev_cmd_mutex);
206 
207 	if (!sev_init_ex_buffer)
208 		return -EOPNOTSUPP;
209 
210 	fp = open_file_as_root(init_ex_path, O_RDONLY, 0);
211 	if (IS_ERR(fp)) {
212 		int ret = PTR_ERR(fp);
213 
214 		dev_err(sev->dev,
215 			"SEV: could not open %s for read, error %d\n",
216 			init_ex_path, ret);
217 		return ret;
218 	}
219 
220 	nread = kernel_read(fp, sev_init_ex_buffer, NV_LENGTH, NULL);
221 	if (nread != NV_LENGTH) {
222 		dev_err(sev->dev,
223 			"SEV: failed to read %u bytes to non volatile memory area, ret %ld\n",
224 			NV_LENGTH, nread);
225 		return -EIO;
226 	}
227 
228 	dev_dbg(sev->dev, "SEV: read %ld bytes from NV file\n", nread);
229 	filp_close(fp, NULL);
230 
231 	return 0;
232 }
233 
234 static void sev_write_init_ex_file(void)
235 {
236 	struct sev_device *sev = psp_master->sev_data;
237 	struct file *fp;
238 	loff_t offset = 0;
239 	ssize_t nwrite;
240 
241 	lockdep_assert_held(&sev_cmd_mutex);
242 
243 	if (!sev_init_ex_buffer)
244 		return;
245 
246 	fp = open_file_as_root(init_ex_path, O_CREAT | O_WRONLY, 0600);
247 	if (IS_ERR(fp)) {
248 		dev_err(sev->dev,
249 			"SEV: could not open file for write, error %ld\n",
250 			PTR_ERR(fp));
251 		return;
252 	}
253 
254 	nwrite = kernel_write(fp, sev_init_ex_buffer, NV_LENGTH, &offset);
255 	vfs_fsync(fp, 0);
256 	filp_close(fp, NULL);
257 
258 	if (nwrite != NV_LENGTH) {
259 		dev_err(sev->dev,
260 			"SEV: failed to write %u bytes to non volatile memory area, ret %ld\n",
261 			NV_LENGTH, nwrite);
262 		return;
263 	}
264 
265 	dev_dbg(sev->dev, "SEV: write successful to NV file\n");
266 }
267 
268 static void sev_write_init_ex_file_if_required(int cmd_id)
269 {
270 	lockdep_assert_held(&sev_cmd_mutex);
271 
272 	if (!sev_init_ex_buffer)
273 		return;
274 
275 	/*
276 	 * Only a few platform commands modify the SPI/NV area, but none of the
277 	 * non-platform commands do. Only INIT(_EX), PLATFORM_RESET, PEK_GEN,
278 	 * PEK_CERT_IMPORT, and PDH_GEN do.
279 	 */
280 	switch (cmd_id) {
281 	case SEV_CMD_FACTORY_RESET:
282 	case SEV_CMD_INIT_EX:
283 	case SEV_CMD_PDH_GEN:
284 	case SEV_CMD_PEK_CERT_IMPORT:
285 	case SEV_CMD_PEK_GEN:
286 		break;
287 	default:
288 		return;
289 	}
290 
291 	sev_write_init_ex_file();
292 }
293 
294 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
295 {
296 	struct psp_device *psp = psp_master;
297 	struct sev_device *sev;
298 	unsigned int phys_lsb, phys_msb;
299 	unsigned int reg, ret = 0;
300 	int buf_len;
301 
302 	if (!psp || !psp->sev_data)
303 		return -ENODEV;
304 
305 	if (psp_dead)
306 		return -EBUSY;
307 
308 	sev = psp->sev_data;
309 
310 	buf_len = sev_cmd_buffer_len(cmd);
311 	if (WARN_ON_ONCE(!data != !buf_len))
312 		return -EINVAL;
313 
314 	/*
315 	 * Copy the incoming data to driver's scratch buffer as __pa() will not
316 	 * work for some memory, e.g. vmalloc'd addresses, and @data may not be
317 	 * physically contiguous.
318 	 */
319 	if (data)
320 		memcpy(sev->cmd_buf, data, buf_len);
321 
322 	/* Get the physical address of the command buffer */
323 	phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0;
324 	phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0;
325 
326 	dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
327 		cmd, phys_msb, phys_lsb, psp_timeout);
328 
329 	print_hex_dump_debug("(in):  ", DUMP_PREFIX_OFFSET, 16, 2, data,
330 			     buf_len, false);
331 
332 	iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
333 	iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
334 
335 	sev->int_rcvd = 0;
336 
337 	reg = cmd;
338 	reg <<= SEV_CMDRESP_CMD_SHIFT;
339 	reg |= SEV_CMDRESP_IOC;
340 	iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
341 
342 	/* wait for command completion */
343 	ret = sev_wait_cmd_ioc(sev, &reg, psp_timeout);
344 	if (ret) {
345 		if (psp_ret)
346 			*psp_ret = 0;
347 
348 		dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
349 		psp_dead = true;
350 
351 		return ret;
352 	}
353 
354 	psp_timeout = psp_cmd_timeout;
355 
356 	if (psp_ret)
357 		*psp_ret = reg & PSP_CMDRESP_ERR_MASK;
358 
359 	if (reg & PSP_CMDRESP_ERR_MASK) {
360 		dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n",
361 			cmd, reg & PSP_CMDRESP_ERR_MASK);
362 		ret = -EIO;
363 	} else {
364 		sev_write_init_ex_file_if_required(cmd);
365 	}
366 
367 	print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
368 			     buf_len, false);
369 
370 	/*
371 	 * Copy potential output from the PSP back to data.  Do this even on
372 	 * failure in case the caller wants to glean something from the error.
373 	 */
374 	if (data)
375 		memcpy(data, sev->cmd_buf, buf_len);
376 
377 	return ret;
378 }
379 
380 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
381 {
382 	int rc;
383 
384 	mutex_lock(&sev_cmd_mutex);
385 	rc = __sev_do_cmd_locked(cmd, data, psp_ret);
386 	mutex_unlock(&sev_cmd_mutex);
387 
388 	return rc;
389 }
390 
391 static int __sev_init_locked(int *error)
392 {
393 	struct sev_data_init data;
394 
395 	memset(&data, 0, sizeof(data));
396 	if (sev_es_tmr) {
397 		/*
398 		 * Do not include the encryption mask on the physical
399 		 * address of the TMR (firmware should clear it anyway).
400 		 */
401 		data.tmr_address = __pa(sev_es_tmr);
402 
403 		data.flags |= SEV_INIT_FLAGS_SEV_ES;
404 		data.tmr_len = SEV_ES_TMR_SIZE;
405 	}
406 
407 	return __sev_do_cmd_locked(SEV_CMD_INIT, &data, error);
408 }
409 
410 static int __sev_init_ex_locked(int *error)
411 {
412 	struct sev_data_init_ex data;
413 	int ret;
414 
415 	memset(&data, 0, sizeof(data));
416 	data.length = sizeof(data);
417 	data.nv_address = __psp_pa(sev_init_ex_buffer);
418 	data.nv_len = NV_LENGTH;
419 
420 	ret = sev_read_init_ex_file();
421 	if (ret)
422 		return ret;
423 
424 	if (sev_es_tmr) {
425 		/*
426 		 * Do not include the encryption mask on the physical
427 		 * address of the TMR (firmware should clear it anyway).
428 		 */
429 		data.tmr_address = __pa(sev_es_tmr);
430 
431 		data.flags |= SEV_INIT_FLAGS_SEV_ES;
432 		data.tmr_len = SEV_ES_TMR_SIZE;
433 	}
434 
435 	return __sev_do_cmd_locked(SEV_CMD_INIT_EX, &data, error);
436 }
437 
438 static int __sev_platform_init_locked(int *error)
439 {
440 	struct psp_device *psp = psp_master;
441 	struct sev_device *sev;
442 	int rc, psp_ret = -1;
443 	int (*init_function)(int *error);
444 
445 	if (!psp || !psp->sev_data)
446 		return -ENODEV;
447 
448 	sev = psp->sev_data;
449 
450 	if (sev->state == SEV_STATE_INIT)
451 		return 0;
452 
453 	init_function = sev_init_ex_buffer ? __sev_init_ex_locked :
454 			__sev_init_locked;
455 	rc = init_function(&psp_ret);
456 	if (rc && psp_ret == SEV_RET_SECURE_DATA_INVALID) {
457 		/*
458 		 * Initialization command returned an integrity check failure
459 		 * status code, meaning that firmware load and validation of SEV
460 		 * related persistent data has failed. Retrying the
461 		 * initialization function should succeed by replacing the state
462 		 * with a reset state.
463 		 */
464 		dev_err(sev->dev, "SEV: retrying INIT command because of SECURE_DATA_INVALID error. Retrying once to reset PSP SEV state.");
465 		rc = init_function(&psp_ret);
466 	}
467 	if (error)
468 		*error = psp_ret;
469 
470 	if (rc)
471 		return rc;
472 
473 	sev->state = SEV_STATE_INIT;
474 
475 	/* Prepare for first SEV guest launch after INIT */
476 	wbinvd_on_all_cpus();
477 	rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
478 	if (rc)
479 		return rc;
480 
481 	dev_dbg(sev->dev, "SEV firmware initialized\n");
482 
483 	dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
484 		 sev->api_minor, sev->build);
485 
486 	return 0;
487 }
488 
489 int sev_platform_init(int *error)
490 {
491 	int rc;
492 
493 	mutex_lock(&sev_cmd_mutex);
494 	rc = __sev_platform_init_locked(error);
495 	mutex_unlock(&sev_cmd_mutex);
496 
497 	return rc;
498 }
499 EXPORT_SYMBOL_GPL(sev_platform_init);
500 
501 static int __sev_platform_shutdown_locked(int *error)
502 {
503 	struct sev_device *sev = psp_master->sev_data;
504 	int ret;
505 
506 	if (!sev || sev->state == SEV_STATE_UNINIT)
507 		return 0;
508 
509 	ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
510 	if (ret)
511 		return ret;
512 
513 	sev->state = SEV_STATE_UNINIT;
514 	dev_dbg(sev->dev, "SEV firmware shutdown\n");
515 
516 	return ret;
517 }
518 
519 static int sev_platform_shutdown(int *error)
520 {
521 	int rc;
522 
523 	mutex_lock(&sev_cmd_mutex);
524 	rc = __sev_platform_shutdown_locked(NULL);
525 	mutex_unlock(&sev_cmd_mutex);
526 
527 	return rc;
528 }
529 
530 static int sev_get_platform_state(int *state, int *error)
531 {
532 	struct sev_user_data_status data;
533 	int rc;
534 
535 	rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, error);
536 	if (rc)
537 		return rc;
538 
539 	*state = data.state;
540 	return rc;
541 }
542 
543 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
544 {
545 	int state, rc;
546 
547 	if (!writable)
548 		return -EPERM;
549 
550 	/*
551 	 * The SEV spec requires that FACTORY_RESET must be issued in
552 	 * UNINIT state. Before we go further lets check if any guest is
553 	 * active.
554 	 *
555 	 * If FW is in WORKING state then deny the request otherwise issue
556 	 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
557 	 *
558 	 */
559 	rc = sev_get_platform_state(&state, &argp->error);
560 	if (rc)
561 		return rc;
562 
563 	if (state == SEV_STATE_WORKING)
564 		return -EBUSY;
565 
566 	if (state == SEV_STATE_INIT) {
567 		rc = __sev_platform_shutdown_locked(&argp->error);
568 		if (rc)
569 			return rc;
570 	}
571 
572 	return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
573 }
574 
575 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
576 {
577 	struct sev_user_data_status data;
578 	int ret;
579 
580 	memset(&data, 0, sizeof(data));
581 
582 	ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, &data, &argp->error);
583 	if (ret)
584 		return ret;
585 
586 	if (copy_to_user((void __user *)argp->data, &data, sizeof(data)))
587 		ret = -EFAULT;
588 
589 	return ret;
590 }
591 
592 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
593 {
594 	struct sev_device *sev = psp_master->sev_data;
595 	int rc;
596 
597 	if (!writable)
598 		return -EPERM;
599 
600 	if (sev->state == SEV_STATE_UNINIT) {
601 		rc = __sev_platform_init_locked(&argp->error);
602 		if (rc)
603 			return rc;
604 	}
605 
606 	return __sev_do_cmd_locked(cmd, NULL, &argp->error);
607 }
608 
609 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
610 {
611 	struct sev_device *sev = psp_master->sev_data;
612 	struct sev_user_data_pek_csr input;
613 	struct sev_data_pek_csr data;
614 	void __user *input_address;
615 	void *blob = NULL;
616 	int ret;
617 
618 	if (!writable)
619 		return -EPERM;
620 
621 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
622 		return -EFAULT;
623 
624 	memset(&data, 0, sizeof(data));
625 
626 	/* userspace wants to query CSR length */
627 	if (!input.address || !input.length)
628 		goto cmd;
629 
630 	/* allocate a physically contiguous buffer to store the CSR blob */
631 	input_address = (void __user *)input.address;
632 	if (input.length > SEV_FW_BLOB_MAX_SIZE)
633 		return -EFAULT;
634 
635 	blob = kzalloc(input.length, GFP_KERNEL);
636 	if (!blob)
637 		return -ENOMEM;
638 
639 	data.address = __psp_pa(blob);
640 	data.len = input.length;
641 
642 cmd:
643 	if (sev->state == SEV_STATE_UNINIT) {
644 		ret = __sev_platform_init_locked(&argp->error);
645 		if (ret)
646 			goto e_free_blob;
647 	}
648 
649 	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error);
650 
651 	 /* If we query the CSR length, FW responded with expected data. */
652 	input.length = data.len;
653 
654 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
655 		ret = -EFAULT;
656 		goto e_free_blob;
657 	}
658 
659 	if (blob) {
660 		if (copy_to_user(input_address, blob, input.length))
661 			ret = -EFAULT;
662 	}
663 
664 e_free_blob:
665 	kfree(blob);
666 	return ret;
667 }
668 
669 void *psp_copy_user_blob(u64 uaddr, u32 len)
670 {
671 	if (!uaddr || !len)
672 		return ERR_PTR(-EINVAL);
673 
674 	/* verify that blob length does not exceed our limit */
675 	if (len > SEV_FW_BLOB_MAX_SIZE)
676 		return ERR_PTR(-EINVAL);
677 
678 	return memdup_user((void __user *)uaddr, len);
679 }
680 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
681 
682 static int sev_get_api_version(void)
683 {
684 	struct sev_device *sev = psp_master->sev_data;
685 	struct sev_user_data_status status;
686 	int error = 0, ret;
687 
688 	ret = sev_platform_status(&status, &error);
689 	if (ret) {
690 		dev_err(sev->dev,
691 			"SEV: failed to get status. Error: %#x\n", error);
692 		return 1;
693 	}
694 
695 	sev->api_major = status.api_major;
696 	sev->api_minor = status.api_minor;
697 	sev->build = status.build;
698 	sev->state = status.state;
699 
700 	return 0;
701 }
702 
703 static int sev_get_firmware(struct device *dev,
704 			    const struct firmware **firmware)
705 {
706 	char fw_name_specific[SEV_FW_NAME_SIZE];
707 	char fw_name_subset[SEV_FW_NAME_SIZE];
708 
709 	snprintf(fw_name_specific, sizeof(fw_name_specific),
710 		 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
711 		 boot_cpu_data.x86, boot_cpu_data.x86_model);
712 
713 	snprintf(fw_name_subset, sizeof(fw_name_subset),
714 		 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
715 		 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
716 
717 	/* Check for SEV FW for a particular model.
718 	 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
719 	 *
720 	 * or
721 	 *
722 	 * Check for SEV FW common to a subset of models.
723 	 * Ex. amd_sev_fam17h_model0xh.sbin for
724 	 *     Family 17h Model 00h -- Family 17h Model 0Fh
725 	 *
726 	 * or
727 	 *
728 	 * Fall-back to using generic name: sev.fw
729 	 */
730 	if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
731 	    (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
732 	    (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
733 		return 0;
734 
735 	return -ENOENT;
736 }
737 
738 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
739 static int sev_update_firmware(struct device *dev)
740 {
741 	struct sev_data_download_firmware *data;
742 	const struct firmware *firmware;
743 	int ret, error, order;
744 	struct page *p;
745 	u64 data_size;
746 
747 	if (!sev_version_greater_or_equal(0, 15)) {
748 		dev_dbg(dev, "DOWNLOAD_FIRMWARE not supported\n");
749 		return -1;
750 	}
751 
752 	if (sev_get_firmware(dev, &firmware) == -ENOENT) {
753 		dev_dbg(dev, "No SEV firmware file present\n");
754 		return -1;
755 	}
756 
757 	/*
758 	 * SEV FW expects the physical address given to it to be 32
759 	 * byte aligned. Memory allocated has structure placed at the
760 	 * beginning followed by the firmware being passed to the SEV
761 	 * FW. Allocate enough memory for data structure + alignment
762 	 * padding + SEV FW.
763 	 */
764 	data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
765 
766 	order = get_order(firmware->size + data_size);
767 	p = alloc_pages(GFP_KERNEL, order);
768 	if (!p) {
769 		ret = -1;
770 		goto fw_err;
771 	}
772 
773 	/*
774 	 * Copy firmware data to a kernel allocated contiguous
775 	 * memory region.
776 	 */
777 	data = page_address(p);
778 	memcpy(page_address(p) + data_size, firmware->data, firmware->size);
779 
780 	data->address = __psp_pa(page_address(p) + data_size);
781 	data->len = firmware->size;
782 
783 	ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
784 
785 	/*
786 	 * A quirk for fixing the committed TCB version, when upgrading from
787 	 * earlier firmware version than 1.50.
788 	 */
789 	if (!ret && !sev_version_greater_or_equal(1, 50))
790 		ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
791 
792 	if (ret)
793 		dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
794 	else
795 		dev_info(dev, "SEV firmware update successful\n");
796 
797 	__free_pages(p, order);
798 
799 fw_err:
800 	release_firmware(firmware);
801 
802 	return ret;
803 }
804 
805 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
806 {
807 	struct sev_device *sev = psp_master->sev_data;
808 	struct sev_user_data_pek_cert_import input;
809 	struct sev_data_pek_cert_import data;
810 	void *pek_blob, *oca_blob;
811 	int ret;
812 
813 	if (!writable)
814 		return -EPERM;
815 
816 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
817 		return -EFAULT;
818 
819 	/* copy PEK certificate blobs from userspace */
820 	pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
821 	if (IS_ERR(pek_blob))
822 		return PTR_ERR(pek_blob);
823 
824 	data.reserved = 0;
825 	data.pek_cert_address = __psp_pa(pek_blob);
826 	data.pek_cert_len = input.pek_cert_len;
827 
828 	/* copy PEK certificate blobs from userspace */
829 	oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
830 	if (IS_ERR(oca_blob)) {
831 		ret = PTR_ERR(oca_blob);
832 		goto e_free_pek;
833 	}
834 
835 	data.oca_cert_address = __psp_pa(oca_blob);
836 	data.oca_cert_len = input.oca_cert_len;
837 
838 	/* If platform is not in INIT state then transition it to INIT */
839 	if (sev->state != SEV_STATE_INIT) {
840 		ret = __sev_platform_init_locked(&argp->error);
841 		if (ret)
842 			goto e_free_oca;
843 	}
844 
845 	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error);
846 
847 e_free_oca:
848 	kfree(oca_blob);
849 e_free_pek:
850 	kfree(pek_blob);
851 	return ret;
852 }
853 
854 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
855 {
856 	struct sev_user_data_get_id2 input;
857 	struct sev_data_get_id data;
858 	void __user *input_address;
859 	void *id_blob = NULL;
860 	int ret;
861 
862 	/* SEV GET_ID is available from SEV API v0.16 and up */
863 	if (!sev_version_greater_or_equal(0, 16))
864 		return -ENOTSUPP;
865 
866 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
867 		return -EFAULT;
868 
869 	input_address = (void __user *)input.address;
870 
871 	if (input.address && input.length) {
872 		id_blob = kzalloc(input.length, GFP_KERNEL);
873 		if (!id_blob)
874 			return -ENOMEM;
875 
876 		data.address = __psp_pa(id_blob);
877 		data.len = input.length;
878 	} else {
879 		data.address = 0;
880 		data.len = 0;
881 	}
882 
883 	ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, &data, &argp->error);
884 
885 	/*
886 	 * Firmware will return the length of the ID value (either the minimum
887 	 * required length or the actual length written), return it to the user.
888 	 */
889 	input.length = data.len;
890 
891 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
892 		ret = -EFAULT;
893 		goto e_free;
894 	}
895 
896 	if (id_blob) {
897 		if (copy_to_user(input_address, id_blob, data.len)) {
898 			ret = -EFAULT;
899 			goto e_free;
900 		}
901 	}
902 
903 e_free:
904 	kfree(id_blob);
905 
906 	return ret;
907 }
908 
909 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
910 {
911 	struct sev_data_get_id *data;
912 	u64 data_size, user_size;
913 	void *id_blob, *mem;
914 	int ret;
915 
916 	/* SEV GET_ID available from SEV API v0.16 and up */
917 	if (!sev_version_greater_or_equal(0, 16))
918 		return -ENOTSUPP;
919 
920 	/* SEV FW expects the buffer it fills with the ID to be
921 	 * 8-byte aligned. Memory allocated should be enough to
922 	 * hold data structure + alignment padding + memory
923 	 * where SEV FW writes the ID.
924 	 */
925 	data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
926 	user_size = sizeof(struct sev_user_data_get_id);
927 
928 	mem = kzalloc(data_size + user_size, GFP_KERNEL);
929 	if (!mem)
930 		return -ENOMEM;
931 
932 	data = mem;
933 	id_blob = mem + data_size;
934 
935 	data->address = __psp_pa(id_blob);
936 	data->len = user_size;
937 
938 	ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
939 	if (!ret) {
940 		if (copy_to_user((void __user *)argp->data, id_blob, data->len))
941 			ret = -EFAULT;
942 	}
943 
944 	kfree(mem);
945 
946 	return ret;
947 }
948 
949 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
950 {
951 	struct sev_device *sev = psp_master->sev_data;
952 	struct sev_user_data_pdh_cert_export input;
953 	void *pdh_blob = NULL, *cert_blob = NULL;
954 	struct sev_data_pdh_cert_export data;
955 	void __user *input_cert_chain_address;
956 	void __user *input_pdh_cert_address;
957 	int ret;
958 
959 	/* If platform is not in INIT state then transition it to INIT. */
960 	if (sev->state != SEV_STATE_INIT) {
961 		if (!writable)
962 			return -EPERM;
963 
964 		ret = __sev_platform_init_locked(&argp->error);
965 		if (ret)
966 			return ret;
967 	}
968 
969 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
970 		return -EFAULT;
971 
972 	memset(&data, 0, sizeof(data));
973 
974 	/* Userspace wants to query the certificate length. */
975 	if (!input.pdh_cert_address ||
976 	    !input.pdh_cert_len ||
977 	    !input.cert_chain_address)
978 		goto cmd;
979 
980 	input_pdh_cert_address = (void __user *)input.pdh_cert_address;
981 	input_cert_chain_address = (void __user *)input.cert_chain_address;
982 
983 	/* Allocate a physically contiguous buffer to store the PDH blob. */
984 	if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE)
985 		return -EFAULT;
986 
987 	/* Allocate a physically contiguous buffer to store the cert chain blob. */
988 	if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE)
989 		return -EFAULT;
990 
991 	pdh_blob = kzalloc(input.pdh_cert_len, GFP_KERNEL);
992 	if (!pdh_blob)
993 		return -ENOMEM;
994 
995 	data.pdh_cert_address = __psp_pa(pdh_blob);
996 	data.pdh_cert_len = input.pdh_cert_len;
997 
998 	cert_blob = kzalloc(input.cert_chain_len, GFP_KERNEL);
999 	if (!cert_blob) {
1000 		ret = -ENOMEM;
1001 		goto e_free_pdh;
1002 	}
1003 
1004 	data.cert_chain_address = __psp_pa(cert_blob);
1005 	data.cert_chain_len = input.cert_chain_len;
1006 
1007 cmd:
1008 	ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error);
1009 
1010 	/* If we query the length, FW responded with expected data. */
1011 	input.cert_chain_len = data.cert_chain_len;
1012 	input.pdh_cert_len = data.pdh_cert_len;
1013 
1014 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
1015 		ret = -EFAULT;
1016 		goto e_free_cert;
1017 	}
1018 
1019 	if (pdh_blob) {
1020 		if (copy_to_user(input_pdh_cert_address,
1021 				 pdh_blob, input.pdh_cert_len)) {
1022 			ret = -EFAULT;
1023 			goto e_free_cert;
1024 		}
1025 	}
1026 
1027 	if (cert_blob) {
1028 		if (copy_to_user(input_cert_chain_address,
1029 				 cert_blob, input.cert_chain_len))
1030 			ret = -EFAULT;
1031 	}
1032 
1033 e_free_cert:
1034 	kfree(cert_blob);
1035 e_free_pdh:
1036 	kfree(pdh_blob);
1037 	return ret;
1038 }
1039 
1040 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
1041 {
1042 	void __user *argp = (void __user *)arg;
1043 	struct sev_issue_cmd input;
1044 	int ret = -EFAULT;
1045 	bool writable = file->f_mode & FMODE_WRITE;
1046 
1047 	if (!psp_master || !psp_master->sev_data)
1048 		return -ENODEV;
1049 
1050 	if (ioctl != SEV_ISSUE_CMD)
1051 		return -EINVAL;
1052 
1053 	if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
1054 		return -EFAULT;
1055 
1056 	if (input.cmd > SEV_MAX)
1057 		return -EINVAL;
1058 
1059 	mutex_lock(&sev_cmd_mutex);
1060 
1061 	switch (input.cmd) {
1062 
1063 	case SEV_FACTORY_RESET:
1064 		ret = sev_ioctl_do_reset(&input, writable);
1065 		break;
1066 	case SEV_PLATFORM_STATUS:
1067 		ret = sev_ioctl_do_platform_status(&input);
1068 		break;
1069 	case SEV_PEK_GEN:
1070 		ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
1071 		break;
1072 	case SEV_PDH_GEN:
1073 		ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
1074 		break;
1075 	case SEV_PEK_CSR:
1076 		ret = sev_ioctl_do_pek_csr(&input, writable);
1077 		break;
1078 	case SEV_PEK_CERT_IMPORT:
1079 		ret = sev_ioctl_do_pek_import(&input, writable);
1080 		break;
1081 	case SEV_PDH_CERT_EXPORT:
1082 		ret = sev_ioctl_do_pdh_export(&input, writable);
1083 		break;
1084 	case SEV_GET_ID:
1085 		pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
1086 		ret = sev_ioctl_do_get_id(&input);
1087 		break;
1088 	case SEV_GET_ID2:
1089 		ret = sev_ioctl_do_get_id2(&input);
1090 		break;
1091 	default:
1092 		ret = -EINVAL;
1093 		goto out;
1094 	}
1095 
1096 	if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
1097 		ret = -EFAULT;
1098 out:
1099 	mutex_unlock(&sev_cmd_mutex);
1100 
1101 	return ret;
1102 }
1103 
1104 static const struct file_operations sev_fops = {
1105 	.owner	= THIS_MODULE,
1106 	.unlocked_ioctl = sev_ioctl,
1107 };
1108 
1109 int sev_platform_status(struct sev_user_data_status *data, int *error)
1110 {
1111 	return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
1112 }
1113 EXPORT_SYMBOL_GPL(sev_platform_status);
1114 
1115 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
1116 {
1117 	return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
1118 }
1119 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
1120 
1121 int sev_guest_activate(struct sev_data_activate *data, int *error)
1122 {
1123 	return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
1124 }
1125 EXPORT_SYMBOL_GPL(sev_guest_activate);
1126 
1127 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
1128 {
1129 	return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
1130 }
1131 EXPORT_SYMBOL_GPL(sev_guest_decommission);
1132 
1133 int sev_guest_df_flush(int *error)
1134 {
1135 	return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
1136 }
1137 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
1138 
1139 static void sev_exit(struct kref *ref)
1140 {
1141 	misc_deregister(&misc_dev->misc);
1142 	kfree(misc_dev);
1143 	misc_dev = NULL;
1144 }
1145 
1146 static int sev_misc_init(struct sev_device *sev)
1147 {
1148 	struct device *dev = sev->dev;
1149 	int ret;
1150 
1151 	/*
1152 	 * SEV feature support can be detected on multiple devices but the SEV
1153 	 * FW commands must be issued on the master. During probe, we do not
1154 	 * know the master hence we create /dev/sev on the first device probe.
1155 	 * sev_do_cmd() finds the right master device to which to issue the
1156 	 * command to the firmware.
1157 	 */
1158 	if (!misc_dev) {
1159 		struct miscdevice *misc;
1160 
1161 		misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
1162 		if (!misc_dev)
1163 			return -ENOMEM;
1164 
1165 		misc = &misc_dev->misc;
1166 		misc->minor = MISC_DYNAMIC_MINOR;
1167 		misc->name = DEVICE_NAME;
1168 		misc->fops = &sev_fops;
1169 
1170 		ret = misc_register(misc);
1171 		if (ret)
1172 			return ret;
1173 
1174 		kref_init(&misc_dev->refcount);
1175 	} else {
1176 		kref_get(&misc_dev->refcount);
1177 	}
1178 
1179 	init_waitqueue_head(&sev->int_queue);
1180 	sev->misc = misc_dev;
1181 	dev_dbg(dev, "registered SEV device\n");
1182 
1183 	return 0;
1184 }
1185 
1186 int sev_dev_init(struct psp_device *psp)
1187 {
1188 	struct device *dev = psp->dev;
1189 	struct sev_device *sev;
1190 	int ret = -ENOMEM;
1191 
1192 	if (!boot_cpu_has(X86_FEATURE_SEV)) {
1193 		dev_info_once(dev, "SEV: memory encryption not enabled by BIOS\n");
1194 		return 0;
1195 	}
1196 
1197 	sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
1198 	if (!sev)
1199 		goto e_err;
1200 
1201 	sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0);
1202 	if (!sev->cmd_buf)
1203 		goto e_sev;
1204 
1205 	psp->sev_data = sev;
1206 
1207 	sev->dev = dev;
1208 	sev->psp = psp;
1209 
1210 	sev->io_regs = psp->io_regs;
1211 
1212 	sev->vdata = (struct sev_vdata *)psp->vdata->sev;
1213 	if (!sev->vdata) {
1214 		ret = -ENODEV;
1215 		dev_err(dev, "sev: missing driver data\n");
1216 		goto e_buf;
1217 	}
1218 
1219 	psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
1220 
1221 	ret = sev_misc_init(sev);
1222 	if (ret)
1223 		goto e_irq;
1224 
1225 	dev_notice(dev, "sev enabled\n");
1226 
1227 	return 0;
1228 
1229 e_irq:
1230 	psp_clear_sev_irq_handler(psp);
1231 e_buf:
1232 	devm_free_pages(dev, (unsigned long)sev->cmd_buf);
1233 e_sev:
1234 	devm_kfree(dev, sev);
1235 e_err:
1236 	psp->sev_data = NULL;
1237 
1238 	dev_notice(dev, "sev initialization failed\n");
1239 
1240 	return ret;
1241 }
1242 
1243 static void sev_firmware_shutdown(struct sev_device *sev)
1244 {
1245 	sev_platform_shutdown(NULL);
1246 
1247 	if (sev_es_tmr) {
1248 		/* The TMR area was encrypted, flush it from the cache */
1249 		wbinvd_on_all_cpus();
1250 
1251 		free_pages((unsigned long)sev_es_tmr,
1252 			   get_order(SEV_ES_TMR_SIZE));
1253 		sev_es_tmr = NULL;
1254 	}
1255 
1256 	if (sev_init_ex_buffer) {
1257 		free_pages((unsigned long)sev_init_ex_buffer,
1258 			   get_order(NV_LENGTH));
1259 		sev_init_ex_buffer = NULL;
1260 	}
1261 }
1262 
1263 void sev_dev_destroy(struct psp_device *psp)
1264 {
1265 	struct sev_device *sev = psp->sev_data;
1266 
1267 	if (!sev)
1268 		return;
1269 
1270 	sev_firmware_shutdown(sev);
1271 
1272 	if (sev->misc)
1273 		kref_put(&misc_dev->refcount, sev_exit);
1274 
1275 	psp_clear_sev_irq_handler(psp);
1276 }
1277 
1278 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1279 				void *data, int *error)
1280 {
1281 	if (!filep || filep->f_op != &sev_fops)
1282 		return -EBADF;
1283 
1284 	return sev_do_cmd(cmd, data, error);
1285 }
1286 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1287 
1288 void sev_pci_init(void)
1289 {
1290 	struct sev_device *sev = psp_master->sev_data;
1291 	int error, rc;
1292 
1293 	if (!sev)
1294 		return;
1295 
1296 	psp_timeout = psp_probe_timeout;
1297 
1298 	if (sev_get_api_version())
1299 		goto err;
1300 
1301 	if (sev_update_firmware(sev->dev) == 0)
1302 		sev_get_api_version();
1303 
1304 	/* If an init_ex_path is provided rely on INIT_EX for PSP initialization
1305 	 * instead of INIT.
1306 	 */
1307 	if (init_ex_path) {
1308 		sev_init_ex_buffer = sev_fw_alloc(NV_LENGTH);
1309 		if (!sev_init_ex_buffer) {
1310 			dev_err(sev->dev,
1311 				"SEV: INIT_EX NV memory allocation failed\n");
1312 			goto err;
1313 		}
1314 	}
1315 
1316 	/* Obtain the TMR memory area for SEV-ES use */
1317 	sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE);
1318 	if (!sev_es_tmr)
1319 		dev_warn(sev->dev,
1320 			 "SEV: TMR allocation failed, SEV-ES support unavailable\n");
1321 
1322 	if (!psp_init_on_probe)
1323 		return;
1324 
1325 	/* Initialize the platform */
1326 	rc = sev_platform_init(&error);
1327 	if (rc)
1328 		dev_err(sev->dev, "SEV: failed to INIT error %#x, rc %d\n",
1329 			error, rc);
1330 
1331 	return;
1332 
1333 err:
1334 	psp_master->sev_data = NULL;
1335 }
1336 
1337 void sev_pci_exit(void)
1338 {
1339 	struct sev_device *sev = psp_master->sev_data;
1340 
1341 	if (!sev)
1342 		return;
1343 
1344 	sev_firmware_shutdown(sev);
1345 }
1346