1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AMD Secure Encrypted Virtualization (SEV) interface 4 * 5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc. 6 * 7 * Author: Brijesh Singh <brijesh.singh@amd.com> 8 */ 9 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> 23 24 #include <asm/smp.h> 25 26 #include "psp-dev.h" 27 #include "sev-dev.h" 28 29 #define DEVICE_NAME "sev" 30 #define SEV_FW_FILE "amd/sev.fw" 31 #define SEV_FW_NAME_SIZE 64 32 33 static DEFINE_MUTEX(sev_cmd_mutex); 34 static struct sev_misc_dev *misc_dev; 35 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"); 39 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"); 43 44 static bool psp_dead; 45 static int psp_timeout; 46 47 static inline bool sev_version_greater_or_equal(u8 maj, u8 min) 48 { 49 struct sev_device *sev = psp_master->sev_data; 50 51 if (sev->api_major > maj) 52 return true; 53 54 if (sev->api_major == maj && sev->api_minor >= min) 55 return true; 56 57 return false; 58 } 59 60 static void sev_irq_handler(int irq, void *data, unsigned int status) 61 { 62 struct sev_device *sev = data; 63 int reg; 64 65 /* Check if it is command completion: */ 66 if (!(status & SEV_CMD_COMPLETE)) 67 return; 68 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); 74 } 75 } 76 77 static int sev_wait_cmd_ioc(struct sev_device *sev, 78 unsigned int *reg, unsigned int timeout) 79 { 80 int ret; 81 82 ret = wait_event_timeout(sev->int_queue, 83 sev->int_rcvd, timeout * HZ); 84 if (!ret) 85 return -ETIMEDOUT; 86 87 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 88 89 return 0; 90 } 91 92 static int sev_cmd_buffer_len(int cmd) 93 { 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; 123 } 124 125 return 0; 126 } 127 128 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) 129 { 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; 134 135 if (!psp || !psp->sev_data) 136 return -ENODEV; 137 138 if (psp_dead) 139 return -EBUSY; 140 141 sev = psp->sev_data; 142 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; 146 147 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", 148 cmd, phys_msb, phys_lsb, psp_timeout); 149 150 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, 151 sev_cmd_buffer_len(cmd), false); 152 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); 155 156 sev->int_rcvd = 0; 157 158 reg = cmd; 159 reg <<= SEV_CMDRESP_CMD_SHIFT; 160 reg |= SEV_CMDRESP_IOC; 161 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg); 162 163 /* wait for command completion */ 164 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout); 165 if (ret) { 166 if (psp_ret) 167 *psp_ret = 0; 168 169 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd); 170 psp_dead = true; 171 172 return ret; 173 } 174 175 psp_timeout = psp_cmd_timeout; 176 177 if (psp_ret) 178 *psp_ret = reg & PSP_CMDRESP_ERR_MASK; 179 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; 184 } 185 186 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, 187 sev_cmd_buffer_len(cmd), false); 188 189 return ret; 190 } 191 192 static int sev_do_cmd(int cmd, void *data, int *psp_ret) 193 { 194 int rc; 195 196 mutex_lock(&sev_cmd_mutex); 197 rc = __sev_do_cmd_locked(cmd, data, psp_ret); 198 mutex_unlock(&sev_cmd_mutex); 199 200 return rc; 201 } 202 203 static int __sev_platform_init_locked(int *error) 204 { 205 struct psp_device *psp = psp_master; 206 struct sev_device *sev; 207 int rc = 0; 208 209 if (!psp || !psp->sev_data) 210 return -ENODEV; 211 212 sev = psp->sev_data; 213 214 if (sev->state == SEV_STATE_INIT) 215 return 0; 216 217 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error); 218 if (rc) 219 return rc; 220 221 sev->state = SEV_STATE_INIT; 222 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; 228 229 dev_dbg(sev->dev, "SEV firmware initialized\n"); 230 231 return rc; 232 } 233 234 int sev_platform_init(int *error) 235 { 236 int rc; 237 238 mutex_lock(&sev_cmd_mutex); 239 rc = __sev_platform_init_locked(error); 240 mutex_unlock(&sev_cmd_mutex); 241 242 return rc; 243 } 244 EXPORT_SYMBOL_GPL(sev_platform_init); 245 246 static int __sev_platform_shutdown_locked(int *error) 247 { 248 struct sev_device *sev = psp_master->sev_data; 249 int ret; 250 251 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); 252 if (ret) 253 return ret; 254 255 sev->state = SEV_STATE_UNINIT; 256 dev_dbg(sev->dev, "SEV firmware shutdown\n"); 257 258 return ret; 259 } 260 261 static int sev_platform_shutdown(int *error) 262 { 263 int rc; 264 265 mutex_lock(&sev_cmd_mutex); 266 rc = __sev_platform_shutdown_locked(NULL); 267 mutex_unlock(&sev_cmd_mutex); 268 269 return rc; 270 } 271 272 static int sev_get_platform_state(int *state, int *error) 273 { 274 struct sev_device *sev = psp_master->sev_data; 275 int rc; 276 277 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, 278 &sev->status_cmd_buf, error); 279 if (rc) 280 return rc; 281 282 *state = sev->status_cmd_buf.state; 283 return rc; 284 } 285 286 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp) 287 { 288 int state, rc; 289 290 if (!capable(CAP_SYS_ADMIN)) 291 return -EPERM; 292 293 /* 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. 297 * 298 * If FW is in WORKING state then deny the request otherwise issue 299 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET. 300 * 301 */ 302 rc = sev_get_platform_state(&state, &argp->error); 303 if (rc) 304 return rc; 305 306 if (state == SEV_STATE_WORKING) 307 return -EBUSY; 308 309 if (state == SEV_STATE_INIT) { 310 rc = __sev_platform_shutdown_locked(&argp->error); 311 if (rc) 312 return rc; 313 } 314 315 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); 316 } 317 318 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) 319 { 320 struct sev_device *sev = psp_master->sev_data; 321 struct sev_user_data_status *data = &sev->status_cmd_buf; 322 int ret; 323 324 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error); 325 if (ret) 326 return ret; 327 328 if (copy_to_user((void __user *)argp->data, data, sizeof(*data))) 329 ret = -EFAULT; 330 331 return ret; 332 } 333 334 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp) 335 { 336 struct sev_device *sev = psp_master->sev_data; 337 int rc; 338 339 if (!capable(CAP_SYS_ADMIN)) 340 return -EPERM; 341 342 if (sev->state == SEV_STATE_UNINIT) { 343 rc = __sev_platform_init_locked(&argp->error); 344 if (rc) 345 return rc; 346 } 347 348 return __sev_do_cmd_locked(cmd, NULL, &argp->error); 349 } 350 351 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp) 352 { 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; 358 359 if (!capable(CAP_SYS_ADMIN)) 360 return -EPERM; 361 362 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 363 return -EFAULT; 364 365 data = kzalloc(sizeof(*data), GFP_KERNEL); 366 if (!data) 367 return -ENOMEM; 368 369 /* userspace wants to query CSR length */ 370 if (!input.address || !input.length) 371 goto cmd; 372 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; 378 } 379 380 blob = kmalloc(input.length, GFP_KERNEL); 381 if (!blob) { 382 ret = -ENOMEM; 383 goto e_free; 384 } 385 386 data->address = __psp_pa(blob); 387 data->len = input.length; 388 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; 394 } 395 396 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error); 397 398 /* If we query the CSR length, FW responded with expected data. */ 399 input.length = data->len; 400 401 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 402 ret = -EFAULT; 403 goto e_free_blob; 404 } 405 406 if (blob) { 407 if (copy_to_user((void __user *)input.address, blob, input.length)) 408 ret = -EFAULT; 409 } 410 411 e_free_blob: 412 kfree(blob); 413 e_free: 414 kfree(data); 415 return ret; 416 } 417 418 void *psp_copy_user_blob(u64 __user uaddr, u32 len) 419 { 420 if (!uaddr || !len) 421 return ERR_PTR(-EINVAL); 422 423 /* verify that blob length does not exceed our limit */ 424 if (len > SEV_FW_BLOB_MAX_SIZE) 425 return ERR_PTR(-EINVAL); 426 427 return memdup_user((void __user *)(uintptr_t)uaddr, len); 428 } 429 EXPORT_SYMBOL_GPL(psp_copy_user_blob); 430 431 static int sev_get_api_version(void) 432 { 433 struct sev_device *sev = psp_master->sev_data; 434 struct sev_user_data_status *status; 435 int error = 0, ret; 436 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; 443 } 444 445 sev->api_major = status->api_major; 446 sev->api_minor = status->api_minor; 447 sev->build = status->build; 448 sev->state = status->state; 449 450 return 0; 451 } 452 453 static int sev_get_firmware(struct device *dev, 454 const struct firmware **firmware) 455 { 456 char fw_name_specific[SEV_FW_NAME_SIZE]; 457 char fw_name_subset[SEV_FW_NAME_SIZE]; 458 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); 462 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); 466 467 /* Check for SEV FW for a particular model. 468 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h 469 * 470 * or 471 * 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 475 * 476 * or 477 * 478 * Fall-back to using generic name: sev.fw 479 */ 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; 484 485 return -ENOENT; 486 } 487 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) 490 { 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; 496 497 if (sev_get_firmware(dev, &firmware) == -ENOENT) { 498 dev_dbg(dev, "No SEV firmware file present\n"); 499 return -1; 500 } 501 502 /* 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. 508 */ 509 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32); 510 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; 516 } 517 518 /* 519 * Copy firmware data to a kernel allocated contiguous 520 * memory region. 521 */ 522 data = page_address(p); 523 memcpy(page_address(p) + data_size, firmware->data, firmware->size); 524 525 data->address = __psp_pa(page_address(p) + data_size); 526 data->len = firmware->size; 527 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"); 533 534 __free_pages(p, order); 535 536 fw_err: 537 release_firmware(firmware); 538 539 return ret; 540 } 541 542 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp) 543 { 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; 549 550 if (!capable(CAP_SYS_ADMIN)) 551 return -EPERM; 552 553 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 554 return -EFAULT; 555 556 data = kzalloc(sizeof(*data), GFP_KERNEL); 557 if (!data) 558 return -ENOMEM; 559 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; 565 } 566 567 data->pek_cert_address = __psp_pa(pek_blob); 568 data->pek_cert_len = input.pek_cert_len; 569 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; 575 } 576 577 data->oca_cert_address = __psp_pa(oca_blob); 578 data->oca_cert_len = input.oca_cert_len; 579 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; 585 } 586 587 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error); 588 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; 596 } 597 598 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) 599 { 600 struct sev_user_data_get_id2 input; 601 struct sev_data_get_id *data; 602 void *id_blob = NULL; 603 int ret; 604 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; 608 609 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 610 return -EFAULT; 611 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; 617 618 data = kzalloc(sizeof(*data), GFP_KERNEL); 619 if (!data) 620 return -ENOMEM; 621 622 if (input.address && input.length) { 623 id_blob = kmalloc(input.length, GFP_KERNEL); 624 if (!id_blob) { 625 kfree(data); 626 return -ENOMEM; 627 } 628 629 data->address = __psp_pa(id_blob); 630 data->len = input.length; 631 } 632 633 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); 634 635 /* 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. 638 */ 639 input.length = data->len; 640 641 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 642 ret = -EFAULT; 643 goto e_free; 644 } 645 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; 651 } 652 } 653 654 e_free: 655 kfree(id_blob); 656 kfree(data); 657 658 return ret; 659 } 660 661 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp) 662 { 663 struct sev_data_get_id *data; 664 u64 data_size, user_size; 665 void *id_blob, *mem; 666 int ret; 667 668 /* SEV GET_ID available from SEV API v0.16 and up */ 669 if (!sev_version_greater_or_equal(0, 16)) 670 return -ENOTSUPP; 671 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. 676 */ 677 data_size = ALIGN(sizeof(struct sev_data_get_id), 8); 678 user_size = sizeof(struct sev_user_data_get_id); 679 680 mem = kzalloc(data_size + user_size, GFP_KERNEL); 681 if (!mem) 682 return -ENOMEM; 683 684 data = mem; 685 id_blob = mem + data_size; 686 687 data->address = __psp_pa(id_blob); 688 data->len = user_size; 689 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; 694 } 695 696 kfree(mem); 697 698 return ret; 699 } 700 701 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp) 702 { 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; 708 709 /* If platform is not in INIT state then transition it to INIT. */ 710 if (sev->state != SEV_STATE_INIT) { 711 if (!capable(CAP_SYS_ADMIN)) 712 return -EPERM; 713 714 ret = __sev_platform_init_locked(&argp->error); 715 if (ret) 716 return ret; 717 } 718 719 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 720 return -EFAULT; 721 722 data = kzalloc(sizeof(*data), GFP_KERNEL); 723 if (!data) 724 return -ENOMEM; 725 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; 731 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; 737 } 738 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; 744 } 745 746 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL); 747 if (!pdh_blob) { 748 ret = -ENOMEM; 749 goto e_free; 750 } 751 752 data->pdh_cert_address = __psp_pa(pdh_blob); 753 data->pdh_cert_len = input.pdh_cert_len; 754 755 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL); 756 if (!cert_blob) { 757 ret = -ENOMEM; 758 goto e_free_pdh; 759 } 760 761 data->cert_chain_address = __psp_pa(cert_blob); 762 data->cert_chain_len = input.cert_chain_len; 763 764 cmd: 765 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error); 766 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; 770 771 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 772 ret = -EFAULT; 773 goto e_free_cert; 774 } 775 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; 781 } 782 } 783 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; 788 } 789 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; 797 } 798 799 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) 800 { 801 void __user *argp = (void __user *)arg; 802 struct sev_issue_cmd input; 803 int ret = -EFAULT; 804 805 if (!psp_master || !psp_master->sev_data) 806 return -ENODEV; 807 808 if (ioctl != SEV_ISSUE_CMD) 809 return -EINVAL; 810 811 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd))) 812 return -EFAULT; 813 814 if (input.cmd > SEV_MAX) 815 return -EINVAL; 816 817 mutex_lock(&sev_cmd_mutex); 818 819 switch (input.cmd) { 820 821 case SEV_FACTORY_RESET: 822 ret = sev_ioctl_do_reset(&input); 823 break; 824 case SEV_PLATFORM_STATUS: 825 ret = sev_ioctl_do_platform_status(&input); 826 break; 827 case SEV_PEK_GEN: 828 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input); 829 break; 830 case SEV_PDH_GEN: 831 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input); 832 break; 833 case SEV_PEK_CSR: 834 ret = sev_ioctl_do_pek_csr(&input); 835 break; 836 case SEV_PEK_CERT_IMPORT: 837 ret = sev_ioctl_do_pek_import(&input); 838 break; 839 case SEV_PDH_CERT_EXPORT: 840 ret = sev_ioctl_do_pdh_export(&input); 841 break; 842 case SEV_GET_ID: 843 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n"); 844 ret = sev_ioctl_do_get_id(&input); 845 break; 846 case SEV_GET_ID2: 847 ret = sev_ioctl_do_get_id2(&input); 848 break; 849 default: 850 ret = -EINVAL; 851 goto out; 852 } 853 854 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) 855 ret = -EFAULT; 856 out: 857 mutex_unlock(&sev_cmd_mutex); 858 859 return ret; 860 } 861 862 static const struct file_operations sev_fops = { 863 .owner = THIS_MODULE, 864 .unlocked_ioctl = sev_ioctl, 865 }; 866 867 int sev_platform_status(struct sev_user_data_status *data, int *error) 868 { 869 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error); 870 } 871 EXPORT_SYMBOL_GPL(sev_platform_status); 872 873 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) 874 { 875 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error); 876 } 877 EXPORT_SYMBOL_GPL(sev_guest_deactivate); 878 879 int sev_guest_activate(struct sev_data_activate *data, int *error) 880 { 881 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error); 882 } 883 EXPORT_SYMBOL_GPL(sev_guest_activate); 884 885 int sev_guest_decommission(struct sev_data_decommission *data, int *error) 886 { 887 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error); 888 } 889 EXPORT_SYMBOL_GPL(sev_guest_decommission); 890 891 int sev_guest_df_flush(int *error) 892 { 893 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); 894 } 895 EXPORT_SYMBOL_GPL(sev_guest_df_flush); 896 897 static void sev_exit(struct kref *ref) 898 { 899 struct sev_misc_dev *misc_dev = container_of(ref, struct sev_misc_dev, refcount); 900 901 misc_deregister(&misc_dev->misc); 902 } 903 904 static int sev_misc_init(struct sev_device *sev) 905 { 906 struct device *dev = sev->dev; 907 int ret; 908 909 /* 910 * SEV feature support can be detected on multiple devices but the SEV 911 * FW commands must be issued on the master. During probe, we do not 912 * know the master hence we create /dev/sev on the first device probe. 913 * sev_do_cmd() finds the right master device to which to issue the 914 * command to the firmware. 915 */ 916 if (!misc_dev) { 917 struct miscdevice *misc; 918 919 misc_dev = devm_kzalloc(dev, sizeof(*misc_dev), GFP_KERNEL); 920 if (!misc_dev) 921 return -ENOMEM; 922 923 misc = &misc_dev->misc; 924 misc->minor = MISC_DYNAMIC_MINOR; 925 misc->name = DEVICE_NAME; 926 misc->fops = &sev_fops; 927 928 ret = misc_register(misc); 929 if (ret) 930 return ret; 931 932 kref_init(&misc_dev->refcount); 933 } else { 934 kref_get(&misc_dev->refcount); 935 } 936 937 init_waitqueue_head(&sev->int_queue); 938 sev->misc = misc_dev; 939 dev_dbg(dev, "registered SEV device\n"); 940 941 return 0; 942 } 943 944 int sev_dev_init(struct psp_device *psp) 945 { 946 struct device *dev = psp->dev; 947 struct sev_device *sev; 948 int ret = -ENOMEM; 949 950 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL); 951 if (!sev) 952 goto e_err; 953 954 psp->sev_data = sev; 955 956 sev->dev = dev; 957 sev->psp = psp; 958 959 sev->io_regs = psp->io_regs; 960 961 sev->vdata = (struct sev_vdata *)psp->vdata->sev; 962 if (!sev->vdata) { 963 ret = -ENODEV; 964 dev_err(dev, "sev: missing driver data\n"); 965 goto e_err; 966 } 967 968 psp_set_sev_irq_handler(psp, sev_irq_handler, sev); 969 970 ret = sev_misc_init(sev); 971 if (ret) 972 goto e_irq; 973 974 dev_notice(dev, "sev enabled\n"); 975 976 return 0; 977 978 e_irq: 979 psp_clear_sev_irq_handler(psp); 980 e_err: 981 psp->sev_data = NULL; 982 983 dev_notice(dev, "sev initialization failed\n"); 984 985 return ret; 986 } 987 988 void sev_dev_destroy(struct psp_device *psp) 989 { 990 struct sev_device *sev = psp->sev_data; 991 992 if (!sev) 993 return; 994 995 if (sev->misc) 996 kref_put(&misc_dev->refcount, sev_exit); 997 998 psp_clear_sev_irq_handler(psp); 999 } 1000 1001 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, 1002 void *data, int *error) 1003 { 1004 if (!filep || filep->f_op != &sev_fops) 1005 return -EBADF; 1006 1007 return sev_do_cmd(cmd, data, error); 1008 } 1009 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); 1010 1011 void sev_pci_init(void) 1012 { 1013 struct sev_device *sev = psp_master->sev_data; 1014 int error, rc; 1015 1016 if (!sev) 1017 return; 1018 1019 psp_timeout = psp_probe_timeout; 1020 1021 if (sev_get_api_version()) 1022 goto err; 1023 1024 /* 1025 * If platform is not in UNINIT state then firmware upgrade and/or 1026 * platform INIT command will fail. These command require UNINIT state. 1027 * 1028 * In a normal boot we should never run into case where the firmware 1029 * is not in UNINIT state on boot. But in case of kexec boot, a reboot 1030 * may not go through a typical shutdown sequence and may leave the 1031 * firmware in INIT or WORKING state. 1032 */ 1033 1034 if (sev->state != SEV_STATE_UNINIT) { 1035 sev_platform_shutdown(NULL); 1036 sev->state = SEV_STATE_UNINIT; 1037 } 1038 1039 if (sev_version_greater_or_equal(0, 15) && 1040 sev_update_firmware(sev->dev) == 0) 1041 sev_get_api_version(); 1042 1043 /* Initialize the platform */ 1044 rc = sev_platform_init(&error); 1045 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) { 1046 /* 1047 * INIT command returned an integrity check failure 1048 * status code, meaning that firmware load and 1049 * validation of SEV related persistent data has 1050 * failed and persistent state has been erased. 1051 * Retrying INIT command here should succeed. 1052 */ 1053 dev_dbg(sev->dev, "SEV: retrying INIT command"); 1054 rc = sev_platform_init(&error); 1055 } 1056 1057 if (rc) { 1058 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error); 1059 return; 1060 } 1061 1062 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, 1063 sev->api_minor, sev->build); 1064 1065 return; 1066 1067 err: 1068 psp_master->sev_data = NULL; 1069 } 1070 1071 void sev_pci_exit(void) 1072 { 1073 if (!psp_master->sev_data) 1074 return; 1075 1076 sev_platform_shutdown(NULL); 1077 } 1078