staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / crypto / ccp / psp-dev.c
blob218739b961fe37b8c633443173d3026e19df0a69
1 /*
2 * AMD Platform Security Processor (PSP) interface
4 * Copyright (C) 2016-2017 Advanced Micro Devices, Inc.
6 * Author: Brijesh Singh <brijesh.singh@amd.com>
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.
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>
27 #include "sp-dev.h"
28 #include "psp-dev.h"
30 #define SEV_VERSION_GREATER_OR_EQUAL(_maj, _min) \
31 ((psp_master->api_major) >= _maj && \
32 (psp_master->api_minor) >= _min)
34 #define DEVICE_NAME "sev"
35 #define SEV_FW_FILE "amd/sev.fw"
37 static DEFINE_MUTEX(sev_cmd_mutex);
38 static struct sev_misc_dev *misc_dev;
39 static struct psp_device *psp_master;
41 static struct psp_device *psp_alloc_struct(struct sp_device *sp)
43 struct device *dev = sp->dev;
44 struct psp_device *psp;
46 psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL);
47 if (!psp)
48 return NULL;
50 psp->dev = dev;
51 psp->sp = sp;
53 snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord);
55 return psp;
58 static irqreturn_t psp_irq_handler(int irq, void *data)
60 struct psp_device *psp = data;
61 unsigned int status;
62 int reg;
64 /* Read the interrupt status: */
65 status = ioread32(psp->io_regs + psp->vdata->intsts_reg);
67 /* Check if it is command completion: */
68 if (!(status & PSP_CMD_COMPLETE))
69 goto done;
71 /* Check if it is SEV command completion: */
72 reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
73 if (reg & PSP_CMDRESP_RESP) {
74 psp->sev_int_rcvd = 1;
75 wake_up(&psp->sev_int_queue);
78 done:
79 /* Clear the interrupt status by writing the same value we read. */
80 iowrite32(status, psp->io_regs + psp->vdata->intsts_reg);
82 return IRQ_HANDLED;
85 static void sev_wait_cmd_ioc(struct psp_device *psp, unsigned int *reg)
87 wait_event(psp->sev_int_queue, psp->sev_int_rcvd);
88 *reg = ioread32(psp->io_regs + psp->vdata->cmdresp_reg);
91 static int sev_cmd_buffer_len(int cmd)
93 switch (cmd) {
94 case SEV_CMD_INIT: return sizeof(struct sev_data_init);
95 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status);
96 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr);
97 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import);
98 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export);
99 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start);
100 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data);
101 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa);
102 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish);
103 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure);
104 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate);
105 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate);
106 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission);
107 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status);
108 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg);
109 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg);
110 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start);
111 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data);
112 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa);
113 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish);
114 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start);
115 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish);
116 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data);
117 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa);
118 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret);
119 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware);
120 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id);
121 default: return 0;
124 return 0;
127 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
129 struct psp_device *psp = psp_master;
130 unsigned int phys_lsb, phys_msb;
131 unsigned int reg, ret = 0;
133 if (!psp)
134 return -ENODEV;
136 /* Get the physical address of the command buffer */
137 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0;
138 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0;
140 dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x\n",
141 cmd, phys_msb, phys_lsb);
143 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data,
144 sev_cmd_buffer_len(cmd), false);
146 iowrite32(phys_lsb, psp->io_regs + psp->vdata->cmdbuff_addr_lo_reg);
147 iowrite32(phys_msb, psp->io_regs + psp->vdata->cmdbuff_addr_hi_reg);
149 psp->sev_int_rcvd = 0;
151 reg = cmd;
152 reg <<= PSP_CMDRESP_CMD_SHIFT;
153 reg |= PSP_CMDRESP_IOC;
154 iowrite32(reg, psp->io_regs + psp->vdata->cmdresp_reg);
156 /* wait for command completion */
157 sev_wait_cmd_ioc(psp, &reg);
159 if (psp_ret)
160 *psp_ret = reg & PSP_CMDRESP_ERR_MASK;
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;
168 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data,
169 sev_cmd_buffer_len(cmd), false);
171 return ret;
174 static int sev_do_cmd(int cmd, void *data, int *psp_ret)
176 int rc;
178 mutex_lock(&sev_cmd_mutex);
179 rc = __sev_do_cmd_locked(cmd, data, psp_ret);
180 mutex_unlock(&sev_cmd_mutex);
182 return rc;
185 static int __sev_platform_init_locked(int *error)
187 struct psp_device *psp = psp_master;
188 int rc = 0;
190 if (!psp)
191 return -ENODEV;
193 if (psp->sev_state == SEV_STATE_INIT)
194 return 0;
196 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &psp->init_cmd_buf, error);
197 if (rc)
198 return rc;
200 psp->sev_state = SEV_STATE_INIT;
201 dev_dbg(psp->dev, "SEV firmware initialized\n");
203 return rc;
206 int sev_platform_init(int *error)
208 int rc;
210 mutex_lock(&sev_cmd_mutex);
211 rc = __sev_platform_init_locked(error);
212 mutex_unlock(&sev_cmd_mutex);
214 return rc;
216 EXPORT_SYMBOL_GPL(sev_platform_init);
218 static int __sev_platform_shutdown_locked(int *error)
220 int ret;
222 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error);
223 if (ret)
224 return ret;
226 psp_master->sev_state = SEV_STATE_UNINIT;
227 dev_dbg(psp_master->dev, "SEV firmware shutdown\n");
229 return ret;
232 static int sev_platform_shutdown(int *error)
234 int rc;
236 mutex_lock(&sev_cmd_mutex);
237 rc = __sev_platform_shutdown_locked(NULL);
238 mutex_unlock(&sev_cmd_mutex);
240 return rc;
243 static int sev_get_platform_state(int *state, int *error)
245 int rc;
247 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS,
248 &psp_master->status_cmd_buf, error);
249 if (rc)
250 return rc;
252 *state = psp_master->status_cmd_buf.state;
253 return rc;
256 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp)
258 int state, rc;
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.
265 * If FW is in WORKING state then deny the request otherwise issue
266 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET.
269 rc = sev_get_platform_state(&state, &argp->error);
270 if (rc)
271 return rc;
273 if (state == SEV_STATE_WORKING)
274 return -EBUSY;
276 if (state == SEV_STATE_INIT) {
277 rc = __sev_platform_shutdown_locked(&argp->error);
278 if (rc)
279 return rc;
282 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error);
285 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
287 struct sev_user_data_status *data = &psp_master->status_cmd_buf;
288 int ret;
290 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error);
291 if (ret)
292 return ret;
294 if (copy_to_user((void __user *)argp->data, data, sizeof(*data)))
295 ret = -EFAULT;
297 return ret;
300 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp)
302 int rc;
304 if (psp_master->sev_state == SEV_STATE_UNINIT) {
305 rc = __sev_platform_init_locked(&argp->error);
306 if (rc)
307 return rc;
310 return __sev_do_cmd_locked(cmd, NULL, &argp->error);
313 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp)
315 struct sev_user_data_pek_csr input;
316 struct sev_data_pek_csr *data;
317 void *blob = NULL;
318 int ret;
320 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
321 return -EFAULT;
323 data = kzalloc(sizeof(*data), GFP_KERNEL);
324 if (!data)
325 return -ENOMEM;
327 /* userspace wants to query CSR length */
328 if (!input.address || !input.length)
329 goto cmd;
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;
338 blob = kmalloc(input.length, GFP_KERNEL);
339 if (!blob) {
340 ret = -ENOMEM;
341 goto e_free;
344 data->address = __psp_pa(blob);
345 data->len = input.length;
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;
354 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error);
356 /* If we query the CSR length, FW responded with expected data. */
357 input.length = data->len;
359 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
360 ret = -EFAULT;
361 goto e_free_blob;
364 if (blob) {
365 if (copy_to_user((void __user *)input.address, blob, input.length))
366 ret = -EFAULT;
369 e_free_blob:
370 kfree(blob);
371 e_free:
372 kfree(data);
373 return ret;
376 void *psp_copy_user_blob(u64 __user uaddr, u32 len)
378 if (!uaddr || !len)
379 return ERR_PTR(-EINVAL);
381 /* verify that blob length does not exceed our limit */
382 if (len > SEV_FW_BLOB_MAX_SIZE)
383 return ERR_PTR(-EINVAL);
385 return memdup_user((void __user *)(uintptr_t)uaddr, len);
387 EXPORT_SYMBOL_GPL(psp_copy_user_blob);
389 static int sev_get_api_version(void)
391 struct sev_user_data_status *status;
392 int error, ret;
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;
402 psp_master->api_major = status->api_major;
403 psp_master->api_minor = status->api_minor;
404 psp_master->build = status->build;
406 return 0;
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)
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;
418 ret = request_firmware(&firmware, SEV_FW_FILE, dev);
419 if (ret < 0)
420 return -1;
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.
429 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32);
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;
439 * Copy firmware data to a kernel allocated contiguous
440 * memory region.
442 data = page_address(p);
443 memcpy(page_address(p) + data_size, firmware->data, firmware->size);
445 data->address = __psp_pa(page_address(p) + data_size);
446 data->len = firmware->size;
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");
454 __free_pages(p, order);
456 fw_err:
457 release_firmware(firmware);
459 return ret;
462 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp)
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;
469 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
470 return -EFAULT;
472 data = kzalloc(sizeof(*data), GFP_KERNEL);
473 if (!data)
474 return -ENOMEM;
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;
483 data->pek_cert_address = __psp_pa(pek_blob);
484 data->pek_cert_len = input.pek_cert_len;
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;
493 data->oca_cert_address = __psp_pa(oca_blob);
494 data->oca_cert_len = input.oca_cert_len;
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;
503 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error);
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;
514 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp)
516 struct sev_data_get_id *data;
517 u64 data_size, user_size;
518 void *id_blob, *mem;
519 int ret;
521 /* SEV GET_ID available from SEV API v0.16 and up */
522 if (!SEV_VERSION_GREATER_OR_EQUAL(0, 16))
523 return -ENOTSUPP;
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.
530 data_size = ALIGN(sizeof(struct sev_data_get_id), 8);
531 user_size = sizeof(struct sev_user_data_get_id);
533 mem = kzalloc(data_size + user_size, GFP_KERNEL);
534 if (!mem)
535 return -ENOMEM;
537 data = mem;
538 id_blob = mem + data_size;
540 data->address = __psp_pa(id_blob);
541 data->len = user_size;
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;
549 kfree(mem);
551 return ret;
554 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp)
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;
561 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
562 return -EFAULT;
564 data = kzalloc(sizeof(*data), GFP_KERNEL);
565 if (!data)
566 return -ENOMEM;
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;
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;
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;
588 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL);
589 if (!pdh_blob) {
590 ret = -ENOMEM;
591 goto e_free;
594 data->pdh_cert_address = __psp_pa(pdh_blob);
595 data->pdh_cert_len = input.pdh_cert_len;
597 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL);
598 if (!cert_blob) {
599 ret = -ENOMEM;
600 goto e_free_pdh;
603 data->cert_chain_address = __psp_pa(cert_blob);
604 data->cert_chain_len = input.cert_chain_len;
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;
614 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error);
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;
620 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) {
621 ret = -EFAULT;
622 goto e_free_cert;
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;
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;
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;
648 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)
650 void __user *argp = (void __user *)arg;
651 struct sev_issue_cmd input;
652 int ret = -EFAULT;
654 if (!psp_master)
655 return -ENODEV;
657 if (ioctl != SEV_ISSUE_CMD)
658 return -EINVAL;
660 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd)))
661 return -EFAULT;
663 if (input.cmd > SEV_MAX)
664 return -EINVAL;
666 mutex_lock(&sev_cmd_mutex);
668 switch (input.cmd) {
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;
699 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd)))
700 ret = -EFAULT;
701 out:
702 mutex_unlock(&sev_cmd_mutex);
704 return ret;
707 static const struct file_operations sev_fops = {
708 .owner = THIS_MODULE,
709 .unlocked_ioctl = sev_ioctl,
712 int sev_platform_status(struct sev_user_data_status *data, int *error)
714 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error);
716 EXPORT_SYMBOL_GPL(sev_platform_status);
718 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error)
720 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error);
722 EXPORT_SYMBOL_GPL(sev_guest_deactivate);
724 int sev_guest_activate(struct sev_data_activate *data, int *error)
726 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error);
728 EXPORT_SYMBOL_GPL(sev_guest_activate);
730 int sev_guest_decommission(struct sev_data_decommission *data, int *error)
732 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error);
734 EXPORT_SYMBOL_GPL(sev_guest_decommission);
736 int sev_guest_df_flush(int *error)
738 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
740 EXPORT_SYMBOL_GPL(sev_guest_df_flush);
742 static void sev_exit(struct kref *ref)
744 struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount);
746 misc_deregister(&misc_dev->misc);
749 static int sev_misc_init(struct psp_device *psp)
751 struct device *dev = psp->dev;
752 int ret;
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.
761 if (!misc_dev) {
762 struct miscdevice *misc;
764 misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL);
765 if (!misc_dev)
766 return -ENOMEM;
768 misc = &misc_dev->misc;
769 misc->minor = MISC_DYNAMIC_MINOR;
770 misc->name = DEVICE_NAME;
771 misc->fops = &sev_fops;
773 ret = misc_register(misc);
774 if (ret)
775 return ret;
777 kref_init(&misc_dev->refcount);
778 } else {
779 kref_get(&misc_dev->refcount);
782 init_waitqueue_head(&psp->sev_int_queue);
783 psp->sev_misc = misc_dev;
784 dev_dbg(dev, "registered SEV device\n");
786 return 0;
789 static int sev_init(struct psp_device *psp)
791 /* Check if device supports SEV feature */
792 if (!(ioread32(psp->io_regs + psp->vdata->feature_reg) & 1)) {
793 dev_dbg(psp->dev, "device does not support SEV\n");
794 return 1;
797 return sev_misc_init(psp);
800 int psp_dev_init(struct sp_device *sp)
802 struct device *dev = sp->dev;
803 struct psp_device *psp;
804 int ret;
806 ret = -ENOMEM;
807 psp = psp_alloc_struct(sp);
808 if (!psp)
809 goto e_err;
811 sp->psp_data = psp;
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;
820 psp->io_regs = sp->io_map;
822 /* Disable and clear interrupts until ready */
823 iowrite32(0, psp->io_regs + psp->vdata->inten_reg);
824 iowrite32(-1, psp->io_regs + psp->vdata->intsts_reg);
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;
833 ret = sev_init(psp);
834 if (ret)
835 goto e_irq;
837 if (sp->set_psp_master_device)
838 sp->set_psp_master_device(sp);
840 /* Enable interrupt */
841 iowrite32(-1, psp->io_regs + psp->vdata->inten_reg);
843 dev_notice(dev, "psp enabled\n");
845 return 0;
847 e_irq:
848 sp_free_psp_irq(psp->sp, psp);
849 e_err:
850 sp->psp_data = NULL;
852 dev_notice(dev, "psp initialization failed\n");
854 return ret;
857 void psp_dev_destroy(struct sp_device *sp)
859 struct psp_device *psp = sp->psp_data;
861 if (!psp)
862 return;
864 if (psp->sev_misc)
865 kref_put(&misc_dev->refcount, sev_exit);
867 sp_free_psp_irq(sp, psp);
870 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd,
871 void *data, int *error)
873 if (!filep || filep->f_op != &sev_fops)
874 return -EBADF;
876 return sev_do_cmd(cmd, data, error);
878 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
880 void psp_pci_init(void)
882 struct sp_device *sp;
883 int error, rc;
885 sp = sp_get_psp_master_device();
886 if (!sp)
887 return;
889 psp_master = sp->psp_data;
891 if (sev_get_api_version())
892 goto err;
894 if (SEV_VERSION_GREATER_OR_EQUAL(0, 15) &&
895 sev_update_firmware(psp_master->dev) == 0)
896 sev_get_api_version();
898 /* Initialize the platform */
899 rc = sev_platform_init(&error);
900 if (rc) {
901 dev_err(sp->dev, "SEV: failed to INIT error %#x\n", error);
902 goto err;
905 dev_info(sp->dev, "SEV API:%d.%d build:%d\n", psp_master->api_major,
906 psp_master->api_minor, psp_master->build);
908 return;
910 err:
911 psp_master = NULL;
914 void psp_pci_exit(void)
916 if (!psp_master)
917 return;
919 sev_platform_shutdown(NULL);