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