xref: /openbmc/linux/drivers/crypto/ccp/psp-dev.c (revision 1f327613)
1 /*
2  * AMD Platform Security Processor (PSP) interface
3  *
4  * Copyright (C) 2016,2018 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 	psp_master->sev_state = status->state;
441 
442 	return 0;
443 }
444 
445 static int sev_get_firmware(struct device *dev,
446 			    const struct firmware **firmware)
447 {
448 	char fw_name_specific[SEV_FW_NAME_SIZE];
449 	char fw_name_subset[SEV_FW_NAME_SIZE];
450 
451 	snprintf(fw_name_specific, sizeof(fw_name_specific),
452 		 "amd/amd_sev_fam%.2xh_model%.2xh.sbin",
453 		 boot_cpu_data.x86, boot_cpu_data.x86_model);
454 
455 	snprintf(fw_name_subset, sizeof(fw_name_subset),
456 		 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin",
457 		 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4);
458 
459 	/* Check for SEV FW for a particular model.
460 	 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
461 	 *
462 	 * or
463 	 *
464 	 * Check for SEV FW common to a subset of models.
465 	 * Ex. amd_sev_fam17h_model0xh.sbin for
466 	 *     Family 17h Model 00h -- Family 17h Model 0Fh
467 	 *
468 	 * or
469 	 *
470 	 * Fall-back to using generic name: sev.fw
471 	 */
472 	if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) ||
473 	    (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) ||
474 	    (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0))
475 		return 0;
476 
477 	return -ENOENT;
478 }
479 
480 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */
481 static int sev_update_firmware(struct device *dev)
482 {
483 	struct sev_data_download_firmware *data;
484 	const struct firmware *firmware;
485 	int ret, error, order;
486 	struct page *p;
487 	u64 data_size;
488 
489 	if (sev_get_firmware(dev, &firmware) == -ENOENT) {
490 		dev_dbg(dev, "No SEV firmware file present\n");
491 		return -1;
492 	}
493 
494 	/*
495 	 * SEV FW expects the physical address given to it to be 32
496 	 * byte aligned. Memory allocated has structure placed at the
497 	 * beginning followed by the firmware being passed to the SEV
498 	 * FW. Allocate enough memory for data structure + alignment
499 	 * padding + SEV FW.
500 	 */
501 	data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
502 
503 	order = get_order(firmware->size + data_size);
504 	p = alloc_pages(GFP_KERNEL, order);
505 	if (!p) {
506 		ret = -1;
507 		goto fw_err;
508 	}
509 
510 	/*
511 	 * Copy firmware data to a kernel allocated contiguous
512 	 * memory region.
513 	 */
514 	data = page_address(p);
515 	memcpy(page_address(p) + data_size, firmware->data, firmware->size);
516 
517 	data->address = __psp_pa(page_address(p) + data_size);
518 	data->len = firmware->size;
519 
520 	ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error);
521 	if (ret)
522 		dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error);
523 	else
524 		dev_info(dev, "SEV firmware update successful\n");
525 
526 	__free_pages(p, order);
527 
528 fw_err:
529 	release_firmware(firmware);
530 
531 	return ret;
532 }
533 
534 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp)
535 {
536 	struct sev_user_data_pek_cert_import input;
537 	struct sev_data_pek_cert_import *data;
538 	void *pek_blob, *oca_blob;
539 	int ret;
540 
541 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
542 		return -EFAULT;
543 
544 	data = kzalloc(sizeof(*data), GFP_KERNEL);
545 	if (!data)
546 		return -ENOMEM;
547 
548 	/* copy PEK certificate blobs from userspace */
549 	pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len);
550 	if (IS_ERR(pek_blob)) {
551 		ret = PTR_ERR(pek_blob);
552 		goto e_free;
553 	}
554 
555 	data->pek_cert_address = __psp_pa(pek_blob);
556 	data->pek_cert_len = input.pek_cert_len;
557 
558 	/* copy PEK certificate blobs from userspace */
559 	oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len);
560 	if (IS_ERR(oca_blob)) {
561 		ret = PTR_ERR(oca_blob);
562 		goto e_free_pek;
563 	}
564 
565 	data->oca_cert_address = __psp_pa(oca_blob);
566 	data->oca_cert_len = input.oca_cert_len;
567 
568 	/* If platform is not in INIT state then transition it to INIT */
569 	if (psp_master->sev_state != SEV_STATE_INIT) {
570 		ret = __sev_platform_init_locked(&argp->error);
571 		if (ret)
572 			goto e_free_oca;
573 	}
574 
575 	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
576 
577 e_free_oca:
578 	kfree(oca_blob);
579 e_free_pek:
580 	kfree(pek_blob);
581 e_free:
582 	kfree(data);
583 	return ret;
584 }
585 
586 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
587 {
588 	struct sev_user_data_get_id2 input;
589 	struct sev_data_get_id *data;
590 	void *id_blob = NULL;
591 	int ret;
592 
593 	/* SEV GET_ID is available from SEV API v0.16 and up */
594 	if (!SEV_VERSION_GREATER_OR_EQUAL(0, 16))
595 		return -ENOTSUPP;
596 
597 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
598 		return -EFAULT;
599 
600 	/* Check if we have write access to the userspace buffer */
601 	if (input.address &&
602 	    input.length &&
603 	    !access_ok(input.address, input.length))
604 		return -EFAULT;
605 
606 	data = kzalloc(sizeof(*data), GFP_KERNEL);
607 	if (!data)
608 		return -ENOMEM;
609 
610 	if (input.address && input.length) {
611 		id_blob = kmalloc(input.length, GFP_KERNEL);
612 		if (!id_blob) {
613 			kfree(data);
614 			return -ENOMEM;
615 		}
616 
617 		data->address = __psp_pa(id_blob);
618 		data->len = input.length;
619 	}
620 
621 	ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
622 
623 	/*
624 	 * Firmware will return the length of the ID value (either the minimum
625 	 * required length or the actual length written), return it to the user.
626 	 */
627 	input.length = data->len;
628 
629 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
630 		ret = -EFAULT;
631 		goto e_free;
632 	}
633 
634 	if (id_blob) {
635 		if (copy_to_user((void __user *)input.address,
636 				 id_blob, data->len)) {
637 			ret = -EFAULT;
638 			goto e_free;
639 		}
640 	}
641 
642 e_free:
643 	kfree(id_blob);
644 	kfree(data);
645 
646 	return ret;
647 }
648 
649 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
650 {
651 	struct sev_data_get_id *data;
652 	u64 data_size, user_size;
653 	void *id_blob, *mem;
654 	int ret;
655 
656 	/* SEV GET_ID available from SEV API v0.16 and up */
657 	if (!SEV_VERSION_GREATER_OR_EQUAL(0, 16))
658 		return -ENOTSUPP;
659 
660 	/* SEV FW expects the buffer it fills with the ID to be
661 	 * 8-byte aligned. Memory allocated should be enough to
662 	 * hold data structure + alignment padding + memory
663 	 * where SEV FW writes the ID.
664 	 */
665 	data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
666 	user_size = sizeof(struct sev_user_data_get_id);
667 
668 	mem = kzalloc(data_size + user_size, GFP_KERNEL);
669 	if (!mem)
670 		return -ENOMEM;
671 
672 	data = mem;
673 	id_blob = mem + data_size;
674 
675 	data->address = __psp_pa(id_blob);
676 	data->len = user_size;
677 
678 	ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
679 	if (!ret) {
680 		if (copy_to_user((void __user *)argp->data, id_blob, data->len))
681 			ret = -EFAULT;
682 	}
683 
684 	kfree(mem);
685 
686 	return ret;
687 }
688 
689 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp)
690 {
691 	struct sev_user_data_pdh_cert_export input;
692 	void *pdh_blob = NULL, *cert_blob = NULL;
693 	struct sev_data_pdh_cert_export *data;
694 	int ret;
695 
696 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
697 		return -EFAULT;
698 
699 	data = kzalloc(sizeof(*data), GFP_KERNEL);
700 	if (!data)
701 		return -ENOMEM;
702 
703 	/* Userspace wants to query the certificate length. */
704 	if (!input.pdh_cert_address ||
705 	    !input.pdh_cert_len ||
706 	    !input.cert_chain_address)
707 		goto cmd;
708 
709 	/* Allocate a physically contiguous buffer to store the PDH blob. */
710 	if ((input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) ||
711 	    !access_ok(input.pdh_cert_address, input.pdh_cert_len)) {
712 		ret = -EFAULT;
713 		goto e_free;
714 	}
715 
716 	/* Allocate a physically contiguous buffer to store the cert chain blob. */
717 	if ((input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) ||
718 	    !access_ok(input.cert_chain_address, input.cert_chain_len)) {
719 		ret = -EFAULT;
720 		goto e_free;
721 	}
722 
723 	pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
724 	if (!pdh_blob) {
725 		ret = -ENOMEM;
726 		goto e_free;
727 	}
728 
729 	data->pdh_cert_address = __psp_pa(pdh_blob);
730 	data->pdh_cert_len = input.pdh_cert_len;
731 
732 	cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
733 	if (!cert_blob) {
734 		ret = -ENOMEM;
735 		goto e_free_pdh;
736 	}
737 
738 	data->cert_chain_address = __psp_pa(cert_blob);
739 	data->cert_chain_len = input.cert_chain_len;
740 
741 cmd:
742 	/* If platform is not in INIT state then transition it to INIT. */
743 	if (psp_master->sev_state != SEV_STATE_INIT) {
744 		ret = __sev_platform_init_locked(&argp->error);
745 		if (ret)
746 			goto e_free_cert;
747 	}
748 
749 	ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
750 
751 	/* If we query the length, FW responded with expected data. */
752 	input.cert_chain_len = data->cert_chain_len;
753 	input.pdh_cert_len = data->pdh_cert_len;
754 
755 	if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
756 		ret = -EFAULT;
757 		goto e_free_cert;
758 	}
759 
760 	if (pdh_blob) {
761 		if (copy_to_user((void __user *)input.pdh_cert_address,
762 				 pdh_blob, input.pdh_cert_len)) {
763 			ret = -EFAULT;
764 			goto e_free_cert;
765 		}
766 	}
767 
768 	if (cert_blob) {
769 		if (copy_to_user((void __user *)input.cert_chain_address,
770 				 cert_blob, input.cert_chain_len))
771 			ret = -EFAULT;
772 	}
773 
774 e_free_cert:
775 	kfree(cert_blob);
776 e_free_pdh:
777 	kfree(pdh_blob);
778 e_free:
779 	kfree(data);
780 	return ret;
781 }
782 
783 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
784 {
785 	void __user *argp = (void __user *)arg;
786 	struct sev_issue_cmd input;
787 	int ret = -EFAULT;
788 
789 	if (!psp_master)
790 		return -ENODEV;
791 
792 	if (ioctl != SEV_ISSUE_CMD)
793 		return -EINVAL;
794 
795 	if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
796 		return -EFAULT;
797 
798 	if (input.cmd > SEV_MAX)
799 		return -EINVAL;
800 
801 	mutex_lock(&sev_cmd_mutex);
802 
803 	switch (input.cmd) {
804 
805 	case SEV_FACTORY_RESET:
806 		ret = sev_ioctl_do_reset(&input);
807 		break;
808 	case SEV_PLATFORM_STATUS:
809 		ret = sev_ioctl_do_platform_status(&input);
810 		break;
811 	case SEV_PEK_GEN:
812 		ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input);
813 		break;
814 	case SEV_PDH_GEN:
815 		ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input);
816 		break;
817 	case SEV_PEK_CSR:
818 		ret = sev_ioctl_do_pek_csr(&input);
819 		break;
820 	case SEV_PEK_CERT_IMPORT:
821 		ret = sev_ioctl_do_pek_import(&input);
822 		break;
823 	case SEV_PDH_CERT_EXPORT:
824 		ret = sev_ioctl_do_pdh_export(&input);
825 		break;
826 	case SEV_GET_ID:
827 		pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n");
828 		ret = sev_ioctl_do_get_id(&input);
829 		break;
830 	case SEV_GET_ID2:
831 		ret = sev_ioctl_do_get_id2(&input);
832 		break;
833 	default:
834 		ret = -EINVAL;
835 		goto out;
836 	}
837 
838 	if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
839 		ret = -EFAULT;
840 out:
841 	mutex_unlock(&sev_cmd_mutex);
842 
843 	return ret;
844 }
845 
846 static const struct file_operations sev_fops = {
847 	.owner	= THIS_MODULE,
848 	.unlocked_ioctl = sev_ioctl,
849 };
850 
851 int sev_platform_status(struct sev_user_data_status *data, int *error)
852 {
853 	return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
854 }
855 EXPORT_SYMBOL_GPL(sev_platform_status);
856 
857 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
858 {
859 	return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
860 }
861 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
862 
863 int sev_guest_activate(struct sev_data_activate *data, int *error)
864 {
865 	return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
866 }
867 EXPORT_SYMBOL_GPL(sev_guest_activate);
868 
869 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
870 {
871 	return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
872 }
873 EXPORT_SYMBOL_GPL(sev_guest_decommission);
874 
875 int sev_guest_df_flush(int *error)
876 {
877 	return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
878 }
879 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
880 
881 static void sev_exit(struct kref *ref)
882 {
883 	struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
884 
885 	misc_deregister(&misc_dev->misc);
886 }
887 
888 static int sev_misc_init(struct psp_device *psp)
889 {
890 	struct device *dev = psp->dev;
891 	int ret;
892 
893 	/*
894 	 * SEV feature support can be detected on multiple devices but the SEV
895 	 * FW commands must be issued on the master. During probe, we do not
896 	 * know the master hence we create /dev/sev on the first device probe.
897 	 * sev_do_cmd() finds the right master device to which to issue the
898 	 * command to the firmware.
899 	 */
900 	if (!misc_dev) {
901 		struct miscdevice *misc;
902 
903 		misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
904 		if (!misc_dev)
905 			return -ENOMEM;
906 
907 		misc = &misc_dev->misc;
908 		misc->minor = MISC_DYNAMIC_MINOR;
909 		misc->name = DEVICE_NAME;
910 		misc->fops = &sev_fops;
911 
912 		ret = misc_register(misc);
913 		if (ret)
914 			return ret;
915 
916 		kref_init(&misc_dev->refcount);
917 	} else {
918 		kref_get(&misc_dev->refcount);
919 	}
920 
921 	init_waitqueue_head(&psp->sev_int_queue);
922 	psp->sev_misc = misc_dev;
923 	dev_dbg(dev, "registered SEV device\n");
924 
925 	return 0;
926 }
927 
928 static int psp_check_sev_support(struct psp_device *psp)
929 {
930 	/* Check if device supports SEV feature */
931 	if (!(ioread32(psp->io_regs + psp->vdata->feature_reg) & 1)) {
932 		dev_dbg(psp->dev, "psp does not support SEV\n");
933 		return -ENODEV;
934 	}
935 
936 	return 0;
937 }
938 
939 int psp_dev_init(struct sp_device *sp)
940 {
941 	struct device *dev = sp->dev;
942 	struct psp_device *psp;
943 	int ret;
944 
945 	ret = -ENOMEM;
946 	psp = psp_alloc_struct(sp);
947 	if (!psp)
948 		goto e_err;
949 
950 	sp->psp_data = psp;
951 
952 	psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata;
953 	if (!psp->vdata) {
954 		ret = -ENODEV;
955 		dev_err(dev, "missing driver data\n");
956 		goto e_err;
957 	}
958 
959 	psp->io_regs = sp->io_map;
960 
961 	ret = psp_check_sev_support(psp);
962 	if (ret)
963 		goto e_disable;
964 
965 	/* Disable and clear interrupts until ready */
966 	iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
967 	iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
968 
969 	/* Request an irq */
970 	ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp);
971 	if (ret) {
972 		dev_err(dev, "psp: unable to allocate an IRQ\n");
973 		goto e_err;
974 	}
975 
976 	ret = sev_misc_init(psp);
977 	if (ret)
978 		goto e_irq;
979 
980 	if (sp->set_psp_master_device)
981 		sp->set_psp_master_device(sp);
982 
983 	/* Enable interrupt */
984 	iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
985 
986 	dev_notice(dev, "psp enabled\n");
987 
988 	return 0;
989 
990 e_irq:
991 	sp_free_psp_irq(psp->sp, psp);
992 e_err:
993 	sp->psp_data = NULL;
994 
995 	dev_notice(dev, "psp initialization failed\n");
996 
997 	return ret;
998 
999 e_disable:
1000 	sp->psp_data = NULL;
1001 
1002 	return ret;
1003 }
1004 
1005 void psp_dev_destroy(struct sp_device *sp)
1006 {
1007 	struct psp_device *psp = sp->psp_data;
1008 
1009 	if (!psp)
1010 		return;
1011 
1012 	if (psp->sev_misc)
1013 		kref_put(&misc_dev->refcount, sev_exit);
1014 
1015 	sp_free_psp_irq(sp, psp);
1016 }
1017 
1018 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1019 				void *data, int *error)
1020 {
1021 	if (!filep || filep->f_op != &sev_fops)
1022 		return -EBADF;
1023 
1024 	return  sev_do_cmd(cmd, data, error);
1025 }
1026 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1027 
1028 void psp_pci_init(void)
1029 {
1030 	struct sp_device *sp;
1031 	int error, rc;
1032 
1033 	sp = sp_get_psp_master_device();
1034 	if (!sp)
1035 		return;
1036 
1037 	psp_master = sp->psp_data;
1038 
1039 	psp_timeout = psp_probe_timeout;
1040 
1041 	if (sev_get_api_version())
1042 		goto err;
1043 
1044 	/*
1045 	 * If platform is not in UNINIT state then firmware upgrade and/or
1046 	 * platform INIT command will fail. These command require UNINIT state.
1047 	 *
1048 	 * In a normal boot we should never run into case where the firmware
1049 	 * is not in UNINIT state on boot. But in case of kexec boot, a reboot
1050 	 * may not go through a typical shutdown sequence and may leave the
1051 	 * firmware in INIT or WORKING state.
1052 	 */
1053 
1054 	if (psp_master->sev_state != SEV_STATE_UNINIT) {
1055 		sev_platform_shutdown(NULL);
1056 		psp_master->sev_state = SEV_STATE_UNINIT;
1057 	}
1058 
1059 	if (SEV_VERSION_GREATER_OR_EQUAL(0, 15) &&
1060 	    sev_update_firmware(psp_master->dev) == 0)
1061 		sev_get_api_version();
1062 
1063 	/* Initialize the platform */
1064 	rc = sev_platform_init(&error);
1065 	if (rc) {
1066 		dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error);
1067 		return;
1068 	}
1069 
1070 	dev_info(sp->dev, "SEV API:%d.%d build:%d\n", psp_master->api_major,
1071 		 psp_master->api_minor, psp_master->build);
1072 
1073 	return;
1074 
1075 err:
1076 	psp_master = NULL;
1077 }
1078 
1079 void psp_pci_exit(void)
1080 {
1081 	if (!psp_master)
1082 		return;
1083 
1084 	sev_platform_shutdown(NULL);
1085 }
1086