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