1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2004 IBM Corporation 4 * Copyright (C) 2014 Intel Corporation 5 * 6 * Authors: 7 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> 8 * Leendert van Doorn <leendert@watson.ibm.com> 9 * Dave Safford <safford@watson.ibm.com> 10 * Reiner Sailer <sailer@watson.ibm.com> 11 * Kylene Hall <kjhall@us.ibm.com> 12 * 13 * Maintained by: <tpmdd-devel@lists.sourceforge.net> 14 * 15 * TPM chip management routines. 16 */ 17 18 #include <linux/poll.h> 19 #include <linux/slab.h> 20 #include <linux/mutex.h> 21 #include <linux/spinlock.h> 22 #include <linux/freezer.h> 23 #include <linux/major.h> 24 #include <linux/tpm_eventlog.h> 25 #include <linux/hw_random.h> 26 #include "tpm.h" 27 28 DEFINE_IDR(dev_nums_idr); 29 static DEFINE_MUTEX(idr_lock); 30 31 struct class *tpm_class; 32 struct class *tpmrm_class; 33 dev_t tpm_devt; 34 35 static int tpm_request_locality(struct tpm_chip *chip) 36 { 37 int rc; 38 39 if (!chip->ops->request_locality) 40 return 0; 41 42 rc = chip->ops->request_locality(chip, 0); 43 if (rc < 0) 44 return rc; 45 46 chip->locality = rc; 47 return 0; 48 } 49 50 static void tpm_relinquish_locality(struct tpm_chip *chip) 51 { 52 int rc; 53 54 if (!chip->ops->relinquish_locality) 55 return; 56 57 rc = chip->ops->relinquish_locality(chip, chip->locality); 58 if (rc) 59 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc); 60 61 chip->locality = -1; 62 } 63 64 static int tpm_cmd_ready(struct tpm_chip *chip) 65 { 66 if (!chip->ops->cmd_ready) 67 return 0; 68 69 return chip->ops->cmd_ready(chip); 70 } 71 72 static int tpm_go_idle(struct tpm_chip *chip) 73 { 74 if (!chip->ops->go_idle) 75 return 0; 76 77 return chip->ops->go_idle(chip); 78 } 79 80 static void tpm_clk_enable(struct tpm_chip *chip) 81 { 82 if (chip->ops->clk_enable) 83 chip->ops->clk_enable(chip, true); 84 } 85 86 static void tpm_clk_disable(struct tpm_chip *chip) 87 { 88 if (chip->ops->clk_enable) 89 chip->ops->clk_enable(chip, false); 90 } 91 92 /** 93 * tpm_chip_start() - power on the TPM 94 * @chip: a TPM chip to use 95 * 96 * Return: 97 * * The response length - OK 98 * * -errno - A system error 99 */ 100 int tpm_chip_start(struct tpm_chip *chip) 101 { 102 int ret; 103 104 tpm_clk_enable(chip); 105 106 if (chip->locality == -1) { 107 ret = tpm_request_locality(chip); 108 if (ret) { 109 tpm_clk_disable(chip); 110 return ret; 111 } 112 } 113 114 ret = tpm_cmd_ready(chip); 115 if (ret) { 116 tpm_relinquish_locality(chip); 117 tpm_clk_disable(chip); 118 return ret; 119 } 120 121 return 0; 122 } 123 EXPORT_SYMBOL_GPL(tpm_chip_start); 124 125 /** 126 * tpm_chip_stop() - power off the TPM 127 * @chip: a TPM chip to use 128 * 129 * Return: 130 * * The response length - OK 131 * * -errno - A system error 132 */ 133 void tpm_chip_stop(struct tpm_chip *chip) 134 { 135 tpm_go_idle(chip); 136 tpm_relinquish_locality(chip); 137 tpm_clk_disable(chip); 138 } 139 EXPORT_SYMBOL_GPL(tpm_chip_stop); 140 141 /** 142 * tpm_try_get_ops() - Get a ref to the tpm_chip 143 * @chip: Chip to ref 144 * 145 * The caller must already have some kind of locking to ensure that chip is 146 * valid. This function will lock the chip so that the ops member can be 147 * accessed safely. The locking prevents tpm_chip_unregister from 148 * completing, so it should not be held for long periods. 149 * 150 * Returns -ERRNO if the chip could not be got. 151 */ 152 int tpm_try_get_ops(struct tpm_chip *chip) 153 { 154 int rc = -EIO; 155 156 get_device(&chip->dev); 157 158 down_read(&chip->ops_sem); 159 if (!chip->ops) 160 goto out_ops; 161 162 mutex_lock(&chip->tpm_mutex); 163 rc = tpm_chip_start(chip); 164 if (rc) 165 goto out_lock; 166 167 return 0; 168 out_lock: 169 mutex_unlock(&chip->tpm_mutex); 170 out_ops: 171 up_read(&chip->ops_sem); 172 put_device(&chip->dev); 173 return rc; 174 } 175 EXPORT_SYMBOL_GPL(tpm_try_get_ops); 176 177 /** 178 * tpm_put_ops() - Release a ref to the tpm_chip 179 * @chip: Chip to put 180 * 181 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may 182 * be kfree'd. 183 */ 184 void tpm_put_ops(struct tpm_chip *chip) 185 { 186 tpm_chip_stop(chip); 187 mutex_unlock(&chip->tpm_mutex); 188 up_read(&chip->ops_sem); 189 put_device(&chip->dev); 190 } 191 EXPORT_SYMBOL_GPL(tpm_put_ops); 192 193 /** 194 * tpm_default_chip() - find a TPM chip and get a reference to it 195 */ 196 struct tpm_chip *tpm_default_chip(void) 197 { 198 struct tpm_chip *chip, *res = NULL; 199 int chip_num = 0; 200 int chip_prev; 201 202 mutex_lock(&idr_lock); 203 204 do { 205 chip_prev = chip_num; 206 chip = idr_get_next(&dev_nums_idr, &chip_num); 207 if (chip) { 208 get_device(&chip->dev); 209 res = chip; 210 break; 211 } 212 } while (chip_prev != chip_num); 213 214 mutex_unlock(&idr_lock); 215 216 return res; 217 } 218 EXPORT_SYMBOL_GPL(tpm_default_chip); 219 220 /** 221 * tpm_find_get_ops() - find and reserve a TPM chip 222 * @chip: a &struct tpm_chip instance, %NULL for the default chip 223 * 224 * Finds a TPM chip and reserves its class device and operations. The chip must 225 * be released with tpm_put_ops() after use. 226 * This function is for internal use only. It supports existing TPM callers 227 * by accepting NULL, but those callers should be converted to pass in a chip 228 * directly. 229 * 230 * Return: 231 * A reserved &struct tpm_chip instance. 232 * %NULL if a chip is not found. 233 * %NULL if the chip is not available. 234 */ 235 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip) 236 { 237 int rc; 238 239 if (chip) { 240 if (!tpm_try_get_ops(chip)) 241 return chip; 242 return NULL; 243 } 244 245 chip = tpm_default_chip(); 246 if (!chip) 247 return NULL; 248 rc = tpm_try_get_ops(chip); 249 /* release additional reference we got from tpm_default_chip() */ 250 put_device(&chip->dev); 251 if (rc) 252 return NULL; 253 return chip; 254 } 255 256 /** 257 * tpm_dev_release() - free chip memory and the device number 258 * @dev: the character device for the TPM chip 259 * 260 * This is used as the release function for the character device. 261 */ 262 static void tpm_dev_release(struct device *dev) 263 { 264 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); 265 266 mutex_lock(&idr_lock); 267 idr_remove(&dev_nums_idr, chip->dev_num); 268 mutex_unlock(&idr_lock); 269 270 kfree(chip->log.bios_event_log); 271 kfree(chip->work_space.context_buf); 272 kfree(chip->work_space.session_buf); 273 kfree(chip->allocated_banks); 274 kfree(chip); 275 } 276 277 static void tpm_devs_release(struct device *dev) 278 { 279 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs); 280 281 dump_stack(); 282 283 /* release the master device reference */ 284 put_device(&chip->dev); 285 } 286 287 /** 288 * tpm_class_shutdown() - prepare the TPM device for loss of power. 289 * @dev: device to which the chip is associated. 290 * 291 * Issues a TPM2_Shutdown command prior to loss of power, as required by the 292 * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code. 293 * 294 * Return: always 0 (i.e. success) 295 */ 296 static int tpm_class_shutdown(struct device *dev) 297 { 298 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); 299 300 down_write(&chip->ops_sem); 301 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 302 if (!tpm_chip_start(chip)) { 303 tpm2_shutdown(chip, TPM2_SU_CLEAR); 304 tpm_chip_stop(chip); 305 } 306 } 307 chip->ops = NULL; 308 up_write(&chip->ops_sem); 309 310 return 0; 311 } 312 313 /** 314 * tpm_chip_alloc() - allocate a new struct tpm_chip instance 315 * @pdev: device to which the chip is associated 316 * At this point pdev mst be initialized, but does not have to 317 * be registered 318 * @ops: struct tpm_class_ops instance 319 * 320 * Allocates a new struct tpm_chip instance and assigns a free 321 * device number for it. Must be paired with put_device(&chip->dev). 322 */ 323 struct tpm_chip *tpm_chip_alloc(struct device *pdev, 324 const struct tpm_class_ops *ops) 325 { 326 struct tpm_chip *chip; 327 int rc; 328 329 chip = kzalloc(sizeof(*chip), GFP_KERNEL); 330 if (chip == NULL) 331 return ERR_PTR(-ENOMEM); 332 333 mutex_init(&chip->tpm_mutex); 334 init_rwsem(&chip->ops_sem); 335 336 chip->ops = ops; 337 338 mutex_lock(&idr_lock); 339 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL); 340 mutex_unlock(&idr_lock); 341 if (rc < 0) { 342 dev_err(pdev, "No available tpm device numbers\n"); 343 kfree(chip); 344 return ERR_PTR(rc); 345 } 346 chip->dev_num = rc; 347 348 device_initialize(&chip->dev); 349 device_initialize(&chip->devs); 350 351 chip->dev.class = tpm_class; 352 chip->dev.class->shutdown_pre = tpm_class_shutdown; 353 chip->dev.release = tpm_dev_release; 354 chip->dev.parent = pdev; 355 chip->dev.groups = chip->groups; 356 357 chip->devs.parent = pdev; 358 chip->devs.class = tpmrm_class; 359 chip->devs.release = tpm_devs_release; 360 /* get extra reference on main device to hold on 361 * behalf of devs. This holds the chip structure 362 * while cdevs is in use. The corresponding put 363 * is in the tpm_devs_release (TPM2 only) 364 */ 365 if (chip->flags & TPM_CHIP_FLAG_TPM2) 366 get_device(&chip->dev); 367 368 if (chip->dev_num == 0) 369 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR); 370 else 371 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num); 372 373 chip->devs.devt = 374 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES); 375 376 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num); 377 if (rc) 378 goto out; 379 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num); 380 if (rc) 381 goto out; 382 383 if (!pdev) 384 chip->flags |= TPM_CHIP_FLAG_VIRTUAL; 385 386 cdev_init(&chip->cdev, &tpm_fops); 387 cdev_init(&chip->cdevs, &tpmrm_fops); 388 chip->cdev.owner = THIS_MODULE; 389 chip->cdevs.owner = THIS_MODULE; 390 391 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE); 392 if (rc) { 393 rc = -ENOMEM; 394 goto out; 395 } 396 397 chip->locality = -1; 398 return chip; 399 400 out: 401 put_device(&chip->devs); 402 put_device(&chip->dev); 403 return ERR_PTR(rc); 404 } 405 EXPORT_SYMBOL_GPL(tpm_chip_alloc); 406 407 /** 408 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance 409 * @pdev: parent device to which the chip is associated 410 * @ops: struct tpm_class_ops instance 411 * 412 * Same as tpm_chip_alloc except devm is used to do the put_device 413 */ 414 struct tpm_chip *tpmm_chip_alloc(struct device *pdev, 415 const struct tpm_class_ops *ops) 416 { 417 struct tpm_chip *chip; 418 int rc; 419 420 chip = tpm_chip_alloc(pdev, ops); 421 if (IS_ERR(chip)) 422 return chip; 423 424 rc = devm_add_action_or_reset(pdev, 425 (void (*)(void *)) put_device, 426 &chip->dev); 427 if (rc) 428 return ERR_PTR(rc); 429 430 dev_set_drvdata(pdev, chip); 431 432 return chip; 433 } 434 EXPORT_SYMBOL_GPL(tpmm_chip_alloc); 435 436 static int tpm_add_char_device(struct tpm_chip *chip) 437 { 438 int rc; 439 440 rc = cdev_device_add(&chip->cdev, &chip->dev); 441 if (rc) { 442 dev_err(&chip->dev, 443 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", 444 dev_name(&chip->dev), MAJOR(chip->dev.devt), 445 MINOR(chip->dev.devt), rc); 446 return rc; 447 } 448 449 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 450 rc = cdev_device_add(&chip->cdevs, &chip->devs); 451 if (rc) { 452 dev_err(&chip->devs, 453 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n", 454 dev_name(&chip->devs), MAJOR(chip->devs.devt), 455 MINOR(chip->devs.devt), rc); 456 return rc; 457 } 458 } 459 460 /* Make the chip available. */ 461 mutex_lock(&idr_lock); 462 idr_replace(&dev_nums_idr, chip, chip->dev_num); 463 mutex_unlock(&idr_lock); 464 465 return rc; 466 } 467 468 static void tpm_del_char_device(struct tpm_chip *chip) 469 { 470 cdev_device_del(&chip->cdev, &chip->dev); 471 472 /* Make the chip unavailable. */ 473 mutex_lock(&idr_lock); 474 idr_replace(&dev_nums_idr, NULL, chip->dev_num); 475 mutex_unlock(&idr_lock); 476 477 /* Make the driver uncallable. */ 478 down_write(&chip->ops_sem); 479 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 480 if (!tpm_chip_start(chip)) { 481 tpm2_shutdown(chip, TPM2_SU_CLEAR); 482 tpm_chip_stop(chip); 483 } 484 } 485 chip->ops = NULL; 486 up_write(&chip->ops_sem); 487 } 488 489 static void tpm_del_legacy_sysfs(struct tpm_chip *chip) 490 { 491 struct attribute **i; 492 493 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) 494 return; 495 496 sysfs_remove_link(&chip->dev.parent->kobj, "ppi"); 497 498 for (i = chip->groups[0]->attrs; *i != NULL; ++i) 499 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name); 500 } 501 502 /* For compatibility with legacy sysfs paths we provide symlinks from the 503 * parent dev directory to selected names within the tpm chip directory. Old 504 * kernel versions created these files directly under the parent. 505 */ 506 static int tpm_add_legacy_sysfs(struct tpm_chip *chip) 507 { 508 struct attribute **i; 509 int rc; 510 511 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL)) 512 return 0; 513 514 rc = compat_only_sysfs_link_entry_to_kobj( 515 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL); 516 if (rc && rc != -ENOENT) 517 return rc; 518 519 /* All the names from tpm-sysfs */ 520 for (i = chip->groups[0]->attrs; *i != NULL; ++i) { 521 rc = compat_only_sysfs_link_entry_to_kobj( 522 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL); 523 if (rc) { 524 tpm_del_legacy_sysfs(chip); 525 return rc; 526 } 527 } 528 529 return 0; 530 } 531 532 static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait) 533 { 534 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); 535 536 return tpm_get_random(chip, data, max); 537 } 538 539 static int tpm_add_hwrng(struct tpm_chip *chip) 540 { 541 if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM)) 542 return 0; 543 544 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name), 545 "tpm-rng-%d", chip->dev_num); 546 chip->hwrng.name = chip->hwrng_name; 547 chip->hwrng.read = tpm_hwrng_read; 548 return hwrng_register(&chip->hwrng); 549 } 550 551 static int tpm_get_pcr_allocation(struct tpm_chip *chip) 552 { 553 int rc; 554 555 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ? 556 tpm2_get_pcr_allocation(chip) : 557 tpm1_get_pcr_allocation(chip); 558 559 if (rc > 0) 560 return -ENODEV; 561 562 return rc; 563 } 564 565 /* 566 * tpm_chip_register() - create a character device for the TPM chip 567 * @chip: TPM chip to use. 568 * 569 * Creates a character device for the TPM chip and adds sysfs attributes for 570 * the device. As the last step this function adds the chip to the list of TPM 571 * chips available for in-kernel use. 572 * 573 * This function should be only called after the chip initialization is 574 * complete. 575 */ 576 int tpm_chip_register(struct tpm_chip *chip) 577 { 578 int rc; 579 580 rc = tpm_chip_start(chip); 581 if (rc) 582 return rc; 583 rc = tpm_auto_startup(chip); 584 if (rc) { 585 tpm_chip_stop(chip); 586 return rc; 587 } 588 589 rc = tpm_get_pcr_allocation(chip); 590 tpm_chip_stop(chip); 591 if (rc) 592 return rc; 593 594 tpm_sysfs_add_device(chip); 595 596 tpm_bios_log_setup(chip); 597 598 tpm_add_ppi(chip); 599 600 rc = tpm_add_hwrng(chip); 601 if (rc) 602 goto out_ppi; 603 604 rc = tpm_add_char_device(chip); 605 if (rc) 606 goto out_hwrng; 607 608 rc = tpm_add_legacy_sysfs(chip); 609 if (rc) { 610 tpm_chip_unregister(chip); 611 return rc; 612 } 613 614 return 0; 615 616 out_hwrng: 617 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) 618 hwrng_unregister(&chip->hwrng); 619 out_ppi: 620 tpm_bios_log_teardown(chip); 621 622 return rc; 623 } 624 EXPORT_SYMBOL_GPL(tpm_chip_register); 625 626 /* 627 * tpm_chip_unregister() - release the TPM driver 628 * @chip: TPM chip to use. 629 * 630 * Takes the chip first away from the list of available TPM chips and then 631 * cleans up all the resources reserved by tpm_chip_register(). 632 * 633 * Once this function returns the driver call backs in 'op's will not be 634 * running and will no longer start. 635 * 636 * NOTE: This function should be only called before deinitializing chip 637 * resources. 638 */ 639 void tpm_chip_unregister(struct tpm_chip *chip) 640 { 641 tpm_del_legacy_sysfs(chip); 642 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM)) 643 hwrng_unregister(&chip->hwrng); 644 tpm_bios_log_teardown(chip); 645 if (chip->flags & TPM_CHIP_FLAG_TPM2) 646 cdev_device_del(&chip->cdevs, &chip->devs); 647 tpm_del_char_device(chip); 648 } 649 EXPORT_SYMBOL_GPL(tpm_chip_unregister); 650