xref: /openbmc/linux/drivers/crypto/ccp/sev-dev.c (revision 15e3ae36)
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 <brijesh.singh@amd.com>
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 
24 #include <asm/smp.h>
25 
26 #include "psp-dev.h"
27 #include "sev-dev.h"
28 
29 #define DEVICE_NAME		"sev"
30 #define SEV_FW_FILE		"amd/sev.fw"
31 #define SEV_FW_NAME_SIZE	64
32 
33 static DEFINE_MUTEX(sev_cmd_mutex);
34 static struct sev_misc_dev *misc_dev;
35 
36 static int psp_cmd_timeout = 100;
37 module_param(psp_cmd_timeout, int, 0644);
38 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands");
39 
40 static int psp_probe_timeout = 5;
41 module_param(psp_probe_timeout, int, 0644);
42 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe");
43 
44 static bool psp_dead;
45 static int psp_timeout;
46 
47 static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
48 {
49 	struct sev_device *sev = psp_master->sev_data;
50 
51 	if (sev->api_major > maj)
52 		return true;
53 
54 	if (sev->api_major == maj && sev->api_minor >= min)
55 		return true;
56 
57 	return false;
58 }
59 
60 static void sev_irq_handler(int irq, void *data, unsigned int status)
61 {
62 	struct sev_device *sev = data;
63 	int reg;
64 
65 	/* Check if it is command completion: */
66 	if (!(status & SEV_CMD_COMPLETE))
67 		return;
68 
69 	/* Check if it is SEV command completion: */
70 	reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
71 	if (reg & PSP_CMDRESP_RESP) {
72 		sev->int_rcvd = 1;
73 		wake_up(&sev->int_queue);
74 	}
75 }
76 
77 static int sev_wait_cmd_ioc(struct sev_device *sev,
78 			    unsigned int *reg, unsigned int timeout)
79 {
80 	int ret;
81 
82 	ret = wait_event_timeout(sev->int_queue,
83 			sev->int_rcvd, timeout * HZ);
84 	if (!ret)
85 		return -ETIMEDOUT;
86 
87 	*reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
88 
89 	return 0;
90 }
91 
92 static int sev_cmd_buffer_len(int cmd)
93 {
94 	switch (cmd) {
95 	case SEV_CMD_INIT:			return sizeof(struct sev_data_init);
96 	case SEV_CMD_PLATFORM_STATUS:		return sizeof(struct sev_user_data_status);
97 	case SEV_CMD_PEK_CSR:			return sizeof(struct sev_data_pek_csr);
98 	case SEV_CMD_PEK_CERT_IMPORT:		return sizeof(struct sev_data_pek_cert_import);
99 	case SEV_CMD_PDH_CERT_EXPORT:		return sizeof(struct sev_data_pdh_cert_export);
100 	case SEV_CMD_LAUNCH_START:		return sizeof(struct sev_data_launch_start);
101 	case SEV_CMD_LAUNCH_UPDATE_DATA:	return sizeof(struct sev_data_launch_update_data);
102 	case SEV_CMD_LAUNCH_UPDATE_VMSA:	return sizeof(struct sev_data_launch_update_vmsa);
103 	case SEV_CMD_LAUNCH_FINISH:		return sizeof(struct sev_data_launch_finish);
104 	case SEV_CMD_LAUNCH_MEASURE:		return sizeof(struct sev_data_launch_measure);
105 	case SEV_CMD_ACTIVATE:			return sizeof(struct sev_data_activate);
106 	case SEV_CMD_DEACTIVATE:		return sizeof(struct sev_data_deactivate);
107 	case SEV_CMD_DECOMMISSION:		return sizeof(struct sev_data_decommission);
108 	case SEV_CMD_GUEST_STATUS:		return sizeof(struct sev_data_guest_status);
109 	case SEV_CMD_DBG_DECRYPT:		return sizeof(struct sev_data_dbg);
110 	case SEV_CMD_DBG_ENCRYPT:		return sizeof(struct sev_data_dbg);
111 	case SEV_CMD_SEND_START:		return sizeof(struct sev_data_send_start);
112 	case SEV_CMD_SEND_UPDATE_DATA:		return sizeof(struct sev_data_send_update_data);
113 	case SEV_CMD_SEND_UPDATE_VMSA:		return sizeof(struct sev_data_send_update_vmsa);
114 	case SEV_CMD_SEND_FINISH:		return sizeof(struct sev_data_send_finish);
115 	case SEV_CMD_RECEIVE_START:		return sizeof(struct sev_data_receive_start);
116 	case SEV_CMD_RECEIVE_FINISH:		return sizeof(struct sev_data_receive_finish);
117 	case SEV_CMD_RECEIVE_UPDATE_DATA:	return sizeof(struct sev_data_receive_update_data);
118 	case SEV_CMD_RECEIVE_UPDATE_VMSA:	return sizeof(struct sev_data_receive_update_vmsa);
119 	case SEV_CMD_LAUNCH_UPDATE_SECRET:	return sizeof(struct sev_data_launch_secret);
120 	case SEV_CMD_DOWNLOAD_FIRMWARE:		return sizeof(struct sev_data_download_firmware);
121 	case SEV_CMD_GET_ID:			return sizeof(struct sev_data_get_id);
122 	default:				return 0;
123 	}
124 
125 	return 0;
126 }
127 
128 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
129 {
130 	struct psp_device *psp = psp_master;
131 	struct sev_device *sev;
132 	unsigned int phys_lsb, phys_msb;
133 	unsigned int reg, ret = 0;
134 
135 	if (!psp || !psp->sev_data)
136 		return -ENODEV;
137 
138 	if (psp_dead)
139 		return -EBUSY;
140 
141 	sev = psp->sev_data;
142 
143 	/* Get the physical address of the command buffer */
144 	phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
145 	phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
146 
147 	dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
148 		cmd, phys_msb, phys_lsb, psp_timeout);
149 
150 	print_hex_dump_debug("(in):  ", DUMP_PREFIX_OFFSET, 16, 2, data,
151 			     sev_cmd_buffer_len(cmd), false);
152 
153 	iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg);
154 	iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg);
155 
156 	sev->int_rcvd = 0;
157 
158 	reg = cmd;
159 	reg <<= SEV_CMDRESP_CMD_SHIFT;
160 	reg |= SEV_CMDRESP_IOC;
161 	iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
162 
163 	/* wait for command completion */
164 	ret = sev_wait_cmd_ioc(sev, &reg, psp_timeout);
165 	if (ret) {
166 		if (psp_ret)
167 			*psp_ret = 0;
168 
169 		dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
170 		psp_dead = true;
171 
172 		return ret;
173 	}
174 
175 	psp_timeout = psp_cmd_timeout;
176 
177 	if (psp_ret)
178 		*psp_ret = reg & PSP_CMDRESP_ERR_MASK;
179 
180 	if (reg & PSP_CMDRESP_ERR_MASK) {
181 		dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n",
182 			cmd, reg & PSP_CMDRESP_ERR_MASK);
183 		ret = -EIO;
184 	}
185 
186 	print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
187 			     sev_cmd_buffer_len(cmd), false);
188 
189 	return ret;
190 }
191 
192 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
193 {
194 	int rc;
195 
196 	mutex_lock(&sev_cmd_mutex);
197 	rc = __sev_do_cmd_locked(cmd, data, psp_ret);
198 	mutex_unlock(&sev_cmd_mutex);
199 
200 	return rc;
201 }
202 
203 static int __sev_platform_init_locked(int *error)
204 {
205 	struct psp_device *psp = psp_master;
206 	struct sev_device *sev;
207 	int rc = 0;
208 
209 	if (!psp || !psp->sev_data)
210 		return -ENODEV;
211 
212 	sev = psp->sev_data;
213 
214 	if (sev->state == SEV_STATE_INIT)
215 		return 0;
216 
217 	rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error);
218 	if (rc)
219 		return rc;
220 
221 	sev->state = SEV_STATE_INIT;
222 
223 	/* Prepare for first SEV guest launch after INIT */
224 	wbinvd_on_all_cpus();
225 	rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error);
226 	if (rc)
227 		return rc;
228 
229 	dev_dbg(sev->dev, "SEV firmware initialized\n");
230 
231 	return rc;
232 }
233 
234 int sev_platform_init(int *error)
235 {
236 	int rc;
237 
238 	mutex_lock(&sev_cmd_mutex);
239 	rc = __sev_platform_init_locked(error);
240 	mutex_unlock(&sev_cmd_mutex);
241 
242 	return rc;
243 }
244 EXPORT_SYMBOL_GPL(sev_platform_init);
245 
246 static int __sev_platform_shutdown_locked(int *error)
247 {
248 	struct sev_device *sev = psp_master->sev_data;
249 	int ret;
250 
251 	ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
252 	if (ret)
253 		return ret;
254 
255 	sev->state = SEV_STATE_UNINIT;
256 	dev_dbg(sev->dev, "SEV firmware shutdown\n");
257 
258 	return ret;
259 }
260 
261 static int sev_platform_shutdown(int *error)
262 {
263 	int rc;
264 
265 	mutex_lock(&sev_cmd_mutex);
266 	rc = __sev_platform_shutdown_locked(NULL);
267 	mutex_unlock(&sev_cmd_mutex);
268 
269 	return rc;
270 }
271 
272 static int sev_get_platform_state(int *state, int *error)
273 {
274 	struct sev_device *sev = psp_master->sev_data;
275 	int rc;
276 
277 	rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
278 				 &sev->status_cmd_buf, error);
279 	if (rc)
280 		return rc;
281 
282 	*state = sev->status_cmd_buf.state;
283 	return rc;
284 }
285 
286 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
287 {
288 	int state, rc;
289 
290 	if (!writable)
291 		return -EPERM;
292 
293 	/*
294 	 * The SEV spec requires that FACTORY_RESET must be issued in
295 	 * UNINIT state. Before we go further lets check if any guest is
296 	 * active.
297 	 *
298 	 * If FW is in WORKING state then deny the request otherwise issue
299 	 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
300 	 *
301 	 */
302 	rc = sev_get_platform_state(&state, &argp->error);
303 	if (rc)
304 		return rc;
305 
306 	if (state == SEV_STATE_WORKING)
307 		return -EBUSY;
308 
309 	if (state == SEV_STATE_INIT) {
310 		rc = __sev_platform_shutdown_locked(&argp->error);
311 		if (rc)
312 			return rc;
313 	}
314 
315 	return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
316 }
317 
318 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
319 {
320 	struct sev_device *sev = psp_master->sev_data;
321 	struct sev_user_data_status *data = &sev->status_cmd_buf;
322 	int ret;
323 
324 	ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
325 	if (ret)
326 		return ret;
327 
328 	if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
329 		ret = -EFAULT;
330 
331 	return ret;
332 }
333 
334 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
335 {
336 	struct sev_device *sev = psp_master->sev_data;
337 	int rc;
338 
339 	if (!writable)
340 		return -EPERM;
341 
342 	if (sev->state == SEV_STATE_UNINIT) {
343 		rc = __sev_platform_init_locked(&argp->error);
344 		if (rc)
345 			return rc;
346 	}
347 
348 	return __sev_do_cmd_locked(cmd, NULL, &argp->error);
349 }
350 
351 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
352 {
353 	struct sev_device *sev = psp_master->sev_data;
354 	struct sev_user_data_pek_csr input;
355 	struct sev_data_pek_csr *data;
356 	void *blob = NULL;
357 	int ret;
358 
359 	if (!writable)
360 		return -EPERM;
361 
362 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
363 		return -EFAULT;
364 
365 	data = kzalloc(sizeof(*data), GFP_KERNEL);
366 	if (!data)
367 		return -ENOMEM;
368 
369 	/* userspace wants to query CSR length */
370 	if (!input.address || !input.length)
371 		goto cmd;
372 
373 	/* allocate a physically contiguous buffer to store the CSR blob */
374 	if (!access_ok(input.address, input.length) ||
375 	    input.length > SEV_FW_BLOB_MAX_SIZE) {
376 		ret = -EFAULT;
377 		goto e_free;
378 	}
379 
380 	blob = kmalloc(input.length, GFP_KERNEL);
381 	if (!blob) {
382 		ret = -ENOMEM;
383 		goto e_free;
384 	}
385 
386 	data->address = __psp_pa(blob);
387 	data->len = input.length;
388 
389 cmd:
390 	if (sev->state == SEV_STATE_UNINIT) {
391 		ret = __sev_platform_init_locked(&argp->error);
392 		if (ret)
393 			goto e_free_blob;
394 	}
395 
396 	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
397 
398 	 /* If we query the CSR length, FW responded with expected data. */
399 	input.length = data->len;
400 
401 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
402 		ret = -EFAULT;
403 		goto e_free_blob;
404 	}
405 
406 	if (blob) {
407 		if (copy_to_user((void __user *)input.address, blob, input.length))
408 			ret = -EFAULT;
409 	}
410 
411 e_free_blob:
412 	kfree(blob);
413 e_free:
414 	kfree(data);
415 	return ret;
416 }
417 
418 void *psp_copy_user_blob(u64 __user uaddr, u32 len)
419 {
420 	if (!uaddr || !len)
421 		return ERR_PTR(-EINVAL);
422 
423 	/* verify that blob length does not exceed our limit */
424 	if (len > SEV_FW_BLOB_MAX_SIZE)
425 		return ERR_PTR(-EINVAL);
426 
427 	return memdup_user((void __user *)(uintptr_t)uaddr, len);
428 }
429 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
430 
431 static int sev_get_api_version(void)
432 {
433 	struct sev_device *sev = psp_master->sev_data;
434 	struct sev_user_data_status *status;
435 	int error = 0, ret;
436 
437 	status = &sev->status_cmd_buf;
438 	ret = sev_platform_status(status, &error);
439 	if (ret) {
440 		dev_err(sev->dev,
441 			"SEV: failed to get status. Error: %#x\n", error);
442 		return 1;
443 	}
444 
445 	sev->api_major = status->api_major;
446 	sev->api_minor = status->api_minor;
447 	sev->build = status->build;
448 	sev->state = status->state;
449 
450 	return 0;
451 }
452 
453 static int sev_get_firmware(struct device *dev,
454 			    const struct firmware **firmware)
455 {
456 	char fw_name_specific[SEV_FW_NAME_SIZE];
457 	char fw_name_subset[SEV_FW_NAME_SIZE];
458 
459 	snprintf(fw_name_specific, sizeof(fw_name_specific),
460 		 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
461 		 boot_cpu_data.x86, boot_cpu_data.x86_model);
462 
463 	snprintf(fw_name_subset, sizeof(fw_name_subset),
464 		 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
465 		 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
466 
467 	/* Check for SEV FW for a particular model.
468 	 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
469 	 *
470 	 * or
471 	 *
472 	 * Check for SEV FW common to a subset of models.
473 	 * Ex. amd_sev_fam17h_model0xh.sbin for
474 	 *     Family 17h Model 00h -- Family 17h Model 0Fh
475 	 *
476 	 * or
477 	 *
478 	 * Fall-back to using generic name: sev.fw
479 	 */
480 	if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
481 	    (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
482 	    (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
483 		return 0;
484 
485 	return -ENOENT;
486 }
487 
488 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
489 static int sev_update_firmware(struct device *dev)
490 {
491 	struct sev_data_download_firmware *data;
492 	const struct firmware *firmware;
493 	int ret, error, order;
494 	struct page *p;
495 	u64 data_size;
496 
497 	if (sev_get_firmware(dev, &firmware) == -ENOENT) {
498 		dev_dbg(dev, "No SEV firmware file present\n");
499 		return -1;
500 	}
501 
502 	/*
503 	 * SEV FW expects the physical address given to it to be 32
504 	 * byte aligned. Memory allocated has structure placed at the
505 	 * beginning followed by the firmware being passed to the SEV
506 	 * FW. Allocate enough memory for data structure + alignment
507 	 * padding + SEV FW.
508 	 */
509 	data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
510 
511 	order = get_order(firmware->size + data_size);
512 	p = alloc_pages(GFP_KERNEL, order);
513 	if (!p) {
514 		ret = -1;
515 		goto fw_err;
516 	}
517 
518 	/*
519 	 * Copy firmware data to a kernel allocated contiguous
520 	 * memory region.
521 	 */
522 	data = page_address(p);
523 	memcpy(page_address(p) + data_size, firmware->data, firmware->size);
524 
525 	data->address = __psp_pa(page_address(p) + data_size);
526 	data->len = firmware->size;
527 
528 	ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
529 	if (ret)
530 		dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
531 	else
532 		dev_info(dev, "SEV firmware update successful\n");
533 
534 	__free_pages(p, order);
535 
536 fw_err:
537 	release_firmware(firmware);
538 
539 	return ret;
540 }
541 
542 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
543 {
544 	struct sev_device *sev = psp_master->sev_data;
545 	struct sev_user_data_pek_cert_import input;
546 	struct sev_data_pek_cert_import *data;
547 	void *pek_blob, *oca_blob;
548 	int ret;
549 
550 	if (!writable)
551 		return -EPERM;
552 
553 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
554 		return -EFAULT;
555 
556 	data = kzalloc(sizeof(*data), GFP_KERNEL);
557 	if (!data)
558 		return -ENOMEM;
559 
560 	/* copy PEK certificate blobs from userspace */
561 	pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
562 	if (IS_ERR(pek_blob)) {
563 		ret = PTR_ERR(pek_blob);
564 		goto e_free;
565 	}
566 
567 	data->pek_cert_address = __psp_pa(pek_blob);
568 	data->pek_cert_len = input.pek_cert_len;
569 
570 	/* copy PEK certificate blobs from userspace */
571 	oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
572 	if (IS_ERR(oca_blob)) {
573 		ret = PTR_ERR(oca_blob);
574 		goto e_free_pek;
575 	}
576 
577 	data->oca_cert_address = __psp_pa(oca_blob);
578 	data->oca_cert_len = input.oca_cert_len;
579 
580 	/* If platform is not in INIT state then transition it to INIT */
581 	if (sev->state != SEV_STATE_INIT) {
582 		ret = __sev_platform_init_locked(&argp->error);
583 		if (ret)
584 			goto e_free_oca;
585 	}
586 
587 	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
588 
589 e_free_oca:
590 	kfree(oca_blob);
591 e_free_pek:
592 	kfree(pek_blob);
593 e_free:
594 	kfree(data);
595 	return ret;
596 }
597 
598 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
599 {
600 	struct sev_user_data_get_id2 input;
601 	struct sev_data_get_id *data;
602 	void *id_blob = NULL;
603 	int ret;
604 
605 	/* SEV GET_ID is available from SEV API v0.16 and up */
606 	if (!sev_version_greater_or_equal(0, 16))
607 		return -ENOTSUPP;
608 
609 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
610 		return -EFAULT;
611 
612 	/* Check if we have write access to the userspace buffer */
613 	if (input.address &&
614 	    input.length &&
615 	    !access_ok(input.address, input.length))
616 		return -EFAULT;
617 
618 	data = kzalloc(sizeof(*data), GFP_KERNEL);
619 	if (!data)
620 		return -ENOMEM;
621 
622 	if (input.address && input.length) {
623 		id_blob = kmalloc(input.length, GFP_KERNEL);
624 		if (!id_blob) {
625 			kfree(data);
626 			return -ENOMEM;
627 		}
628 
629 		data->address = __psp_pa(id_blob);
630 		data->len = input.length;
631 	}
632 
633 	ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
634 
635 	/*
636 	 * Firmware will return the length of the ID value (either the minimum
637 	 * required length or the actual length written), return it to the user.
638 	 */
639 	input.length = data->len;
640 
641 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
642 		ret = -EFAULT;
643 		goto e_free;
644 	}
645 
646 	if (id_blob) {
647 		if (copy_to_user((void __user *)input.address,
648 				 id_blob, data->len)) {
649 			ret = -EFAULT;
650 			goto e_free;
651 		}
652 	}
653 
654 e_free:
655 	kfree(id_blob);
656 	kfree(data);
657 
658 	return ret;
659 }
660 
661 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
662 {
663 	struct sev_data_get_id *data;
664 	u64 data_size, user_size;
665 	void *id_blob, *mem;
666 	int ret;
667 
668 	/* SEV GET_ID available from SEV API v0.16 and up */
669 	if (!sev_version_greater_or_equal(0, 16))
670 		return -ENOTSUPP;
671 
672 	/* SEV FW expects the buffer it fills with the ID to be
673 	 * 8-byte aligned. Memory allocated should be enough to
674 	 * hold data structure + alignment padding + memory
675 	 * where SEV FW writes the ID.
676 	 */
677 	data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
678 	user_size = sizeof(struct sev_user_data_get_id);
679 
680 	mem = kzalloc(data_size + user_size, GFP_KERNEL);
681 	if (!mem)
682 		return -ENOMEM;
683 
684 	data = mem;
685 	id_blob = mem + data_size;
686 
687 	data->address = __psp_pa(id_blob);
688 	data->len = user_size;
689 
690 	ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
691 	if (!ret) {
692 		if (copy_to_user((void __user *)argp->data, id_blob, data->len))
693 			ret = -EFAULT;
694 	}
695 
696 	kfree(mem);
697 
698 	return ret;
699 }
700 
701 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
702 {
703 	struct sev_device *sev = psp_master->sev_data;
704 	struct sev_user_data_pdh_cert_export input;
705 	void *pdh_blob = NULL, *cert_blob = NULL;
706 	struct sev_data_pdh_cert_export *data;
707 	int ret;
708 
709 	/* If platform is not in INIT state then transition it to INIT. */
710 	if (sev->state != SEV_STATE_INIT) {
711 		if (!writable)
712 			return -EPERM;
713 
714 		ret = __sev_platform_init_locked(&argp->error);
715 		if (ret)
716 			return ret;
717 	}
718 
719 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
720 		return -EFAULT;
721 
722 	data = kzalloc(sizeof(*data), GFP_KERNEL);
723 	if (!data)
724 		return -ENOMEM;
725 
726 	/* Userspace wants to query the certificate length. */
727 	if (!input.pdh_cert_address ||
728 	    !input.pdh_cert_len ||
729 	    !input.cert_chain_address)
730 		goto cmd;
731 
732 	/* Allocate a physically contiguous buffer to store the PDH blob. */
733 	if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
734 	    !access_ok(input.pdh_cert_address, input.pdh_cert_len)) {
735 		ret = -EFAULT;
736 		goto e_free;
737 	}
738 
739 	/* Allocate a physically contiguous buffer to store the cert chain blob. */
740 	if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
741 	    !access_ok(input.cert_chain_address, input.cert_chain_len)) {
742 		ret = -EFAULT;
743 		goto e_free;
744 	}
745 
746 	pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
747 	if (!pdh_blob) {
748 		ret = -ENOMEM;
749 		goto e_free;
750 	}
751 
752 	data->pdh_cert_address = __psp_pa(pdh_blob);
753 	data->pdh_cert_len = input.pdh_cert_len;
754 
755 	cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
756 	if (!cert_blob) {
757 		ret = -ENOMEM;
758 		goto e_free_pdh;
759 	}
760 
761 	data->cert_chain_address = __psp_pa(cert_blob);
762 	data->cert_chain_len = input.cert_chain_len;
763 
764 cmd:
765 	ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
766 
767 	/* If we query the length, FW responded with expected data. */
768 	input.cert_chain_len = data->cert_chain_len;
769 	input.pdh_cert_len = data->pdh_cert_len;
770 
771 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
772 		ret = -EFAULT;
773 		goto e_free_cert;
774 	}
775 
776 	if (pdh_blob) {
777 		if (copy_to_user((void __user *)input.pdh_cert_address,
778 				 pdh_blob, input.pdh_cert_len)) {
779 			ret = -EFAULT;
780 			goto e_free_cert;
781 		}
782 	}
783 
784 	if (cert_blob) {
785 		if (copy_to_user((void __user *)input.cert_chain_address,
786 				 cert_blob, input.cert_chain_len))
787 			ret = -EFAULT;
788 	}
789 
790 e_free_cert:
791 	kfree(cert_blob);
792 e_free_pdh:
793 	kfree(pdh_blob);
794 e_free:
795 	kfree(data);
796 	return ret;
797 }
798 
799 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
800 {
801 	void __user *argp = (void __user *)arg;
802 	struct sev_issue_cmd input;
803 	int ret = -EFAULT;
804 	bool writable = file->f_mode & FMODE_WRITE;
805 
806 	if (!psp_master || !psp_master->sev_data)
807 		return -ENODEV;
808 
809 	if (ioctl != SEV_ISSUE_CMD)
810 		return -EINVAL;
811 
812 	if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
813 		return -EFAULT;
814 
815 	if (input.cmd > SEV_MAX)
816 		return -EINVAL;
817 
818 	mutex_lock(&sev_cmd_mutex);
819 
820 	switch (input.cmd) {
821 
822 	case SEV_FACTORY_RESET:
823 		ret = sev_ioctl_do_reset(&input, writable);
824 		break;
825 	case SEV_PLATFORM_STATUS:
826 		ret = sev_ioctl_do_platform_status(&input);
827 		break;
828 	case SEV_PEK_GEN:
829 		ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable);
830 		break;
831 	case SEV_PDH_GEN:
832 		ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable);
833 		break;
834 	case SEV_PEK_CSR:
835 		ret = sev_ioctl_do_pek_csr(&input, writable);
836 		break;
837 	case SEV_PEK_CERT_IMPORT:
838 		ret = sev_ioctl_do_pek_import(&input, writable);
839 		break;
840 	case SEV_PDH_CERT_EXPORT:
841 		ret = sev_ioctl_do_pdh_export(&input, writable);
842 		break;
843 	case SEV_GET_ID:
844 		pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
845 		ret = sev_ioctl_do_get_id(&input);
846 		break;
847 	case SEV_GET_ID2:
848 		ret = sev_ioctl_do_get_id2(&input);
849 		break;
850 	default:
851 		ret = -EINVAL;
852 		goto out;
853 	}
854 
855 	if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
856 		ret = -EFAULT;
857 out:
858 	mutex_unlock(&sev_cmd_mutex);
859 
860 	return ret;
861 }
862 
863 static const struct file_operations sev_fops = {
864 	.owner	= THIS_MODULE,
865 	.unlocked_ioctl = sev_ioctl,
866 };
867 
868 int sev_platform_status(struct sev_user_data_status *data, int *error)
869 {
870 	return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
871 }
872 EXPORT_SYMBOL_GPL(sev_platform_status);
873 
874 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
875 {
876 	return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
877 }
878 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
879 
880 int sev_guest_activate(struct sev_data_activate *data, int *error)
881 {
882 	return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
883 }
884 EXPORT_SYMBOL_GPL(sev_guest_activate);
885 
886 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
887 {
888 	return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
889 }
890 EXPORT_SYMBOL_GPL(sev_guest_decommission);
891 
892 int sev_guest_df_flush(int *error)
893 {
894 	return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
895 }
896 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
897 
898 static void sev_exit(struct kref *ref)
899 {
900 	misc_deregister(&misc_dev->misc);
901 	kfree(misc_dev);
902 	misc_dev = NULL;
903 }
904 
905 static int sev_misc_init(struct sev_device *sev)
906 {
907 	struct device *dev = sev->dev;
908 	int ret;
909 
910 	/*
911 	 * SEV feature support can be detected on multiple devices but the SEV
912 	 * FW commands must be issued on the master. During probe, we do not
913 	 * know the master hence we create /dev/sev on the first device probe.
914 	 * sev_do_cmd() finds the right master device to which to issue the
915 	 * command to the firmware.
916 	 */
917 	if (!misc_dev) {
918 		struct miscdevice *misc;
919 
920 		misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
921 		if (!misc_dev)
922 			return -ENOMEM;
923 
924 		misc = &misc_dev->misc;
925 		misc->minor = MISC_DYNAMIC_MINOR;
926 		misc->name = DEVICE_NAME;
927 		misc->fops = &sev_fops;
928 
929 		ret = misc_register(misc);
930 		if (ret)
931 			return ret;
932 
933 		kref_init(&misc_dev->refcount);
934 	} else {
935 		kref_get(&misc_dev->refcount);
936 	}
937 
938 	init_waitqueue_head(&sev->int_queue);
939 	sev->misc = misc_dev;
940 	dev_dbg(dev, "registered SEV device\n");
941 
942 	return 0;
943 }
944 
945 int sev_dev_init(struct psp_device *psp)
946 {
947 	struct device *dev = psp->dev;
948 	struct sev_device *sev;
949 	int ret = -ENOMEM;
950 
951 	sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
952 	if (!sev)
953 		goto e_err;
954 
955 	psp->sev_data = sev;
956 
957 	sev->dev = dev;
958 	sev->psp = psp;
959 
960 	sev->io_regs = psp->io_regs;
961 
962 	sev->vdata = (struct sev_vdata *)psp->vdata->sev;
963 	if (!sev->vdata) {
964 		ret = -ENODEV;
965 		dev_err(dev, "sev: missing driver data\n");
966 		goto e_err;
967 	}
968 
969 	psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
970 
971 	ret = sev_misc_init(sev);
972 	if (ret)
973 		goto e_irq;
974 
975 	dev_notice(dev, "sev enabled\n");
976 
977 	return 0;
978 
979 e_irq:
980 	psp_clear_sev_irq_handler(psp);
981 e_err:
982 	psp->sev_data = NULL;
983 
984 	dev_notice(dev, "sev initialization failed\n");
985 
986 	return ret;
987 }
988 
989 void sev_dev_destroy(struct psp_device *psp)
990 {
991 	struct sev_device *sev = psp->sev_data;
992 
993 	if (!sev)
994 		return;
995 
996 	if (sev->misc)
997 		kref_put(&misc_dev->refcount, sev_exit);
998 
999 	psp_clear_sev_irq_handler(psp);
1000 }
1001 
1002 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1003 				void *data, int *error)
1004 {
1005 	if (!filep || filep->f_op != &sev_fops)
1006 		return -EBADF;
1007 
1008 	return sev_do_cmd(cmd, data, error);
1009 }
1010 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1011 
1012 void sev_pci_init(void)
1013 {
1014 	struct sev_device *sev = psp_master->sev_data;
1015 	int error, rc;
1016 
1017 	if (!sev)
1018 		return;
1019 
1020 	psp_timeout = psp_probe_timeout;
1021 
1022 	if (sev_get_api_version())
1023 		goto err;
1024 
1025 	/*
1026 	 * If platform is not in UNINIT state then firmware upgrade and/or
1027 	 * platform INIT command will fail. These command require UNINIT state.
1028 	 *
1029 	 * In a normal boot we should never run into case where the firmware
1030 	 * is not in UNINIT state on boot. But in case of kexec boot, a reboot
1031 	 * may not go through a typical shutdown sequence and may leave the
1032 	 * firmware in INIT or WORKING state.
1033 	 */
1034 
1035 	if (sev->state != SEV_STATE_UNINIT) {
1036 		sev_platform_shutdown(NULL);
1037 		sev->state = SEV_STATE_UNINIT;
1038 	}
1039 
1040 	if (sev_version_greater_or_equal(0, 15) &&
1041 	    sev_update_firmware(sev->dev) == 0)
1042 		sev_get_api_version();
1043 
1044 	/* Initialize the platform */
1045 	rc = sev_platform_init(&error);
1046 	if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) {
1047 		/*
1048 		 * INIT command returned an integrity check failure
1049 		 * status code, meaning that firmware load and
1050 		 * validation of SEV related persistent data has
1051 		 * failed and persistent state has been erased.
1052 		 * Retrying INIT command here should succeed.
1053 		 */
1054 		dev_dbg(sev->dev, "SEV: retrying INIT command");
1055 		rc = sev_platform_init(&error);
1056 	}
1057 
1058 	if (rc) {
1059 		dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error);
1060 		return;
1061 	}
1062 
1063 	dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
1064 		 sev->api_minor, sev->build);
1065 
1066 	return;
1067 
1068 err:
1069 	psp_master->sev_data = NULL;
1070 }
1071 
1072 void sev_pci_exit(void)
1073 {
1074 	if (!psp_master->sev_data)
1075 		return;
1076 
1077 	sev_platform_shutdown(NULL);
1078 }
1079