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