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