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 #include <linux/gfp.h> 24 25 #include <asm/smp.h> 26 27 #include "psp-dev.h" 28 #include "sev-dev.h" 29 30 #define DEVICE_NAME "sev" 31 #define SEV_FW_FILE "amd/sev.fw" 32 #define SEV_FW_NAME_SIZE 64 33 34 static DEFINE_MUTEX(sev_cmd_mutex); 35 static struct sev_misc_dev *misc_dev; 36 37 static int psp_cmd_timeout = 100; 38 module_param(psp_cmd_timeout, int, 0644); 39 MODULE_PARM_DESC(psp_cmd_timeout, " default timeout value, in seconds, for PSP commands"); 40 41 static int psp_probe_timeout = 5; 42 module_param(psp_probe_timeout, int, 0644); 43 MODULE_PARM_DESC(psp_probe_timeout, " default timeout value, in seconds, during PSP device probe"); 44 45 static bool psp_dead; 46 static int psp_timeout; 47 48 /* Trusted Memory Region (TMR): 49 * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator 50 * to allocate the memory, which will return aligned memory for the specified 51 * allocation order. 52 */ 53 #define SEV_ES_TMR_SIZE (1024 * 1024) 54 static void *sev_es_tmr; 55 56 static inline bool sev_version_greater_or_equal(u8 maj, u8 min) 57 { 58 struct sev_device *sev = psp_master->sev_data; 59 60 if (sev->api_major > maj) 61 return true; 62 63 if (sev->api_major == maj && sev->api_minor >= min) 64 return true; 65 66 return false; 67 } 68 69 static void sev_irq_handler(int irq, void *data, unsigned int status) 70 { 71 struct sev_device *sev = data; 72 int reg; 73 74 /* Check if it is command completion: */ 75 if (!(status & SEV_CMD_COMPLETE)) 76 return; 77 78 /* Check if it is SEV command completion: */ 79 reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 80 if (reg & PSP_CMDRESP_RESP) { 81 sev->int_rcvd = 1; 82 wake_up(&sev->int_queue); 83 } 84 } 85 86 static int sev_wait_cmd_ioc(struct sev_device *sev, 87 unsigned int *reg, unsigned int timeout) 88 { 89 int ret; 90 91 ret = wait_event_timeout(sev->int_queue, 92 sev->int_rcvd, timeout * HZ); 93 if (!ret) 94 return -ETIMEDOUT; 95 96 *reg = ioread32(sev->io_regs + sev->vdata->cmdresp_reg); 97 98 return 0; 99 } 100 101 static int sev_cmd_buffer_len(int cmd) 102 { 103 switch (cmd) { 104 case SEV_CMD_INIT: return sizeof(struct sev_data_init); 105 case SEV_CMD_PLATFORM_STATUS: return sizeof(struct sev_user_data_status); 106 case SEV_CMD_PEK_CSR: return sizeof(struct sev_data_pek_csr); 107 case SEV_CMD_PEK_CERT_IMPORT: return sizeof(struct sev_data_pek_cert_import); 108 case SEV_CMD_PDH_CERT_EXPORT: return sizeof(struct sev_data_pdh_cert_export); 109 case SEV_CMD_LAUNCH_START: return sizeof(struct sev_data_launch_start); 110 case SEV_CMD_LAUNCH_UPDATE_DATA: return sizeof(struct sev_data_launch_update_data); 111 case SEV_CMD_LAUNCH_UPDATE_VMSA: return sizeof(struct sev_data_launch_update_vmsa); 112 case SEV_CMD_LAUNCH_FINISH: return sizeof(struct sev_data_launch_finish); 113 case SEV_CMD_LAUNCH_MEASURE: return sizeof(struct sev_data_launch_measure); 114 case SEV_CMD_ACTIVATE: return sizeof(struct sev_data_activate); 115 case SEV_CMD_DEACTIVATE: return sizeof(struct sev_data_deactivate); 116 case SEV_CMD_DECOMMISSION: return sizeof(struct sev_data_decommission); 117 case SEV_CMD_GUEST_STATUS: return sizeof(struct sev_data_guest_status); 118 case SEV_CMD_DBG_DECRYPT: return sizeof(struct sev_data_dbg); 119 case SEV_CMD_DBG_ENCRYPT: return sizeof(struct sev_data_dbg); 120 case SEV_CMD_SEND_START: return sizeof(struct sev_data_send_start); 121 case SEV_CMD_SEND_UPDATE_DATA: return sizeof(struct sev_data_send_update_data); 122 case SEV_CMD_SEND_UPDATE_VMSA: return sizeof(struct sev_data_send_update_vmsa); 123 case SEV_CMD_SEND_FINISH: return sizeof(struct sev_data_send_finish); 124 case SEV_CMD_RECEIVE_START: return sizeof(struct sev_data_receive_start); 125 case SEV_CMD_RECEIVE_FINISH: return sizeof(struct sev_data_receive_finish); 126 case SEV_CMD_RECEIVE_UPDATE_DATA: return sizeof(struct sev_data_receive_update_data); 127 case SEV_CMD_RECEIVE_UPDATE_VMSA: return sizeof(struct sev_data_receive_update_vmsa); 128 case SEV_CMD_LAUNCH_UPDATE_SECRET: return sizeof(struct sev_data_launch_secret); 129 case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware); 130 case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); 131 case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report); 132 default: return 0; 133 } 134 135 return 0; 136 } 137 138 static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) 139 { 140 struct psp_device *psp = psp_master; 141 struct sev_device *sev; 142 unsigned int phys_lsb, phys_msb; 143 unsigned int reg, ret = 0; 144 145 if (!psp || !psp->sev_data) 146 return -ENODEV; 147 148 if (psp_dead) 149 return -EBUSY; 150 151 sev = psp->sev_data; 152 153 /* Get the physical address of the command buffer */ 154 phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0; 155 phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0; 156 157 dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", 158 cmd, phys_msb, phys_lsb, psp_timeout); 159 160 print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, 161 sev_cmd_buffer_len(cmd), false); 162 163 iowrite32(phys_lsb, sev->io_regs + sev->vdata->cmdbuff_addr_lo_reg); 164 iowrite32(phys_msb, sev->io_regs + sev->vdata->cmdbuff_addr_hi_reg); 165 166 sev->int_rcvd = 0; 167 168 reg = cmd; 169 reg <<= SEV_CMDRESP_CMD_SHIFT; 170 reg |= SEV_CMDRESP_IOC; 171 iowrite32(reg, sev->io_regs + sev->vdata->cmdresp_reg); 172 173 /* wait for command completion */ 174 ret = sev_wait_cmd_ioc(sev, ®, psp_timeout); 175 if (ret) { 176 if (psp_ret) 177 *psp_ret = 0; 178 179 dev_err(sev->dev, "sev command %#x timed out, disabling PSP\n", cmd); 180 psp_dead = true; 181 182 return ret; 183 } 184 185 psp_timeout = psp_cmd_timeout; 186 187 if (psp_ret) 188 *psp_ret = reg & PSP_CMDRESP_ERR_MASK; 189 190 if (reg & PSP_CMDRESP_ERR_MASK) { 191 dev_dbg(sev->dev, "sev command %#x failed (%#010x)\n", 192 cmd, reg & PSP_CMDRESP_ERR_MASK); 193 ret = -EIO; 194 } 195 196 print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, 197 sev_cmd_buffer_len(cmd), false); 198 199 return ret; 200 } 201 202 static int sev_do_cmd(int cmd, void *data, int *psp_ret) 203 { 204 int rc; 205 206 mutex_lock(&sev_cmd_mutex); 207 rc = __sev_do_cmd_locked(cmd, data, psp_ret); 208 mutex_unlock(&sev_cmd_mutex); 209 210 return rc; 211 } 212 213 static int __sev_platform_init_locked(int *error) 214 { 215 struct psp_device *psp = psp_master; 216 struct sev_device *sev; 217 int rc = 0; 218 219 if (!psp || !psp->sev_data) 220 return -ENODEV; 221 222 sev = psp->sev_data; 223 224 if (sev->state == SEV_STATE_INIT) 225 return 0; 226 227 if (sev_es_tmr) { 228 u64 tmr_pa; 229 230 /* 231 * Do not include the encryption mask on the physical 232 * address of the TMR (firmware should clear it anyway). 233 */ 234 tmr_pa = __pa(sev_es_tmr); 235 236 sev->init_cmd_buf.flags |= SEV_INIT_FLAGS_SEV_ES; 237 sev->init_cmd_buf.tmr_address = tmr_pa; 238 sev->init_cmd_buf.tmr_len = SEV_ES_TMR_SIZE; 239 } 240 241 rc = __sev_do_cmd_locked(SEV_CMD_INIT, &sev->init_cmd_buf, error); 242 if (rc) 243 return rc; 244 245 sev->state = SEV_STATE_INIT; 246 247 /* Prepare for first SEV guest launch after INIT */ 248 wbinvd_on_all_cpus(); 249 rc = __sev_do_cmd_locked(SEV_CMD_DF_FLUSH, NULL, error); 250 if (rc) 251 return rc; 252 253 dev_dbg(sev->dev, "SEV firmware initialized\n"); 254 255 return rc; 256 } 257 258 int sev_platform_init(int *error) 259 { 260 int rc; 261 262 mutex_lock(&sev_cmd_mutex); 263 rc = __sev_platform_init_locked(error); 264 mutex_unlock(&sev_cmd_mutex); 265 266 return rc; 267 } 268 EXPORT_SYMBOL_GPL(sev_platform_init); 269 270 static int __sev_platform_shutdown_locked(int *error) 271 { 272 struct sev_device *sev = psp_master->sev_data; 273 int ret; 274 275 ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); 276 if (ret) 277 return ret; 278 279 sev->state = SEV_STATE_UNINIT; 280 dev_dbg(sev->dev, "SEV firmware shutdown\n"); 281 282 return ret; 283 } 284 285 static int sev_platform_shutdown(int *error) 286 { 287 int rc; 288 289 mutex_lock(&sev_cmd_mutex); 290 rc = __sev_platform_shutdown_locked(NULL); 291 mutex_unlock(&sev_cmd_mutex); 292 293 return rc; 294 } 295 296 static int sev_get_platform_state(int *state, int *error) 297 { 298 struct sev_device *sev = psp_master->sev_data; 299 int rc; 300 301 rc = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, 302 &sev->status_cmd_buf, error); 303 if (rc) 304 return rc; 305 306 *state = sev->status_cmd_buf.state; 307 return rc; 308 } 309 310 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable) 311 { 312 int state, rc; 313 314 if (!writable) 315 return -EPERM; 316 317 /* 318 * The SEV spec requires that FACTORY_RESET must be issued in 319 * UNINIT state. Before we go further lets check if any guest is 320 * active. 321 * 322 * If FW is in WORKING state then deny the request otherwise issue 323 * SHUTDOWN command do INIT -> UNINIT before issuing the FACTORY_RESET. 324 * 325 */ 326 rc = sev_get_platform_state(&state, &argp->error); 327 if (rc) 328 return rc; 329 330 if (state == SEV_STATE_WORKING) 331 return -EBUSY; 332 333 if (state == SEV_STATE_INIT) { 334 rc = __sev_platform_shutdown_locked(&argp->error); 335 if (rc) 336 return rc; 337 } 338 339 return __sev_do_cmd_locked(SEV_CMD_FACTORY_RESET, NULL, &argp->error); 340 } 341 342 static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp) 343 { 344 struct sev_device *sev = psp_master->sev_data; 345 struct sev_user_data_status *data = &sev->status_cmd_buf; 346 int ret; 347 348 ret = __sev_do_cmd_locked(SEV_CMD_PLATFORM_STATUS, data, &argp->error); 349 if (ret) 350 return ret; 351 352 if (copy_to_user((void __user *)argp->data, data, sizeof(*data))) 353 ret = -EFAULT; 354 355 return ret; 356 } 357 358 static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable) 359 { 360 struct sev_device *sev = psp_master->sev_data; 361 int rc; 362 363 if (!writable) 364 return -EPERM; 365 366 if (sev->state == SEV_STATE_UNINIT) { 367 rc = __sev_platform_init_locked(&argp->error); 368 if (rc) 369 return rc; 370 } 371 372 return __sev_do_cmd_locked(cmd, NULL, &argp->error); 373 } 374 375 static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable) 376 { 377 struct sev_device *sev = psp_master->sev_data; 378 struct sev_user_data_pek_csr input; 379 struct sev_data_pek_csr *data; 380 void __user *input_address; 381 void *blob = NULL; 382 int ret; 383 384 if (!writable) 385 return -EPERM; 386 387 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 388 return -EFAULT; 389 390 data = kzalloc(sizeof(*data), GFP_KERNEL); 391 if (!data) 392 return -ENOMEM; 393 394 /* userspace wants to query CSR length */ 395 if (!input.address || !input.length) 396 goto cmd; 397 398 /* allocate a physically contiguous buffer to store the CSR blob */ 399 input_address = (void __user *)input.address; 400 if (input.length > SEV_FW_BLOB_MAX_SIZE) { 401 ret = -EFAULT; 402 goto e_free; 403 } 404 405 blob = kmalloc(input.length, GFP_KERNEL); 406 if (!blob) { 407 ret = -ENOMEM; 408 goto e_free; 409 } 410 411 data->address = __psp_pa(blob); 412 data->len = input.length; 413 414 cmd: 415 if (sev->state == SEV_STATE_UNINIT) { 416 ret = __sev_platform_init_locked(&argp->error); 417 if (ret) 418 goto e_free_blob; 419 } 420 421 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, data, &argp->error); 422 423 /* If we query the CSR length, FW responded with expected data. */ 424 input.length = data->len; 425 426 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 427 ret = -EFAULT; 428 goto e_free_blob; 429 } 430 431 if (blob) { 432 if (copy_to_user(input_address, blob, input.length)) 433 ret = -EFAULT; 434 } 435 436 e_free_blob: 437 kfree(blob); 438 e_free: 439 kfree(data); 440 return ret; 441 } 442 443 void *psp_copy_user_blob(u64 uaddr, u32 len) 444 { 445 if (!uaddr || !len) 446 return ERR_PTR(-EINVAL); 447 448 /* verify that blob length does not exceed our limit */ 449 if (len > SEV_FW_BLOB_MAX_SIZE) 450 return ERR_PTR(-EINVAL); 451 452 return memdup_user((void __user *)uaddr, len); 453 } 454 EXPORT_SYMBOL_GPL(psp_copy_user_blob); 455 456 static int sev_get_api_version(void) 457 { 458 struct sev_device *sev = psp_master->sev_data; 459 struct sev_user_data_status *status; 460 int error = 0, ret; 461 462 status = &sev->status_cmd_buf; 463 ret = sev_platform_status(status, &error); 464 if (ret) { 465 dev_err(sev->dev, 466 "SEV: failed to get status. Error: %#x\n", error); 467 return 1; 468 } 469 470 sev->api_major = status->api_major; 471 sev->api_minor = status->api_minor; 472 sev->build = status->build; 473 sev->state = status->state; 474 475 return 0; 476 } 477 478 static int sev_get_firmware(struct device *dev, 479 const struct firmware **firmware) 480 { 481 char fw_name_specific[SEV_FW_NAME_SIZE]; 482 char fw_name_subset[SEV_FW_NAME_SIZE]; 483 484 snprintf(fw_name_specific, sizeof(fw_name_specific), 485 "amd/amd_sev_fam%.2xh_model%.2xh.sbin", 486 boot_cpu_data.x86, boot_cpu_data.x86_model); 487 488 snprintf(fw_name_subset, sizeof(fw_name_subset), 489 "amd/amd_sev_fam%.2xh_model%.1xxh.sbin", 490 boot_cpu_data.x86, (boot_cpu_data.x86_model & 0xf0) >> 4); 491 492 /* Check for SEV FW for a particular model. 493 * Ex. amd_sev_fam17h_model00h.sbin for Family 17h Model 00h 494 * 495 * or 496 * 497 * Check for SEV FW common to a subset of models. 498 * Ex. amd_sev_fam17h_model0xh.sbin for 499 * Family 17h Model 00h -- Family 17h Model 0Fh 500 * 501 * or 502 * 503 * Fall-back to using generic name: sev.fw 504 */ 505 if ((firmware_request_nowarn(firmware, fw_name_specific, dev) >= 0) || 506 (firmware_request_nowarn(firmware, fw_name_subset, dev) >= 0) || 507 (firmware_request_nowarn(firmware, SEV_FW_FILE, dev) >= 0)) 508 return 0; 509 510 return -ENOENT; 511 } 512 513 /* Don't fail if SEV FW couldn't be updated. Continue with existing SEV FW */ 514 static int sev_update_firmware(struct device *dev) 515 { 516 struct sev_data_download_firmware *data; 517 const struct firmware *firmware; 518 int ret, error, order; 519 struct page *p; 520 u64 data_size; 521 522 if (sev_get_firmware(dev, &firmware) == -ENOENT) { 523 dev_dbg(dev, "No SEV firmware file present\n"); 524 return -1; 525 } 526 527 /* 528 * SEV FW expects the physical address given to it to be 32 529 * byte aligned. Memory allocated has structure placed at the 530 * beginning followed by the firmware being passed to the SEV 531 * FW. Allocate enough memory for data structure + alignment 532 * padding + SEV FW. 533 */ 534 data_size = ALIGN(sizeof(struct sev_data_download_firmware), 32); 535 536 order = get_order(firmware->size + data_size); 537 p = alloc_pages(GFP_KERNEL, order); 538 if (!p) { 539 ret = -1; 540 goto fw_err; 541 } 542 543 /* 544 * Copy firmware data to a kernel allocated contiguous 545 * memory region. 546 */ 547 data = page_address(p); 548 memcpy(page_address(p) + data_size, firmware->data, firmware->size); 549 550 data->address = __psp_pa(page_address(p) + data_size); 551 data->len = firmware->size; 552 553 ret = sev_do_cmd(SEV_CMD_DOWNLOAD_FIRMWARE, data, &error); 554 if (ret) 555 dev_dbg(dev, "Failed to update SEV firmware: %#x\n", error); 556 else 557 dev_info(dev, "SEV firmware update successful\n"); 558 559 __free_pages(p, order); 560 561 fw_err: 562 release_firmware(firmware); 563 564 return ret; 565 } 566 567 static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) 568 { 569 struct sev_device *sev = psp_master->sev_data; 570 struct sev_user_data_pek_cert_import input; 571 struct sev_data_pek_cert_import *data; 572 void *pek_blob, *oca_blob; 573 int ret; 574 575 if (!writable) 576 return -EPERM; 577 578 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 579 return -EFAULT; 580 581 data = kzalloc(sizeof(*data), GFP_KERNEL); 582 if (!data) 583 return -ENOMEM; 584 585 /* copy PEK certificate blobs from userspace */ 586 pek_blob = psp_copy_user_blob(input.pek_cert_address, input.pek_cert_len); 587 if (IS_ERR(pek_blob)) { 588 ret = PTR_ERR(pek_blob); 589 goto e_free; 590 } 591 592 data->pek_cert_address = __psp_pa(pek_blob); 593 data->pek_cert_len = input.pek_cert_len; 594 595 /* copy PEK certificate blobs from userspace */ 596 oca_blob = psp_copy_user_blob(input.oca_cert_address, input.oca_cert_len); 597 if (IS_ERR(oca_blob)) { 598 ret = PTR_ERR(oca_blob); 599 goto e_free_pek; 600 } 601 602 data->oca_cert_address = __psp_pa(oca_blob); 603 data->oca_cert_len = input.oca_cert_len; 604 605 /* If platform is not in INIT state then transition it to INIT */ 606 if (sev->state != SEV_STATE_INIT) { 607 ret = __sev_platform_init_locked(&argp->error); 608 if (ret) 609 goto e_free_oca; 610 } 611 612 ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error); 613 614 e_free_oca: 615 kfree(oca_blob); 616 e_free_pek: 617 kfree(pek_blob); 618 e_free: 619 kfree(data); 620 return ret; 621 } 622 623 static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) 624 { 625 struct sev_user_data_get_id2 input; 626 struct sev_data_get_id *data; 627 void __user *input_address; 628 void *id_blob = NULL; 629 int ret; 630 631 /* SEV GET_ID is available from SEV API v0.16 and up */ 632 if (!sev_version_greater_or_equal(0, 16)) 633 return -ENOTSUPP; 634 635 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 636 return -EFAULT; 637 638 input_address = (void __user *)input.address; 639 640 data = kzalloc(sizeof(*data), GFP_KERNEL); 641 if (!data) 642 return -ENOMEM; 643 644 if (input.address && input.length) { 645 id_blob = kmalloc(input.length, GFP_KERNEL); 646 if (!id_blob) { 647 kfree(data); 648 return -ENOMEM; 649 } 650 651 data->address = __psp_pa(id_blob); 652 data->len = input.length; 653 } 654 655 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); 656 657 /* 658 * Firmware will return the length of the ID value (either the minimum 659 * required length or the actual length written), return it to the user. 660 */ 661 input.length = data->len; 662 663 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 664 ret = -EFAULT; 665 goto e_free; 666 } 667 668 if (id_blob) { 669 if (copy_to_user(input_address, id_blob, data->len)) { 670 ret = -EFAULT; 671 goto e_free; 672 } 673 } 674 675 e_free: 676 kfree(id_blob); 677 kfree(data); 678 679 return ret; 680 } 681 682 static int sev_ioctl_do_get_id(struct sev_issue_cmd *argp) 683 { 684 struct sev_data_get_id *data; 685 u64 data_size, user_size; 686 void *id_blob, *mem; 687 int ret; 688 689 /* SEV GET_ID available from SEV API v0.16 and up */ 690 if (!sev_version_greater_or_equal(0, 16)) 691 return -ENOTSUPP; 692 693 /* SEV FW expects the buffer it fills with the ID to be 694 * 8-byte aligned. Memory allocated should be enough to 695 * hold data structure + alignment padding + memory 696 * where SEV FW writes the ID. 697 */ 698 data_size = ALIGN(sizeof(struct sev_data_get_id), 8); 699 user_size = sizeof(struct sev_user_data_get_id); 700 701 mem = kzalloc(data_size + user_size, GFP_KERNEL); 702 if (!mem) 703 return -ENOMEM; 704 705 data = mem; 706 id_blob = mem + data_size; 707 708 data->address = __psp_pa(id_blob); 709 data->len = user_size; 710 711 ret = __sev_do_cmd_locked(SEV_CMD_GET_ID, data, &argp->error); 712 if (!ret) { 713 if (copy_to_user((void __user *)argp->data, id_blob, data->len)) 714 ret = -EFAULT; 715 } 716 717 kfree(mem); 718 719 return ret; 720 } 721 722 static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) 723 { 724 struct sev_device *sev = psp_master->sev_data; 725 struct sev_user_data_pdh_cert_export input; 726 void *pdh_blob = NULL, *cert_blob = NULL; 727 struct sev_data_pdh_cert_export *data; 728 void __user *input_cert_chain_address; 729 void __user *input_pdh_cert_address; 730 int ret; 731 732 /* If platform is not in INIT state then transition it to INIT. */ 733 if (sev->state != SEV_STATE_INIT) { 734 if (!writable) 735 return -EPERM; 736 737 ret = __sev_platform_init_locked(&argp->error); 738 if (ret) 739 return ret; 740 } 741 742 if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) 743 return -EFAULT; 744 745 data = kzalloc(sizeof(*data), GFP_KERNEL); 746 if (!data) 747 return -ENOMEM; 748 749 /* Userspace wants to query the certificate length. */ 750 if (!input.pdh_cert_address || 751 !input.pdh_cert_len || 752 !input.cert_chain_address) 753 goto cmd; 754 755 input_pdh_cert_address = (void __user *)input.pdh_cert_address; 756 input_cert_chain_address = (void __user *)input.cert_chain_address; 757 758 /* Allocate a physically contiguous buffer to store the PDH blob. */ 759 if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) { 760 ret = -EFAULT; 761 goto e_free; 762 } 763 764 /* Allocate a physically contiguous buffer to store the cert chain blob. */ 765 if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) { 766 ret = -EFAULT; 767 goto e_free; 768 } 769 770 pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL); 771 if (!pdh_blob) { 772 ret = -ENOMEM; 773 goto e_free; 774 } 775 776 data->pdh_cert_address = __psp_pa(pdh_blob); 777 data->pdh_cert_len = input.pdh_cert_len; 778 779 cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL); 780 if (!cert_blob) { 781 ret = -ENOMEM; 782 goto e_free_pdh; 783 } 784 785 data->cert_chain_address = __psp_pa(cert_blob); 786 data->cert_chain_len = input.cert_chain_len; 787 788 cmd: 789 ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error); 790 791 /* If we query the length, FW responded with expected data. */ 792 input.cert_chain_len = data->cert_chain_len; 793 input.pdh_cert_len = data->pdh_cert_len; 794 795 if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { 796 ret = -EFAULT; 797 goto e_free_cert; 798 } 799 800 if (pdh_blob) { 801 if (copy_to_user(input_pdh_cert_address, 802 pdh_blob, input.pdh_cert_len)) { 803 ret = -EFAULT; 804 goto e_free_cert; 805 } 806 } 807 808 if (cert_blob) { 809 if (copy_to_user(input_cert_chain_address, 810 cert_blob, input.cert_chain_len)) 811 ret = -EFAULT; 812 } 813 814 e_free_cert: 815 kfree(cert_blob); 816 e_free_pdh: 817 kfree(pdh_blob); 818 e_free: 819 kfree(data); 820 return ret; 821 } 822 823 static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) 824 { 825 void __user *argp = (void __user *)arg; 826 struct sev_issue_cmd input; 827 int ret = -EFAULT; 828 bool writable = file->f_mode & FMODE_WRITE; 829 830 if (!psp_master || !psp_master->sev_data) 831 return -ENODEV; 832 833 if (ioctl != SEV_ISSUE_CMD) 834 return -EINVAL; 835 836 if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd))) 837 return -EFAULT; 838 839 if (input.cmd > SEV_MAX) 840 return -EINVAL; 841 842 mutex_lock(&sev_cmd_mutex); 843 844 switch (input.cmd) { 845 846 case SEV_FACTORY_RESET: 847 ret = sev_ioctl_do_reset(&input, writable); 848 break; 849 case SEV_PLATFORM_STATUS: 850 ret = sev_ioctl_do_platform_status(&input); 851 break; 852 case SEV_PEK_GEN: 853 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PEK_GEN, &input, writable); 854 break; 855 case SEV_PDH_GEN: 856 ret = sev_ioctl_do_pek_pdh_gen(SEV_CMD_PDH_GEN, &input, writable); 857 break; 858 case SEV_PEK_CSR: 859 ret = sev_ioctl_do_pek_csr(&input, writable); 860 break; 861 case SEV_PEK_CERT_IMPORT: 862 ret = sev_ioctl_do_pek_import(&input, writable); 863 break; 864 case SEV_PDH_CERT_EXPORT: 865 ret = sev_ioctl_do_pdh_export(&input, writable); 866 break; 867 case SEV_GET_ID: 868 pr_warn_once("SEV_GET_ID command is deprecated, use SEV_GET_ID2\n"); 869 ret = sev_ioctl_do_get_id(&input); 870 break; 871 case SEV_GET_ID2: 872 ret = sev_ioctl_do_get_id2(&input); 873 break; 874 default: 875 ret = -EINVAL; 876 goto out; 877 } 878 879 if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) 880 ret = -EFAULT; 881 out: 882 mutex_unlock(&sev_cmd_mutex); 883 884 return ret; 885 } 886 887 static const struct file_operations sev_fops = { 888 .owner = THIS_MODULE, 889 .unlocked_ioctl = sev_ioctl, 890 }; 891 892 int sev_platform_status(struct sev_user_data_status *data, int *error) 893 { 894 return sev_do_cmd(SEV_CMD_PLATFORM_STATUS, data, error); 895 } 896 EXPORT_SYMBOL_GPL(sev_platform_status); 897 898 int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) 899 { 900 return sev_do_cmd(SEV_CMD_DEACTIVATE, data, error); 901 } 902 EXPORT_SYMBOL_GPL(sev_guest_deactivate); 903 904 int sev_guest_activate(struct sev_data_activate *data, int *error) 905 { 906 return sev_do_cmd(SEV_CMD_ACTIVATE, data, error); 907 } 908 EXPORT_SYMBOL_GPL(sev_guest_activate); 909 910 int sev_guest_decommission(struct sev_data_decommission *data, int *error) 911 { 912 return sev_do_cmd(SEV_CMD_DECOMMISSION, data, error); 913 } 914 EXPORT_SYMBOL_GPL(sev_guest_decommission); 915 916 int sev_guest_df_flush(int *error) 917 { 918 return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error); 919 } 920 EXPORT_SYMBOL_GPL(sev_guest_df_flush); 921 922 static void sev_exit(struct kref *ref) 923 { 924 misc_deregister(&misc_dev->misc); 925 kfree(misc_dev); 926 misc_dev = NULL; 927 } 928 929 static int sev_misc_init(struct sev_device *sev) 930 { 931 struct device *dev = sev->dev; 932 int ret; 933 934 /* 935 * SEV feature support can be detected on multiple devices but the SEV 936 * FW commands must be issued on the master. During probe, we do not 937 * know the master hence we create /dev/sev on the first device probe. 938 * sev_do_cmd() finds the right master device to which to issue the 939 * command to the firmware. 940 */ 941 if (!misc_dev) { 942 struct miscdevice *misc; 943 944 misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL); 945 if (!misc_dev) 946 return -ENOMEM; 947 948 misc = &misc_dev->misc; 949 misc->minor = MISC_DYNAMIC_MINOR; 950 misc->name = DEVICE_NAME; 951 misc->fops = &sev_fops; 952 953 ret = misc_register(misc); 954 if (ret) 955 return ret; 956 957 kref_init(&misc_dev->refcount); 958 } else { 959 kref_get(&misc_dev->refcount); 960 } 961 962 init_waitqueue_head(&sev->int_queue); 963 sev->misc = misc_dev; 964 dev_dbg(dev, "registered SEV device\n"); 965 966 return 0; 967 } 968 969 int sev_dev_init(struct psp_device *psp) 970 { 971 struct device *dev = psp->dev; 972 struct sev_device *sev; 973 int ret = -ENOMEM; 974 975 sev = devm_kzalloc(dev, sizeof(*sev), GFP_KERNEL); 976 if (!sev) 977 goto e_err; 978 979 psp->sev_data = sev; 980 981 sev->dev = dev; 982 sev->psp = psp; 983 984 sev->io_regs = psp->io_regs; 985 986 sev->vdata = (struct sev_vdata *)psp->vdata->sev; 987 if (!sev->vdata) { 988 ret = -ENODEV; 989 dev_err(dev, "sev: missing driver data\n"); 990 goto e_err; 991 } 992 993 psp_set_sev_irq_handler(psp, sev_irq_handler, sev); 994 995 ret = sev_misc_init(sev); 996 if (ret) 997 goto e_irq; 998 999 dev_notice(dev, "sev enabled\n"); 1000 1001 return 0; 1002 1003 e_irq: 1004 psp_clear_sev_irq_handler(psp); 1005 e_err: 1006 psp->sev_data = NULL; 1007 1008 dev_notice(dev, "sev initialization failed\n"); 1009 1010 return ret; 1011 } 1012 1013 void sev_dev_destroy(struct psp_device *psp) 1014 { 1015 struct sev_device *sev = psp->sev_data; 1016 1017 if (!sev) 1018 return; 1019 1020 if (sev->misc) 1021 kref_put(&misc_dev->refcount, sev_exit); 1022 1023 psp_clear_sev_irq_handler(psp); 1024 } 1025 1026 int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, 1027 void *data, int *error) 1028 { 1029 if (!filep || filep->f_op != &sev_fops) 1030 return -EBADF; 1031 1032 return sev_do_cmd(cmd, data, error); 1033 } 1034 EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); 1035 1036 void sev_pci_init(void) 1037 { 1038 struct sev_device *sev = psp_master->sev_data; 1039 struct page *tmr_page; 1040 int error, rc; 1041 1042 if (!sev) 1043 return; 1044 1045 psp_timeout = psp_probe_timeout; 1046 1047 if (sev_get_api_version()) 1048 goto err; 1049 1050 /* 1051 * If platform is not in UNINIT state then firmware upgrade and/or 1052 * platform INIT command will fail. These command require UNINIT state. 1053 * 1054 * In a normal boot we should never run into case where the firmware 1055 * is not in UNINIT state on boot. But in case of kexec boot, a reboot 1056 * may not go through a typical shutdown sequence and may leave the 1057 * firmware in INIT or WORKING state. 1058 */ 1059 1060 if (sev->state != SEV_STATE_UNINIT) { 1061 sev_platform_shutdown(NULL); 1062 sev->state = SEV_STATE_UNINIT; 1063 } 1064 1065 if (sev_version_greater_or_equal(0, 15) && 1066 sev_update_firmware(sev->dev) == 0) 1067 sev_get_api_version(); 1068 1069 /* Obtain the TMR memory area for SEV-ES use */ 1070 tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE)); 1071 if (tmr_page) { 1072 sev_es_tmr = page_address(tmr_page); 1073 } else { 1074 sev_es_tmr = NULL; 1075 dev_warn(sev->dev, 1076 "SEV: TMR allocation failed, SEV-ES support unavailable\n"); 1077 } 1078 1079 /* Initialize the platform */ 1080 rc = sev_platform_init(&error); 1081 if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) { 1082 /* 1083 * INIT command returned an integrity check failure 1084 * status code, meaning that firmware load and 1085 * validation of SEV related persistent data has 1086 * failed and persistent state has been erased. 1087 * Retrying INIT command here should succeed. 1088 */ 1089 dev_dbg(sev->dev, "SEV: retrying INIT command"); 1090 rc = sev_platform_init(&error); 1091 } 1092 1093 if (rc) { 1094 dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error); 1095 return; 1096 } 1097 1098 dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, 1099 sev->api_minor, sev->build); 1100 1101 return; 1102 1103 err: 1104 psp_master->sev_data = NULL; 1105 } 1106 1107 void sev_pci_exit(void) 1108 { 1109 if (!psp_master->sev_data) 1110 return; 1111 1112 sev_platform_shutdown(NULL); 1113 1114 if (sev_es_tmr) { 1115 /* The TMR area was encrypted, flush it from the cache */ 1116 wbinvd_on_all_cpus(); 1117 1118 free_pages((unsigned long)sev_es_tmr, 1119 get_order(SEV_ES_TMR_SIZE)); 1120 sev_es_tmr = NULL; 1121 } 1122 } 1123