1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2005, 2006 IBM Corporation 4 * Copyright (C) 2014, 2015 Intel Corporation 5 * 6 * Authors: 7 * Leendert van Doorn <leendert@watson.ibm.com> 8 * Kylene Hall <kjhall@us.ibm.com> 9 * 10 * Maintained by: <tpmdd-devel@lists.sourceforge.net> 11 * 12 * Device driver for TCG/TCPA TPM (trusted platform module). 13 * Specifications at www.trustedcomputinggroup.org 14 * 15 * This device driver implements the TPM interface as defined in 16 * the TCG TPM Interface Spec version 1.2, revision 1.0. 17 */ 18 #include <linux/init.h> 19 #include <linux/module.h> 20 #include <linux/moduleparam.h> 21 #include <linux/pnp.h> 22 #include <linux/slab.h> 23 #include <linux/interrupt.h> 24 #include <linux/wait.h> 25 #include <linux/acpi.h> 26 #include <linux/freezer.h> 27 #include "tpm.h" 28 #include "tpm_tis_core.h" 29 30 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value); 31 32 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, 33 bool check_cancel, bool *canceled) 34 { 35 u8 status = chip->ops->status(chip); 36 37 *canceled = false; 38 if ((status & mask) == mask) 39 return true; 40 if (check_cancel && chip->ops->req_canceled(chip, status)) { 41 *canceled = true; 42 return true; 43 } 44 return false; 45 } 46 47 static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, 48 unsigned long timeout, wait_queue_head_t *queue, 49 bool check_cancel) 50 { 51 unsigned long stop; 52 long rc; 53 u8 status; 54 bool canceled = false; 55 56 /* check current status */ 57 status = chip->ops->status(chip); 58 if ((status & mask) == mask) 59 return 0; 60 61 stop = jiffies + timeout; 62 63 if (chip->flags & TPM_CHIP_FLAG_IRQ) { 64 again: 65 timeout = stop - jiffies; 66 if ((long)timeout <= 0) 67 return -ETIME; 68 rc = wait_event_interruptible_timeout(*queue, 69 wait_for_tpm_stat_cond(chip, mask, check_cancel, 70 &canceled), 71 timeout); 72 if (rc > 0) { 73 if (canceled) 74 return -ECANCELED; 75 return 0; 76 } 77 if (rc == -ERESTARTSYS && freezing(current)) { 78 clear_thread_flag(TIF_SIGPENDING); 79 goto again; 80 } 81 } else { 82 do { 83 usleep_range(TPM_TIMEOUT_USECS_MIN, 84 TPM_TIMEOUT_USECS_MAX); 85 status = chip->ops->status(chip); 86 if ((status & mask) == mask) 87 return 0; 88 } while (time_before(jiffies, stop)); 89 } 90 return -ETIME; 91 } 92 93 /* Before we attempt to access the TPM we must see that the valid bit is set. 94 * The specification says that this bit is 0 at reset and remains 0 until the 95 * 'TPM has gone through its self test and initialization and has established 96 * correct values in the other bits.' 97 */ 98 static int wait_startup(struct tpm_chip *chip, int l) 99 { 100 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 101 unsigned long stop = jiffies + chip->timeout_a; 102 103 do { 104 int rc; 105 u8 access; 106 107 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access); 108 if (rc < 0) 109 return rc; 110 111 if (access & TPM_ACCESS_VALID) 112 return 0; 113 tpm_msleep(TPM_TIMEOUT); 114 } while (time_before(jiffies, stop)); 115 return -1; 116 } 117 118 static bool check_locality(struct tpm_chip *chip, int l) 119 { 120 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 121 int rc; 122 u8 access; 123 124 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access); 125 if (rc < 0) 126 return false; 127 128 if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID 129 | TPM_ACCESS_REQUEST_USE)) == 130 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { 131 priv->locality = l; 132 return true; 133 } 134 135 return false; 136 } 137 138 static int release_locality(struct tpm_chip *chip, int l) 139 { 140 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 141 142 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY); 143 144 return 0; 145 } 146 147 static int request_locality(struct tpm_chip *chip, int l) 148 { 149 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 150 unsigned long stop, timeout; 151 long rc; 152 153 if (check_locality(chip, l)) 154 return l; 155 156 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE); 157 if (rc < 0) 158 return rc; 159 160 stop = jiffies + chip->timeout_a; 161 162 if (chip->flags & TPM_CHIP_FLAG_IRQ) { 163 again: 164 timeout = stop - jiffies; 165 if ((long)timeout <= 0) 166 return -1; 167 rc = wait_event_interruptible_timeout(priv->int_queue, 168 (check_locality 169 (chip, l)), 170 timeout); 171 if (rc > 0) 172 return l; 173 if (rc == -ERESTARTSYS && freezing(current)) { 174 clear_thread_flag(TIF_SIGPENDING); 175 goto again; 176 } 177 } else { 178 /* wait for burstcount */ 179 do { 180 if (check_locality(chip, l)) 181 return l; 182 tpm_msleep(TPM_TIMEOUT); 183 } while (time_before(jiffies, stop)); 184 } 185 return -1; 186 } 187 188 static u8 tpm_tis_status(struct tpm_chip *chip) 189 { 190 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 191 int rc; 192 u8 status; 193 194 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status); 195 if (rc < 0) 196 return 0; 197 198 if (unlikely((status & TPM_STS_READ_ZERO) != 0)) { 199 /* 200 * If this trips, the chances are the read is 201 * returning 0xff because the locality hasn't been 202 * acquired. Usually because tpm_try_get_ops() hasn't 203 * been called before doing a TPM operation. 204 */ 205 WARN_ONCE(1, "TPM returned invalid status\n"); 206 return 0; 207 } 208 209 return status; 210 } 211 212 static void tpm_tis_ready(struct tpm_chip *chip) 213 { 214 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 215 216 /* this causes the current command to be aborted */ 217 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY); 218 } 219 220 static int get_burstcount(struct tpm_chip *chip) 221 { 222 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 223 unsigned long stop; 224 int burstcnt, rc; 225 u32 value; 226 227 /* wait for burstcount */ 228 if (chip->flags & TPM_CHIP_FLAG_TPM2) 229 stop = jiffies + chip->timeout_a; 230 else 231 stop = jiffies + chip->timeout_d; 232 do { 233 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value); 234 if (rc < 0) 235 return rc; 236 237 burstcnt = (value >> 8) & 0xFFFF; 238 if (burstcnt) 239 return burstcnt; 240 usleep_range(TPM_TIMEOUT_USECS_MIN, TPM_TIMEOUT_USECS_MAX); 241 } while (time_before(jiffies, stop)); 242 return -EBUSY; 243 } 244 245 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) 246 { 247 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 248 int size = 0, burstcnt, rc; 249 250 while (size < count) { 251 rc = wait_for_tpm_stat(chip, 252 TPM_STS_DATA_AVAIL | TPM_STS_VALID, 253 chip->timeout_c, 254 &priv->read_queue, true); 255 if (rc < 0) 256 return rc; 257 burstcnt = get_burstcount(chip); 258 if (burstcnt < 0) { 259 dev_err(&chip->dev, "Unable to read burstcount\n"); 260 return burstcnt; 261 } 262 burstcnt = min_t(int, burstcnt, count - size); 263 264 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality), 265 burstcnt, buf + size); 266 if (rc < 0) 267 return rc; 268 269 size += burstcnt; 270 } 271 return size; 272 } 273 274 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count) 275 { 276 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 277 int size = 0; 278 int status; 279 u32 expected; 280 281 if (count < TPM_HEADER_SIZE) { 282 size = -EIO; 283 goto out; 284 } 285 286 size = recv_data(chip, buf, TPM_HEADER_SIZE); 287 /* read first 10 bytes, including tag, paramsize, and result */ 288 if (size < TPM_HEADER_SIZE) { 289 dev_err(&chip->dev, "Unable to read header\n"); 290 goto out; 291 } 292 293 expected = be32_to_cpu(*(__be32 *) (buf + 2)); 294 if (expected > count || expected < TPM_HEADER_SIZE) { 295 size = -EIO; 296 goto out; 297 } 298 299 size += recv_data(chip, &buf[TPM_HEADER_SIZE], 300 expected - TPM_HEADER_SIZE); 301 if (size < expected) { 302 dev_err(&chip->dev, "Unable to read remainder of result\n"); 303 size = -ETIME; 304 goto out; 305 } 306 307 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c, 308 &priv->int_queue, false) < 0) { 309 size = -ETIME; 310 goto out; 311 } 312 status = tpm_tis_status(chip); 313 if (status & TPM_STS_DATA_AVAIL) { /* retry? */ 314 dev_err(&chip->dev, "Error left over data\n"); 315 size = -EIO; 316 goto out; 317 } 318 319 out: 320 tpm_tis_ready(chip); 321 return size; 322 } 323 324 /* 325 * If interrupts are used (signaled by an irq set in the vendor structure) 326 * tpm.c can skip polling for the data to be available as the interrupt is 327 * waited for here 328 */ 329 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len) 330 { 331 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 332 int rc, status, burstcnt; 333 size_t count = 0; 334 bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND; 335 336 status = tpm_tis_status(chip); 337 if ((status & TPM_STS_COMMAND_READY) == 0) { 338 tpm_tis_ready(chip); 339 if (wait_for_tpm_stat 340 (chip, TPM_STS_COMMAND_READY, chip->timeout_b, 341 &priv->int_queue, false) < 0) { 342 rc = -ETIME; 343 goto out_err; 344 } 345 } 346 347 while (count < len - 1) { 348 burstcnt = get_burstcount(chip); 349 if (burstcnt < 0) { 350 dev_err(&chip->dev, "Unable to read burstcount\n"); 351 rc = burstcnt; 352 goto out_err; 353 } 354 burstcnt = min_t(int, burstcnt, len - count - 1); 355 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality), 356 burstcnt, buf + count); 357 if (rc < 0) 358 goto out_err; 359 360 count += burstcnt; 361 362 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c, 363 &priv->int_queue, false) < 0) { 364 rc = -ETIME; 365 goto out_err; 366 } 367 status = tpm_tis_status(chip); 368 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) { 369 rc = -EIO; 370 goto out_err; 371 } 372 } 373 374 /* write last byte */ 375 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]); 376 if (rc < 0) 377 goto out_err; 378 379 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c, 380 &priv->int_queue, false) < 0) { 381 rc = -ETIME; 382 goto out_err; 383 } 384 status = tpm_tis_status(chip); 385 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) { 386 rc = -EIO; 387 goto out_err; 388 } 389 390 return 0; 391 392 out_err: 393 tpm_tis_ready(chip); 394 return rc; 395 } 396 397 static void disable_interrupts(struct tpm_chip *chip) 398 { 399 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 400 u32 intmask; 401 int rc; 402 403 if (priv->irq == 0) 404 return; 405 406 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask); 407 if (rc < 0) 408 intmask = 0; 409 410 intmask &= ~TPM_GLOBAL_INT_ENABLE; 411 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); 412 413 devm_free_irq(chip->dev.parent, priv->irq, chip); 414 priv->irq = 0; 415 chip->flags &= ~TPM_CHIP_FLAG_IRQ; 416 } 417 418 /* 419 * If interrupts are used (signaled by an irq set in the vendor structure) 420 * tpm.c can skip polling for the data to be available as the interrupt is 421 * waited for here 422 */ 423 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len) 424 { 425 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 426 int rc; 427 u32 ordinal; 428 unsigned long dur; 429 430 rc = tpm_tis_send_data(chip, buf, len); 431 if (rc < 0) 432 return rc; 433 434 /* go and do it */ 435 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO); 436 if (rc < 0) 437 goto out_err; 438 439 if (chip->flags & TPM_CHIP_FLAG_IRQ) { 440 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 441 442 dur = tpm_calc_ordinal_duration(chip, ordinal); 443 if (wait_for_tpm_stat 444 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur, 445 &priv->read_queue, false) < 0) { 446 rc = -ETIME; 447 goto out_err; 448 } 449 } 450 return 0; 451 out_err: 452 tpm_tis_ready(chip); 453 return rc; 454 } 455 456 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len) 457 { 458 int rc, irq; 459 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 460 461 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested) 462 return tpm_tis_send_main(chip, buf, len); 463 464 /* Verify receipt of the expected IRQ */ 465 irq = priv->irq; 466 priv->irq = 0; 467 chip->flags &= ~TPM_CHIP_FLAG_IRQ; 468 rc = tpm_tis_send_main(chip, buf, len); 469 priv->irq = irq; 470 chip->flags |= TPM_CHIP_FLAG_IRQ; 471 if (!priv->irq_tested) 472 tpm_msleep(1); 473 if (!priv->irq_tested) 474 disable_interrupts(chip); 475 priv->irq_tested = true; 476 return rc; 477 } 478 479 struct tis_vendor_durations_override { 480 u32 did_vid; 481 struct tpm1_version version; 482 unsigned long durations[3]; 483 }; 484 485 static const struct tis_vendor_durations_override vendor_dur_overrides[] = { 486 /* STMicroelectronics 0x104a */ 487 { 0x0000104a, 488 { 1, 2, 8, 28 }, 489 { (2 * 60 * HZ), (2 * 60 * HZ), (2 * 60 * HZ) } }, 490 }; 491 492 static void tpm_tis_update_durations(struct tpm_chip *chip, 493 unsigned long *duration_cap) 494 { 495 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 496 struct tpm1_version *version; 497 u32 did_vid; 498 int i, rc; 499 cap_t cap; 500 501 chip->duration_adjusted = false; 502 503 if (chip->ops->clk_enable != NULL) 504 chip->ops->clk_enable(chip, true); 505 506 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid); 507 if (rc < 0) { 508 dev_warn(&chip->dev, "%s: failed to read did_vid. %d\n", 509 __func__, rc); 510 goto out; 511 } 512 513 /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */ 514 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap, 515 "attempting to determine the 1.2 version", 516 sizeof(cap.version2)); 517 if (!rc) { 518 version = &cap.version2.version; 519 } else { 520 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap, 521 "attempting to determine the 1.1 version", 522 sizeof(cap.version1)); 523 524 if (rc) 525 goto out; 526 527 version = &cap.version1; 528 } 529 530 for (i = 0; i != ARRAY_SIZE(vendor_dur_overrides); i++) { 531 if (vendor_dur_overrides[i].did_vid != did_vid) 532 continue; 533 534 if ((version->major == 535 vendor_dur_overrides[i].version.major) && 536 (version->minor == 537 vendor_dur_overrides[i].version.minor) && 538 (version->rev_major == 539 vendor_dur_overrides[i].version.rev_major) && 540 (version->rev_minor == 541 vendor_dur_overrides[i].version.rev_minor)) { 542 543 memcpy(duration_cap, 544 vendor_dur_overrides[i].durations, 545 sizeof(vendor_dur_overrides[i].durations)); 546 547 chip->duration_adjusted = true; 548 goto out; 549 } 550 } 551 552 out: 553 if (chip->ops->clk_enable != NULL) 554 chip->ops->clk_enable(chip, false); 555 } 556 557 struct tis_vendor_timeout_override { 558 u32 did_vid; 559 unsigned long timeout_us[4]; 560 }; 561 562 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = { 563 /* Atmel 3204 */ 564 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000), 565 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } }, 566 }; 567 568 static void tpm_tis_update_timeouts(struct tpm_chip *chip, 569 unsigned long *timeout_cap) 570 { 571 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 572 int i, rc; 573 u32 did_vid; 574 575 chip->timeout_adjusted = false; 576 577 if (chip->ops->clk_enable != NULL) 578 chip->ops->clk_enable(chip, true); 579 580 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid); 581 if (rc < 0) { 582 dev_warn(&chip->dev, "%s: failed to read did_vid: %d\n", 583 __func__, rc); 584 goto out; 585 } 586 587 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) { 588 if (vendor_timeout_overrides[i].did_vid != did_vid) 589 continue; 590 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us, 591 sizeof(vendor_timeout_overrides[i].timeout_us)); 592 chip->timeout_adjusted = true; 593 } 594 595 out: 596 if (chip->ops->clk_enable != NULL) 597 chip->ops->clk_enable(chip, false); 598 599 return; 600 } 601 602 /* 603 * Early probing for iTPM with STS_DATA_EXPECT flaw. 604 * Try sending command without itpm flag set and if that 605 * fails, repeat with itpm flag set. 606 */ 607 static int probe_itpm(struct tpm_chip *chip) 608 { 609 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 610 int rc = 0; 611 static const u8 cmd_getticks[] = { 612 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, 613 0x00, 0x00, 0x00, 0xf1 614 }; 615 size_t len = sizeof(cmd_getticks); 616 u16 vendor; 617 618 if (priv->flags & TPM_TIS_ITPM_WORKAROUND) 619 return 0; 620 621 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor); 622 if (rc < 0) 623 return rc; 624 625 /* probe only iTPMS */ 626 if (vendor != TPM_VID_INTEL) 627 return 0; 628 629 if (request_locality(chip, 0) != 0) 630 return -EBUSY; 631 632 rc = tpm_tis_send_data(chip, cmd_getticks, len); 633 if (rc == 0) 634 goto out; 635 636 tpm_tis_ready(chip); 637 638 priv->flags |= TPM_TIS_ITPM_WORKAROUND; 639 640 rc = tpm_tis_send_data(chip, cmd_getticks, len); 641 if (rc == 0) 642 dev_info(&chip->dev, "Detected an iTPM.\n"); 643 else { 644 priv->flags &= ~TPM_TIS_ITPM_WORKAROUND; 645 rc = -EFAULT; 646 } 647 648 out: 649 tpm_tis_ready(chip); 650 release_locality(chip, priv->locality); 651 652 return rc; 653 } 654 655 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status) 656 { 657 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 658 659 switch (priv->manufacturer_id) { 660 case TPM_VID_WINBOND: 661 return ((status == TPM_STS_VALID) || 662 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY))); 663 case TPM_VID_STM: 664 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)); 665 default: 666 return (status == TPM_STS_COMMAND_READY); 667 } 668 } 669 670 static irqreturn_t tis_int_handler(int dummy, void *dev_id) 671 { 672 struct tpm_chip *chip = dev_id; 673 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 674 u32 interrupt; 675 int i, rc; 676 677 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt); 678 if (rc < 0) 679 return IRQ_NONE; 680 681 if (interrupt == 0) 682 return IRQ_NONE; 683 684 priv->irq_tested = true; 685 if (interrupt & TPM_INTF_DATA_AVAIL_INT) 686 wake_up_interruptible(&priv->read_queue); 687 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT) 688 for (i = 0; i < 5; i++) 689 if (check_locality(chip, i)) 690 break; 691 if (interrupt & 692 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT | 693 TPM_INTF_CMD_READY_INT)) 694 wake_up_interruptible(&priv->int_queue); 695 696 /* Clear interrupts handled with TPM_EOI */ 697 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt); 698 if (rc < 0) 699 return IRQ_NONE; 700 701 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt); 702 return IRQ_HANDLED; 703 } 704 705 static int tpm_tis_gen_interrupt(struct tpm_chip *chip) 706 { 707 const char *desc = "attempting to generate an interrupt"; 708 u32 cap2; 709 cap_t cap; 710 711 if (chip->flags & TPM_CHIP_FLAG_TPM2) 712 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc); 713 else 714 return tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 715 0); 716 } 717 718 /* Register the IRQ and issue a command that will cause an interrupt. If an 719 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse 720 * everything and leave in polling mode. Returns 0 on success. 721 */ 722 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, 723 int flags, int irq) 724 { 725 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 726 u8 original_int_vec; 727 int rc; 728 u32 int_status; 729 730 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags, 731 dev_name(&chip->dev), chip) != 0) { 732 dev_info(&chip->dev, "Unable to request irq: %d for probe\n", 733 irq); 734 return -1; 735 } 736 priv->irq = irq; 737 738 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality), 739 &original_int_vec); 740 if (rc < 0) 741 return rc; 742 743 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq); 744 if (rc < 0) 745 return rc; 746 747 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status); 748 if (rc < 0) 749 return rc; 750 751 /* Clear all existing */ 752 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status); 753 if (rc < 0) 754 return rc; 755 756 /* Turn on */ 757 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), 758 intmask | TPM_GLOBAL_INT_ENABLE); 759 if (rc < 0) 760 return rc; 761 762 priv->irq_tested = false; 763 764 /* Generate an interrupt by having the core call through to 765 * tpm_tis_send 766 */ 767 rc = tpm_tis_gen_interrupt(chip); 768 if (rc < 0) 769 return rc; 770 771 /* tpm_tis_send will either confirm the interrupt is working or it 772 * will call disable_irq which undoes all of the above. 773 */ 774 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) { 775 rc = tpm_tis_write8(priv, original_int_vec, 776 TPM_INT_VECTOR(priv->locality)); 777 if (rc < 0) 778 return rc; 779 780 return 1; 781 } 782 783 return 0; 784 } 785 786 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that 787 * do not have ACPI/etc. We typically expect the interrupt to be declared if 788 * present. 789 */ 790 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask) 791 { 792 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 793 u8 original_int_vec; 794 int i, rc; 795 796 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality), 797 &original_int_vec); 798 if (rc < 0) 799 return; 800 801 if (!original_int_vec) { 802 if (IS_ENABLED(CONFIG_X86)) 803 for (i = 3; i <= 15; i++) 804 if (!tpm_tis_probe_irq_single(chip, intmask, 0, 805 i)) 806 return; 807 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0, 808 original_int_vec)) 809 return; 810 } 811 812 void tpm_tis_remove(struct tpm_chip *chip) 813 { 814 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 815 u32 reg = TPM_INT_ENABLE(priv->locality); 816 u32 interrupt; 817 int rc; 818 819 tpm_tis_clkrun_enable(chip, true); 820 821 rc = tpm_tis_read32(priv, reg, &interrupt); 822 if (rc < 0) 823 interrupt = 0; 824 825 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt); 826 827 tpm_tis_clkrun_enable(chip, false); 828 829 if (priv->ilb_base_addr) 830 iounmap(priv->ilb_base_addr); 831 } 832 EXPORT_SYMBOL_GPL(tpm_tis_remove); 833 834 /** 835 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration 836 * of a single TPM command 837 * @chip: TPM chip to use 838 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running 839 * 0 - Enable CLKRUN protocol 840 * Call this function directly in tpm_tis_remove() in error or driver removal 841 * path, since the chip->ops is set to NULL in tpm_chip_unregister(). 842 */ 843 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value) 844 { 845 struct tpm_tis_data *data = dev_get_drvdata(&chip->dev); 846 u32 clkrun_val; 847 848 if (!IS_ENABLED(CONFIG_X86) || !is_bsw() || 849 !data->ilb_base_addr) 850 return; 851 852 if (value) { 853 data->clkrun_enabled++; 854 if (data->clkrun_enabled > 1) 855 return; 856 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET); 857 858 /* Disable LPC CLKRUN# */ 859 clkrun_val &= ~LPC_CLKRUN_EN; 860 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET); 861 862 /* 863 * Write any random value on port 0x80 which is on LPC, to make 864 * sure LPC clock is running before sending any TPM command. 865 */ 866 outb(0xCC, 0x80); 867 } else { 868 data->clkrun_enabled--; 869 if (data->clkrun_enabled) 870 return; 871 872 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET); 873 874 /* Enable LPC CLKRUN# */ 875 clkrun_val |= LPC_CLKRUN_EN; 876 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET); 877 878 /* 879 * Write any random value on port 0x80 which is on LPC, to make 880 * sure LPC clock is running before sending any TPM command. 881 */ 882 outb(0xCC, 0x80); 883 } 884 } 885 886 static const struct tpm_class_ops tpm_tis = { 887 .flags = TPM_OPS_AUTO_STARTUP, 888 .status = tpm_tis_status, 889 .recv = tpm_tis_recv, 890 .send = tpm_tis_send, 891 .cancel = tpm_tis_ready, 892 .update_timeouts = tpm_tis_update_timeouts, 893 .update_durations = tpm_tis_update_durations, 894 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 895 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 896 .req_canceled = tpm_tis_req_canceled, 897 .request_locality = request_locality, 898 .relinquish_locality = release_locality, 899 .clk_enable = tpm_tis_clkrun_enable, 900 }; 901 902 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, 903 const struct tpm_tis_phy_ops *phy_ops, 904 acpi_handle acpi_dev_handle) 905 { 906 u32 vendor; 907 u32 intfcaps; 908 u32 intmask; 909 u32 clkrun_val; 910 u8 rid; 911 int rc, probe; 912 struct tpm_chip *chip; 913 914 chip = tpmm_chip_alloc(dev, &tpm_tis); 915 if (IS_ERR(chip)) 916 return PTR_ERR(chip); 917 918 #ifdef CONFIG_ACPI 919 chip->acpi_dev_handle = acpi_dev_handle; 920 #endif 921 922 chip->hwrng.quality = priv->rng_quality; 923 924 /* Maximum timeouts */ 925 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX); 926 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX); 927 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX); 928 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX); 929 priv->phy_ops = phy_ops; 930 dev_set_drvdata(&chip->dev, priv); 931 932 if (is_bsw()) { 933 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR, 934 ILB_REMAP_SIZE); 935 if (!priv->ilb_base_addr) 936 return -ENOMEM; 937 938 clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET); 939 /* Check if CLKRUN# is already not enabled in the LPC bus */ 940 if (!(clkrun_val & LPC_CLKRUN_EN)) { 941 iounmap(priv->ilb_base_addr); 942 priv->ilb_base_addr = NULL; 943 } 944 } 945 946 if (chip->ops->clk_enable != NULL) 947 chip->ops->clk_enable(chip, true); 948 949 if (wait_startup(chip, 0) != 0) { 950 rc = -ENODEV; 951 goto out_err; 952 } 953 954 /* Take control of the TPM's interrupt hardware and shut it off */ 955 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask); 956 if (rc < 0) 957 goto out_err; 958 959 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT | 960 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT; 961 intmask &= ~TPM_GLOBAL_INT_ENABLE; 962 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); 963 964 rc = tpm_chip_start(chip); 965 if (rc) 966 goto out_err; 967 rc = tpm2_probe(chip); 968 tpm_chip_stop(chip); 969 if (rc) 970 goto out_err; 971 972 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor); 973 if (rc < 0) 974 goto out_err; 975 976 priv->manufacturer_id = vendor; 977 978 rc = tpm_tis_read8(priv, TPM_RID(0), &rid); 979 if (rc < 0) 980 goto out_err; 981 982 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n", 983 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2", 984 vendor >> 16, rid); 985 986 probe = probe_itpm(chip); 987 if (probe < 0) { 988 rc = -ENODEV; 989 goto out_err; 990 } 991 992 /* Figure out the capabilities */ 993 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps); 994 if (rc < 0) 995 goto out_err; 996 997 dev_dbg(dev, "TPM interface capabilities (0x%x):\n", 998 intfcaps); 999 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC) 1000 dev_dbg(dev, "\tBurst Count Static\n"); 1001 if (intfcaps & TPM_INTF_CMD_READY_INT) 1002 dev_dbg(dev, "\tCommand Ready Int Support\n"); 1003 if (intfcaps & TPM_INTF_INT_EDGE_FALLING) 1004 dev_dbg(dev, "\tInterrupt Edge Falling\n"); 1005 if (intfcaps & TPM_INTF_INT_EDGE_RISING) 1006 dev_dbg(dev, "\tInterrupt Edge Rising\n"); 1007 if (intfcaps & TPM_INTF_INT_LEVEL_LOW) 1008 dev_dbg(dev, "\tInterrupt Level Low\n"); 1009 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH) 1010 dev_dbg(dev, "\tInterrupt Level High\n"); 1011 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT) 1012 dev_dbg(dev, "\tLocality Change Int Support\n"); 1013 if (intfcaps & TPM_INTF_STS_VALID_INT) 1014 dev_dbg(dev, "\tSts Valid Int Support\n"); 1015 if (intfcaps & TPM_INTF_DATA_AVAIL_INT) 1016 dev_dbg(dev, "\tData Avail Int Support\n"); 1017 1018 /* INTERRUPT Setup */ 1019 init_waitqueue_head(&priv->read_queue); 1020 init_waitqueue_head(&priv->int_queue); 1021 if (irq != -1) { 1022 /* Before doing irq testing issue a command to the TPM in polling mode 1023 * to make sure it works. May as well use that command to set the 1024 * proper timeouts for the driver. 1025 */ 1026 if (tpm_get_timeouts(chip)) { 1027 dev_err(dev, "Could not get TPM timeouts and durations\n"); 1028 rc = -ENODEV; 1029 goto out_err; 1030 } 1031 1032 if (irq) { 1033 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED, 1034 irq); 1035 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) { 1036 dev_err(&chip->dev, FW_BUG 1037 "TPM interrupt not working, polling instead\n"); 1038 1039 disable_interrupts(chip); 1040 } 1041 } else { 1042 tpm_tis_probe_irq(chip, intmask); 1043 } 1044 } 1045 1046 rc = tpm_chip_register(chip); 1047 if (rc) 1048 goto out_err; 1049 1050 if (chip->ops->clk_enable != NULL) 1051 chip->ops->clk_enable(chip, false); 1052 1053 return 0; 1054 out_err: 1055 if (chip->ops->clk_enable != NULL) 1056 chip->ops->clk_enable(chip, false); 1057 1058 tpm_tis_remove(chip); 1059 1060 return rc; 1061 } 1062 EXPORT_SYMBOL_GPL(tpm_tis_core_init); 1063 1064 #ifdef CONFIG_PM_SLEEP 1065 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip) 1066 { 1067 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 1068 u32 intmask; 1069 int rc; 1070 1071 if (chip->ops->clk_enable != NULL) 1072 chip->ops->clk_enable(chip, true); 1073 1074 /* reenable interrupts that device may have lost or 1075 * BIOS/firmware may have disabled 1076 */ 1077 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq); 1078 if (rc < 0) 1079 goto out; 1080 1081 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask); 1082 if (rc < 0) 1083 goto out; 1084 1085 intmask |= TPM_INTF_CMD_READY_INT 1086 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT 1087 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE; 1088 1089 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); 1090 1091 out: 1092 if (chip->ops->clk_enable != NULL) 1093 chip->ops->clk_enable(chip, false); 1094 1095 return; 1096 } 1097 1098 int tpm_tis_resume(struct device *dev) 1099 { 1100 struct tpm_chip *chip = dev_get_drvdata(dev); 1101 int ret; 1102 1103 if (chip->flags & TPM_CHIP_FLAG_IRQ) 1104 tpm_tis_reenable_interrupts(chip); 1105 1106 ret = tpm_pm_resume(dev); 1107 if (ret) 1108 return ret; 1109 1110 /* TPM 1.2 requires self-test on resume. This function actually returns 1111 * an error code but for unknown reason it isn't handled. 1112 */ 1113 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) 1114 tpm1_do_selftest(chip); 1115 1116 return 0; 1117 } 1118 EXPORT_SYMBOL_GPL(tpm_tis_resume); 1119 #endif 1120 1121 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 1122 MODULE_DESCRIPTION("TPM Driver"); 1123 MODULE_VERSION("2.0"); 1124 MODULE_LICENSE("GPL"); 1125