dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / crypto / ccp / sev-dev.c
blob896f190b9a502562557f9575c6d3f585eacdc794
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AMD Secure Encrypted Virtualization (SEV) interface
5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
7 * Author: Brijesh Singh <brijesh.singh@amd.com>
8 */
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>
24 #include <asm/smp.h>
26 #include "psp-dev.h"
27 #include "sev-dev.h"
29 #define DEVICE_NAME "sev"
30 #define SEV_FW_FILE "amd/sev.fw"
31 #define SEV_FW_NAME_SIZE 64
33 static DEFINE_MUTEX(sev_cmd_mutex);
34 static struct sev_misc_dev *misc_dev;
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");
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");
44 static bool psp_dead;
45 static int psp_timeout;
47 static inline bool sev_version_greater_or_equal(u8 maj, u8 min)
49 struct sev_device *sev = psp_master->sev_data;
51 if (sev->api_major > maj)
52 return true;
54 if (sev->api_major == maj && sev->api_minor >= min)
55 return true;
57 return false;
60 static void sev_irq_handler(int irq, void *data, unsigned int status)
62 struct sev_device *sev = data;
63 int reg;
65 /* Check if it is command completion: */
66 if (!(status & SEV_CMD_COMPLETE))
67 return;
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);
77 static int sev_wait_cmd_ioc(struct sev_device *sev,
78 unsigned int *reg, unsigned int timeout)
80 int ret;
82 ret = wait_event_timeout(sev->int_queue,
83 sev->int_rcvd, timeout * HZ);
84 if (!ret)
85 return -ETIMEDOUT;
87 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg);
89 return 0;
92 static int sev_cmd_buffer_len(int cmd)
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;
125 return 0;
128 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
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;
135 if (!psp || !psp->sev_data)
136 return -ENODEV;
138 if (psp_dead)
139 return -EBUSY;
141 sev = psp->sev_data;
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;
147 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n",
148 cmd, phys_msb, phys_lsb, psp_timeout);
150 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
151 sev_cmd_buffer_len(cmd), false);
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);
156 sev->int_rcvd = 0;
158 reg = cmd;
159 reg <<= SEV_CMDRESP_CMD_SHIFT;
160 reg |= SEV_CMDRESP_IOC;
161 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg);
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;
169 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd);
170 psp_dead = true;
172 return ret;
175 psp_timeout = psp_cmd_timeout;
177 if (psp_ret)
178 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
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;
186 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
187 sev_cmd_buffer_len(cmd), false);
189 return ret;
192 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
194 int rc;
196 mutex_lock(&sev_cmd_mutex);
197 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
198 mutex_unlock(&sev_cmd_mutex);
200 return rc;
203 static int __sev_platform_init_locked(int *error)
205 struct psp_device *psp = psp_master;
206 struct sev_device *sev;
207 int rc = 0;
209 if (!psp || !psp->sev_data)
210 return -ENODEV;
212 sev = psp->sev_data;
214 if (sev->state == SEV_STATE_INIT)
215 return 0;
217 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error);
218 if (rc)
219 return rc;
221 sev->state = SEV_STATE_INIT;
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;
229 dev_dbg(sev->dev, "SEV firmware initialized\n");
231 return rc;
234 int sev_platform_init(int *error)
236 int rc;
238 mutex_lock(&sev_cmd_mutex);
239 rc = __sev_platform_init_locked(error);
240 mutex_unlock(&sev_cmd_mutex);
242 return rc;
244 EXPORT_SYMBOL_GPL(sev_platform_init);
246 static int __sev_platform_shutdown_locked(int *error)
248 struct sev_device *sev = psp_master->sev_data;
249 int ret;
251 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
252 if (ret)
253 return ret;
255 sev->state = SEV_STATE_UNINIT;
256 dev_dbg(sev->dev, "SEV firmware shutdown\n");
258 return ret;
261 static int sev_platform_shutdown(int *error)
263 int rc;
265 mutex_lock(&sev_cmd_mutex);
266 rc = __sev_platform_shutdown_locked(NULL);
267 mutex_unlock(&sev_cmd_mutex);
269 return rc;
272 static int sev_get_platform_state(int *state, int *error)
274 struct sev_device *sev = psp_master->sev_data;
275 int rc;
277 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
278 &sev->status_cmd_buf, error);
279 if (rc)
280 return rc;
282 *state = sev->status_cmd_buf.state;
283 return rc;
286 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
288 int state, rc;
290 if (!writable)
291 return -EPERM;
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.
298 * If FW is in WORKING state then deny the request otherwise issue
299 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
302 rc = sev_get_platform_state(&state, &argp->error);
303 if (rc)
304 return rc;
306 if (state == SEV_STATE_WORKING)
307 return -EBUSY;
309 if (state == SEV_STATE_INIT) {
310 rc = __sev_platform_shutdown_locked(&argp->error);
311 if (rc)
312 return rc;
315 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
318 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
320 struct sev_device *sev = psp_master->sev_data;
321 struct sev_user_data_status *data = &sev->status_cmd_buf;
322 int ret;
324 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
325 if (ret)
326 return ret;
328 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
329 ret = -EFAULT;
331 return ret;
334 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
336 struct sev_device *sev = psp_master->sev_data;
337 int rc;
339 if (!writable)
340 return -EPERM;
342 if (sev->state == SEV_STATE_UNINIT) {
343 rc = __sev_platform_init_locked(&argp->error);
344 if (rc)
345 return rc;
348 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
351 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
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;
359 if (!writable)
360 return -EPERM;
362 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
363 return -EFAULT;
365 data = kzalloc(sizeof(*data), GFP_KERNEL);
366 if (!data)
367 return -ENOMEM;
369 /* userspace wants to query CSR length */
370 if (!input.address || !input.length)
371 goto cmd;
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;
380 blob = kmalloc(input.length, GFP_KERNEL);
381 if (!blob) {
382 ret = -ENOMEM;
383 goto e_free;
386 data->address = __psp_pa(blob);
387 data->len = input.length;
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;
396 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
398 /* If we query the CSR length, FW responded with expected data. */
399 input.length = data->len;
401 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
402 ret = -EFAULT;
403 goto e_free_blob;
406 if (blob) {
407 if (copy_to_user((void __user *)input.address, blob, input.length))
408 ret = -EFAULT;
411 e_free_blob:
412 kfree(blob);
413 e_free:
414 kfree(data);
415 return ret;
418 void *psp_copy_user_blob(u64 __user uaddr, u32 len)
420 if (!uaddr || !len)
421 return ERR_PTR(-EINVAL);
423 /* verify that blob length does not exceed our limit */
424 if (len > SEV_FW_BLOB_MAX_SIZE)
425 return ERR_PTR(-EINVAL);
427 return memdup_user((void __user *)(uintptr_t)uaddr, len);
429 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
431 static int sev_get_api_version(void)
433 struct sev_device *sev = psp_master->sev_data;
434 struct sev_user_data_status *status;
435 int error = 0, ret;
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;
445 sev->api_major = status->api_major;
446 sev->api_minor = status->api_minor;
447 sev->build = status->build;
448 sev->state = status->state;
450 return 0;
453 static int sev_get_firmware(struct device *dev,
454 const struct firmware **firmware)
456 char fw_name_specific[SEV_FW_NAME_SIZE];
457 char fw_name_subset[SEV_FW_NAME_SIZE];
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);
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);
467 /* Check for SEV FW for a particular model.
468 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h
470 * or
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
476 * or
478 * Fall-back to using generic name: sev.fw
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;
485 return -ENOENT;
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)
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;
497 if (sev_get_firmware(dev, &firmware) == -ENOENT) {
498 dev_dbg(dev, "No SEV firmware file present\n");
499 return -1;
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.
509 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
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;
519 * Copy firmware data to a kernel allocated contiguous
520 * memory region.
522 data = page_address(p);
523 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
525 data->address = __psp_pa(page_address(p) + data_size);
526 data->len = firmware->size;
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");
534 __free_pages(p, order);
536 fw_err:
537 release_firmware(firmware);
539 return ret;
542 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
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;
550 if (!writable)
551 return -EPERM;
553 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
554 return -EFAULT;
556 data = kzalloc(sizeof(*data), GFP_KERNEL);
557 if (!data)
558 return -ENOMEM;
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;
567 data->pek_cert_address = __psp_pa(pek_blob);
568 data->pek_cert_len = input.pek_cert_len;
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;
577 data->oca_cert_address = __psp_pa(oca_blob);
578 data->oca_cert_len = input.oca_cert_len;
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;
587 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
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;
598 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
600 struct sev_user_data_get_id2 input;
601 struct sev_data_get_id *data;
602 void *id_blob = NULL;
603 int ret;
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;
609 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
610 return -EFAULT;
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;
618 data = kzalloc(sizeof(*data), GFP_KERNEL);
619 if (!data)
620 return -ENOMEM;
622 if (input.address && input.length) {
623 id_blob = kmalloc(input.length, GFP_KERNEL);
624 if (!id_blob) {
625 kfree(data);
626 return -ENOMEM;
629 data->address = __psp_pa(id_blob);
630 data->len = input.length;
633 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error);
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.
639 input.length = data->len;
641 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
642 ret = -EFAULT;
643 goto e_free;
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;
654 e_free:
655 kfree(id_blob);
656 kfree(data);
658 return ret;
661 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
663 struct sev_data_get_id *data;
664 u64 data_size, user_size;
665 void *id_blob, *mem;
666 int ret;
668 /* SEV GET_ID available from SEV API v0.16 and up */
669 if (!sev_version_greater_or_equal(0, 16))
670 return -ENOTSUPP;
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.
677 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
678 user_size = sizeof(struct sev_user_data_get_id);
680 mem = kzalloc(data_size + user_size, GFP_KERNEL);
681 if (!mem)
682 return -ENOMEM;
684 data = mem;
685 id_blob = mem + data_size;
687 data->address = __psp_pa(id_blob);
688 data->len = user_size;
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;
696 kfree(mem);
698 return ret;
701 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
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;
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;
714 ret = __sev_platform_init_locked(&argp->error);
715 if (ret)
716 return ret;
719 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
720 return -EFAULT;
722 data = kzalloc(sizeof(*data), GFP_KERNEL);
723 if (!data)
724 return -ENOMEM;
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;
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;
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;
746 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
747 if (!pdh_blob) {
748 ret = -ENOMEM;
749 goto e_free;
752 data->pdh_cert_address = __psp_pa(pdh_blob);
753 data->pdh_cert_len = input.pdh_cert_len;
755 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
756 if (!cert_blob) {
757 ret = -ENOMEM;
758 goto e_free_pdh;
761 data->cert_chain_address = __psp_pa(cert_blob);
762 data->cert_chain_len = input.cert_chain_len;
764 cmd:
765 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
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;
771 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
772 ret = -EFAULT;
773 goto e_free_cert;
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;
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;
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;
799 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
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;
806 if (!psp_master || !psp_master->sev_data)
807 return -ENODEV;
809 if (ioctl != SEV_ISSUE_CMD)
810 return -EINVAL;
812 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
813 return -EFAULT;
815 if (input.cmd > SEV_MAX)
816 return -EINVAL;
818 mutex_lock(&sev_cmd_mutex);
820 switch (input.cmd) {
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;
855 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
856 ret = -EFAULT;
857 out:
858 mutex_unlock(&sev_cmd_mutex);
860 return ret;
863 static const struct file_operations sev_fops = {
864 .owner = THIS_MODULE,
865 .unlocked_ioctl = sev_ioctl,
868 int sev_platform_status(struct sev_user_data_status *data, int *error)
870 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
872 EXPORT_SYMBOL_GPL(sev_platform_status);
874 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
876 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
878 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
880 int sev_guest_activate(struct sev_data_activate *data, int *error)
882 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
884 EXPORT_SYMBOL_GPL(sev_guest_activate);
886 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
888 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
890 EXPORT_SYMBOL_GPL(sev_guest_decommission);
892 int sev_guest_df_flush(int *error)
894 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
896 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
898 static void sev_exit(struct kref *ref)
900 misc_deregister(&misc_dev->misc);
901 kfree(misc_dev);
902 misc_dev = NULL;
905 static int sev_misc_init(struct sev_device *sev)
907 struct device *dev = sev->dev;
908 int ret;
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.
917 if (!misc_dev) {
918 struct miscdevice *misc;
920 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
921 if (!misc_dev)
922 return -ENOMEM;
924 misc = &misc_dev->misc;
925 misc->minor = MISC_DYNAMIC_MINOR;
926 misc->name = DEVICE_NAME;
927 misc->fops = &sev_fops;
929 ret = misc_register(misc);
930 if (ret)
931 return ret;
933 kref_init(&misc_dev->refcount);
934 } else {
935 kref_get(&misc_dev->refcount);
938 init_waitqueue_head(&sev->int_queue);
939 sev->misc = misc_dev;
940 dev_dbg(dev, "registered SEV device\n");
942 return 0;
945 int sev_dev_init(struct psp_device *psp)
947 struct device *dev = psp->dev;
948 struct sev_device *sev;
949 int ret = -ENOMEM;
951 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL);
952 if (!sev)
953 goto e_err;
955 psp->sev_data = sev;
957 sev->dev = dev;
958 sev->psp = psp;
960 sev->io_regs = psp->io_regs;
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;
969 psp_set_sev_irq_handler(psp, sev_irq_handler, sev);
971 ret = sev_misc_init(sev);
972 if (ret)
973 goto e_irq;
975 dev_notice(dev, "sev enabled\n");
977 return 0;
979 e_irq:
980 psp_clear_sev_irq_handler(psp);
981 e_err:
982 psp->sev_data = NULL;
984 dev_notice(dev, "sev initialization failed\n");
986 return ret;
989 void sev_dev_destroy(struct psp_device *psp)
991 struct sev_device *sev = psp->sev_data;
993 if (!sev)
994 return;
996 if (sev->misc)
997 kref_put(&misc_dev->refcount, sev_exit);
999 psp_clear_sev_irq_handler(psp);
1002 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
1003 void *data, int *error)
1005 if (!filep || filep->f_op != &sev_fops)
1006 return -EBADF;
1008 return sev_do_cmd(cmd, data, error);
1010 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
1012 void sev_pci_init(void)
1014 struct sev_device *sev = psp_master->sev_data;
1015 int error, rc;
1017 if (!sev)
1018 return;
1020 psp_timeout = psp_probe_timeout;
1022 if (sev_get_api_version())
1023 goto err;
1026 * If platform is not in UNINIT state then firmware upgrade and/or
1027 * platform INIT command will fail. These command require UNINIT state.
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.
1035 if (sev->state != SEV_STATE_UNINIT) {
1036 sev_platform_shutdown(NULL);
1037 sev->state = SEV_STATE_UNINIT;
1040 if (sev_version_greater_or_equal(0, 15) &&
1041 sev_update_firmware(sev->dev) == 0)
1042 sev_get_api_version();
1044 /* Initialize the platform */
1045 rc = sev_platform_init(&error);
1046 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) {
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.
1054 dev_dbg(sev->dev, "SEV: retrying INIT command");
1055 rc = sev_platform_init(&error);
1058 if (rc) {
1059 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error);
1060 return;
1063 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major,
1064 sev->api_minor, sev->build);
1066 return;
1068 err:
1069 psp_master->sev_data = NULL;
1072 void sev_pci_exit(void)
1074 if (!psp_master->sev_data)
1075 return;
1077 sev_platform_shutdown(NULL);