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 /* Before we attempt to access the TPM we must see that the valid bit is set. 35 * The specification says that this bit is 0 at reset and remains 0 until the 36 * 'TPM has gone through its self test and initialization and has established 37 * correct values in the other bits.' 38 */ 39 static int wait_startup(struct tpm_chip *chip, int l) 40 { 41 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 42 unsigned long stop = jiffies + chip->timeout_a; 43 44 do { 45 int rc; 46 u8 access; 47 48 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access); 49 if (rc < 0) 50 return rc; 51 52 if (access & TPM_ACCESS_VALID) 53 return 0; 54 msleep(TPM_TIMEOUT); 55 } while (time_before(jiffies, stop)); 56 return -1; 57 } 58 59 static int check_locality(struct tpm_chip *chip, int l) 60 { 61 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 62 int rc; 63 u8 access; 64 65 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access); 66 if (rc < 0) 67 return rc; 68 69 if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == 70 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) 71 return priv->locality = l; 72 73 return -1; 74 } 75 76 static void release_locality(struct tpm_chip *chip, int l, int force) 77 { 78 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 79 int rc; 80 u8 access; 81 82 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access); 83 if (rc < 0) 84 return; 85 86 if (force || (access & 87 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == 88 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) 89 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY); 90 91 } 92 93 static int request_locality(struct tpm_chip *chip, int l) 94 { 95 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 96 unsigned long stop, timeout; 97 long rc; 98 99 if (check_locality(chip, l) >= 0) 100 return l; 101 102 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE); 103 if (rc < 0) 104 return rc; 105 106 stop = jiffies + chip->timeout_a; 107 108 if (chip->flags & TPM_CHIP_FLAG_IRQ) { 109 again: 110 timeout = stop - jiffies; 111 if ((long)timeout <= 0) 112 return -1; 113 rc = wait_event_interruptible_timeout(priv->int_queue, 114 (check_locality 115 (chip, l) >= 0), 116 timeout); 117 if (rc > 0) 118 return l; 119 if (rc == -ERESTARTSYS && freezing(current)) { 120 clear_thread_flag(TIF_SIGPENDING); 121 goto again; 122 } 123 } else { 124 /* wait for burstcount */ 125 do { 126 if (check_locality(chip, l) >= 0) 127 return l; 128 msleep(TPM_TIMEOUT); 129 } while (time_before(jiffies, stop)); 130 } 131 return -1; 132 } 133 134 static u8 tpm_tis_status(struct tpm_chip *chip) 135 { 136 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 137 int rc; 138 u8 status; 139 140 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status); 141 if (rc < 0) 142 return 0; 143 144 return status; 145 } 146 147 static void tpm_tis_ready(struct tpm_chip *chip) 148 { 149 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 150 151 /* this causes the current command to be aborted */ 152 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY); 153 } 154 155 static int get_burstcount(struct tpm_chip *chip) 156 { 157 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 158 unsigned long stop; 159 int burstcnt, rc; 160 u8 value; 161 162 /* wait for burstcount */ 163 /* which timeout value, spec has 2 answers (c & d) */ 164 stop = jiffies + chip->timeout_d; 165 do { 166 rc = tpm_tis_read8(priv, TPM_STS(priv->locality) + 1, &value); 167 if (rc < 0) 168 return rc; 169 170 burstcnt = value; 171 rc = tpm_tis_read8(priv, TPM_STS(priv->locality) + 2, &value); 172 if (rc < 0) 173 return rc; 174 175 burstcnt += value << 8; 176 if (burstcnt) 177 return burstcnt; 178 msleep(TPM_TIMEOUT); 179 } while (time_before(jiffies, stop)); 180 return -EBUSY; 181 } 182 183 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) 184 { 185 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 186 int size = 0, burstcnt, rc; 187 188 while (size < count && 189 wait_for_tpm_stat(chip, 190 TPM_STS_DATA_AVAIL | TPM_STS_VALID, 191 chip->timeout_c, 192 &priv->read_queue, true) == 0) { 193 burstcnt = min_t(int, get_burstcount(chip), count - size); 194 195 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality), 196 burstcnt, buf + size); 197 if (rc < 0) 198 return rc; 199 200 size += burstcnt; 201 } 202 return size; 203 } 204 205 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count) 206 { 207 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 208 int size = 0; 209 int expected, status; 210 211 if (count < TPM_HEADER_SIZE) { 212 size = -EIO; 213 goto out; 214 } 215 216 size = recv_data(chip, buf, TPM_HEADER_SIZE); 217 /* read first 10 bytes, including tag, paramsize, and result */ 218 if (size < TPM_HEADER_SIZE) { 219 dev_err(&chip->dev, "Unable to read header\n"); 220 goto out; 221 } 222 223 expected = be32_to_cpu(*(__be32 *) (buf + 2)); 224 if (expected > count) { 225 size = -EIO; 226 goto out; 227 } 228 229 size += recv_data(chip, &buf[TPM_HEADER_SIZE], 230 expected - TPM_HEADER_SIZE); 231 if (size < expected) { 232 dev_err(&chip->dev, "Unable to read remainder of result\n"); 233 size = -ETIME; 234 goto out; 235 } 236 237 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c, 238 &priv->int_queue, false); 239 status = tpm_tis_status(chip); 240 if (status & TPM_STS_DATA_AVAIL) { /* retry? */ 241 dev_err(&chip->dev, "Error left over data\n"); 242 size = -EIO; 243 goto out; 244 } 245 246 out: 247 tpm_tis_ready(chip); 248 release_locality(chip, priv->locality, 0); 249 return size; 250 } 251 252 /* 253 * If interrupts are used (signaled by an irq set in the vendor structure) 254 * tpm.c can skip polling for the data to be available as the interrupt is 255 * waited for here 256 */ 257 static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len) 258 { 259 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 260 int rc, status, burstcnt; 261 size_t count = 0; 262 bool itpm = priv->flags & TPM_TIS_ITPM_POSSIBLE; 263 264 if (request_locality(chip, 0) < 0) 265 return -EBUSY; 266 267 status = tpm_tis_status(chip); 268 if ((status & TPM_STS_COMMAND_READY) == 0) { 269 tpm_tis_ready(chip); 270 if (wait_for_tpm_stat 271 (chip, TPM_STS_COMMAND_READY, chip->timeout_b, 272 &priv->int_queue, false) < 0) { 273 rc = -ETIME; 274 goto out_err; 275 } 276 } 277 278 while (count < len - 1) { 279 burstcnt = min_t(int, get_burstcount(chip), len - count - 1); 280 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality), 281 burstcnt, buf + count); 282 if (rc < 0) 283 goto out_err; 284 285 count += burstcnt; 286 287 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c, 288 &priv->int_queue, false); 289 status = tpm_tis_status(chip); 290 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) { 291 rc = -EIO; 292 goto out_err; 293 } 294 } 295 296 /* write last byte */ 297 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]); 298 if (rc < 0) 299 goto out_err; 300 301 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c, 302 &priv->int_queue, false); 303 status = tpm_tis_status(chip); 304 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) { 305 rc = -EIO; 306 goto out_err; 307 } 308 309 return 0; 310 311 out_err: 312 tpm_tis_ready(chip); 313 release_locality(chip, priv->locality, 0); 314 return rc; 315 } 316 317 static void disable_interrupts(struct tpm_chip *chip) 318 { 319 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 320 u32 intmask; 321 int rc; 322 323 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask); 324 if (rc < 0) 325 intmask = 0; 326 327 intmask &= ~TPM_GLOBAL_INT_ENABLE; 328 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); 329 330 devm_free_irq(chip->dev.parent, priv->irq, chip); 331 priv->irq = 0; 332 chip->flags &= ~TPM_CHIP_FLAG_IRQ; 333 } 334 335 /* 336 * If interrupts are used (signaled by an irq set in the vendor structure) 337 * tpm.c can skip polling for the data to be available as the interrupt is 338 * waited for here 339 */ 340 static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len) 341 { 342 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 343 int rc; 344 u32 ordinal; 345 unsigned long dur; 346 347 rc = tpm_tis_send_data(chip, buf, len); 348 if (rc < 0) 349 return rc; 350 351 /* go and do it */ 352 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO); 353 if (rc < 0) 354 goto out_err; 355 356 if (chip->flags & TPM_CHIP_FLAG_IRQ) { 357 ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); 358 359 if (chip->flags & TPM_CHIP_FLAG_TPM2) 360 dur = tpm2_calc_ordinal_duration(chip, ordinal); 361 else 362 dur = tpm_calc_ordinal_duration(chip, ordinal); 363 364 if (wait_for_tpm_stat 365 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur, 366 &priv->read_queue, false) < 0) { 367 rc = -ETIME; 368 goto out_err; 369 } 370 } 371 return len; 372 out_err: 373 tpm_tis_ready(chip); 374 release_locality(chip, priv->locality, 0); 375 return rc; 376 } 377 378 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len) 379 { 380 int rc, irq; 381 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 382 383 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested) 384 return tpm_tis_send_main(chip, buf, len); 385 386 /* Verify receipt of the expected IRQ */ 387 irq = priv->irq; 388 priv->irq = 0; 389 chip->flags &= ~TPM_CHIP_FLAG_IRQ; 390 rc = tpm_tis_send_main(chip, buf, len); 391 priv->irq = irq; 392 chip->flags |= TPM_CHIP_FLAG_IRQ; 393 if (!priv->irq_tested) 394 msleep(1); 395 if (!priv->irq_tested) 396 disable_interrupts(chip); 397 priv->irq_tested = true; 398 return rc; 399 } 400 401 struct tis_vendor_timeout_override { 402 u32 did_vid; 403 unsigned long timeout_us[4]; 404 }; 405 406 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = { 407 /* Atmel 3204 */ 408 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000), 409 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } }, 410 }; 411 412 static bool tpm_tis_update_timeouts(struct tpm_chip *chip, 413 unsigned long *timeout_cap) 414 { 415 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 416 int i, rc; 417 u32 did_vid; 418 419 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid); 420 if (rc < 0) 421 return rc; 422 423 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) { 424 if (vendor_timeout_overrides[i].did_vid != did_vid) 425 continue; 426 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us, 427 sizeof(vendor_timeout_overrides[i].timeout_us)); 428 return true; 429 } 430 431 return false; 432 } 433 434 /* 435 * Early probing for iTPM with STS_DATA_EXPECT flaw. 436 * Try sending command without itpm flag set and if that 437 * fails, repeat with itpm flag set. 438 */ 439 static int probe_itpm(struct tpm_chip *chip) 440 { 441 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 442 int rc = 0; 443 u8 cmd_getticks[] = { 444 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a, 445 0x00, 0x00, 0x00, 0xf1 446 }; 447 size_t len = sizeof(cmd_getticks); 448 bool itpm; 449 u16 vendor; 450 451 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor); 452 if (rc < 0) 453 return rc; 454 455 /* probe only iTPMS */ 456 if (vendor != TPM_VID_INTEL) 457 return 0; 458 459 itpm = false; 460 461 rc = tpm_tis_send_data(chip, cmd_getticks, len); 462 if (rc == 0) 463 goto out; 464 465 tpm_tis_ready(chip); 466 release_locality(chip, priv->locality, 0); 467 468 itpm = true; 469 470 rc = tpm_tis_send_data(chip, cmd_getticks, len); 471 if (rc == 0) { 472 dev_info(&chip->dev, "Detected an iTPM.\n"); 473 rc = 1; 474 } else 475 rc = -EFAULT; 476 477 out: 478 tpm_tis_ready(chip); 479 release_locality(chip, priv->locality, 0); 480 481 return rc; 482 } 483 484 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status) 485 { 486 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 487 488 switch (priv->manufacturer_id) { 489 case TPM_VID_WINBOND: 490 return ((status == TPM_STS_VALID) || 491 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY))); 492 case TPM_VID_STM: 493 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)); 494 default: 495 return (status == TPM_STS_COMMAND_READY); 496 } 497 } 498 499 static irqreturn_t tis_int_handler(int dummy, void *dev_id) 500 { 501 struct tpm_chip *chip = dev_id; 502 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 503 u32 interrupt; 504 int i, rc; 505 506 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt); 507 if (rc < 0) 508 return IRQ_NONE; 509 510 if (interrupt == 0) 511 return IRQ_NONE; 512 513 priv->irq_tested = true; 514 if (interrupt & TPM_INTF_DATA_AVAIL_INT) 515 wake_up_interruptible(&priv->read_queue); 516 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT) 517 for (i = 0; i < 5; i++) 518 if (check_locality(chip, i) >= 0) 519 break; 520 if (interrupt & 521 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT | 522 TPM_INTF_CMD_READY_INT)) 523 wake_up_interruptible(&priv->int_queue); 524 525 /* Clear interrupts handled with TPM_EOI */ 526 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt); 527 if (rc < 0) 528 return IRQ_NONE; 529 530 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt); 531 return IRQ_HANDLED; 532 } 533 534 /* Register the IRQ and issue a command that will cause an interrupt. If an 535 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse 536 * everything and leave in polling mode. Returns 0 on success. 537 */ 538 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask, 539 int flags, int irq) 540 { 541 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 542 u8 original_int_vec; 543 int rc; 544 u32 int_status; 545 546 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags, 547 dev_name(&chip->dev), chip) != 0) { 548 dev_info(&chip->dev, "Unable to request irq: %d for probe\n", 549 irq); 550 return -1; 551 } 552 priv->irq = irq; 553 554 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality), 555 &original_int_vec); 556 if (rc < 0) 557 return rc; 558 559 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq); 560 if (rc < 0) 561 return rc; 562 563 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status); 564 if (rc < 0) 565 return rc; 566 567 /* Clear all existing */ 568 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status); 569 if (rc < 0) 570 return rc; 571 572 /* Turn on */ 573 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), 574 intmask | TPM_GLOBAL_INT_ENABLE); 575 if (rc < 0) 576 return rc; 577 578 priv->irq_tested = false; 579 580 /* Generate an interrupt by having the core call through to 581 * tpm_tis_send 582 */ 583 if (chip->flags & TPM_CHIP_FLAG_TPM2) 584 tpm2_gen_interrupt(chip); 585 else 586 tpm_gen_interrupt(chip); 587 588 /* tpm_tis_send will either confirm the interrupt is working or it 589 * will call disable_irq which undoes all of the above. 590 */ 591 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) { 592 rc = tpm_tis_write8(priv, original_int_vec, 593 TPM_INT_VECTOR(priv->locality)); 594 if (rc < 0) 595 return rc; 596 597 return 1; 598 } 599 600 return 0; 601 } 602 603 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that 604 * do not have ACPI/etc. We typically expect the interrupt to be declared if 605 * present. 606 */ 607 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask) 608 { 609 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 610 u8 original_int_vec; 611 int i, rc; 612 613 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality), 614 &original_int_vec); 615 if (rc < 0) 616 return; 617 618 if (!original_int_vec) { 619 if (IS_ENABLED(CONFIG_X86)) 620 for (i = 3; i <= 15; i++) 621 if (!tpm_tis_probe_irq_single(chip, intmask, 0, 622 i)) 623 return; 624 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0, 625 original_int_vec)) 626 return; 627 } 628 629 void tpm_tis_remove(struct tpm_chip *chip) 630 { 631 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 632 u32 reg = TPM_INT_ENABLE(priv->locality); 633 u32 interrupt; 634 int rc; 635 636 rc = tpm_tis_read32(priv, reg, &interrupt); 637 if (rc < 0) 638 interrupt = 0; 639 640 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt); 641 release_locality(chip, priv->locality, 1); 642 } 643 EXPORT_SYMBOL_GPL(tpm_tis_remove); 644 645 static const struct tpm_class_ops tpm_tis = { 646 .status = tpm_tis_status, 647 .recv = tpm_tis_recv, 648 .send = tpm_tis_send, 649 .cancel = tpm_tis_ready, 650 .update_timeouts = tpm_tis_update_timeouts, 651 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 652 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, 653 .req_canceled = tpm_tis_req_canceled, 654 }; 655 656 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq, 657 const struct tpm_tis_phy_ops *phy_ops, 658 acpi_handle acpi_dev_handle) 659 { 660 u32 vendor, intfcaps, intmask; 661 u8 rid; 662 int rc, probe; 663 struct tpm_chip *chip; 664 665 chip = tpmm_chip_alloc(dev, &tpm_tis); 666 if (IS_ERR(chip)) 667 return PTR_ERR(chip); 668 669 #ifdef CONFIG_ACPI 670 chip->acpi_dev_handle = acpi_dev_handle; 671 #endif 672 673 /* Maximum timeouts */ 674 chip->timeout_a = TIS_TIMEOUT_A_MAX; 675 chip->timeout_b = TIS_TIMEOUT_B_MAX; 676 chip->timeout_c = TIS_TIMEOUT_C_MAX; 677 chip->timeout_d = TIS_TIMEOUT_D_MAX; 678 priv->phy_ops = phy_ops; 679 dev_set_drvdata(&chip->dev, priv); 680 681 if (wait_startup(chip, 0) != 0) { 682 rc = -ENODEV; 683 goto out_err; 684 } 685 686 /* Take control of the TPM's interrupt hardware and shut it off */ 687 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask); 688 if (rc < 0) 689 goto out_err; 690 691 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT | 692 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT; 693 intmask &= ~TPM_GLOBAL_INT_ENABLE; 694 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); 695 696 if (request_locality(chip, 0) != 0) { 697 rc = -ENODEV; 698 goto out_err; 699 } 700 701 rc = tpm2_probe(chip); 702 if (rc) 703 goto out_err; 704 705 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor); 706 if (rc < 0) 707 goto out_err; 708 709 priv->manufacturer_id = vendor; 710 711 rc = tpm_tis_read8(priv, TPM_RID(0), &rid); 712 if (rc < 0) 713 goto out_err; 714 715 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n", 716 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2", 717 vendor >> 16, rid); 718 719 if (!(priv->flags & TPM_TIS_ITPM_POSSIBLE)) { 720 probe = probe_itpm(chip); 721 if (probe < 0) { 722 rc = -ENODEV; 723 goto out_err; 724 } 725 726 if (!!probe) 727 priv->flags |= TPM_TIS_ITPM_POSSIBLE; 728 } 729 730 /* Figure out the capabilities */ 731 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps); 732 if (rc < 0) 733 goto out_err; 734 735 dev_dbg(dev, "TPM interface capabilities (0x%x):\n", 736 intfcaps); 737 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC) 738 dev_dbg(dev, "\tBurst Count Static\n"); 739 if (intfcaps & TPM_INTF_CMD_READY_INT) 740 dev_dbg(dev, "\tCommand Ready Int Support\n"); 741 if (intfcaps & TPM_INTF_INT_EDGE_FALLING) 742 dev_dbg(dev, "\tInterrupt Edge Falling\n"); 743 if (intfcaps & TPM_INTF_INT_EDGE_RISING) 744 dev_dbg(dev, "\tInterrupt Edge Rising\n"); 745 if (intfcaps & TPM_INTF_INT_LEVEL_LOW) 746 dev_dbg(dev, "\tInterrupt Level Low\n"); 747 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH) 748 dev_dbg(dev, "\tInterrupt Level High\n"); 749 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT) 750 dev_dbg(dev, "\tLocality Change Int Support\n"); 751 if (intfcaps & TPM_INTF_STS_VALID_INT) 752 dev_dbg(dev, "\tSts Valid Int Support\n"); 753 if (intfcaps & TPM_INTF_DATA_AVAIL_INT) 754 dev_dbg(dev, "\tData Avail Int Support\n"); 755 756 /* Very early on issue a command to the TPM in polling mode to make 757 * sure it works. May as well use that command to set the proper 758 * timeouts for the driver. 759 */ 760 if (tpm_get_timeouts(chip)) { 761 dev_err(dev, "Could not get TPM timeouts and durations\n"); 762 rc = -ENODEV; 763 goto out_err; 764 } 765 766 /* INTERRUPT Setup */ 767 init_waitqueue_head(&priv->read_queue); 768 init_waitqueue_head(&priv->int_queue); 769 if (irq != -1) { 770 if (irq) { 771 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED, 772 irq); 773 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) 774 dev_err(&chip->dev, FW_BUG 775 "TPM interrupt not working, polling instead\n"); 776 } else { 777 tpm_tis_probe_irq(chip, intmask); 778 } 779 } 780 781 if (chip->flags & TPM_CHIP_FLAG_TPM2) { 782 rc = tpm2_do_selftest(chip); 783 if (rc == TPM2_RC_INITIALIZE) { 784 dev_warn(dev, "Firmware has not started TPM\n"); 785 rc = tpm2_startup(chip, TPM2_SU_CLEAR); 786 if (!rc) 787 rc = tpm2_do_selftest(chip); 788 } 789 790 if (rc) { 791 dev_err(dev, "TPM self test failed\n"); 792 if (rc > 0) 793 rc = -ENODEV; 794 goto out_err; 795 } 796 } else { 797 if (tpm_do_selftest(chip)) { 798 dev_err(dev, "TPM self test failed\n"); 799 rc = -ENODEV; 800 goto out_err; 801 } 802 } 803 804 return tpm_chip_register(chip); 805 out_err: 806 tpm_tis_remove(chip); 807 return rc; 808 } 809 EXPORT_SYMBOL_GPL(tpm_tis_core_init); 810 811 #ifdef CONFIG_PM_SLEEP 812 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip) 813 { 814 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev); 815 u32 intmask; 816 int rc; 817 818 /* reenable interrupts that device may have lost or 819 * BIOS/firmware may have disabled 820 */ 821 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq); 822 if (rc < 0) 823 return; 824 825 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask); 826 if (rc < 0) 827 return; 828 829 intmask |= TPM_INTF_CMD_READY_INT 830 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT 831 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE; 832 833 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); 834 } 835 836 int tpm_tis_resume(struct device *dev) 837 { 838 struct tpm_chip *chip = dev_get_drvdata(dev); 839 int ret; 840 841 if (chip->flags & TPM_CHIP_FLAG_IRQ) 842 tpm_tis_reenable_interrupts(chip); 843 844 ret = tpm_pm_resume(dev); 845 if (ret) 846 return ret; 847 848 /* TPM 1.2 requires self-test on resume. This function actually returns 849 * an error code but for unknown reason it isn't handled. 850 */ 851 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) 852 tpm_do_selftest(chip); 853 854 return 0; 855 } 856 EXPORT_SYMBOL_GPL(tpm_tis_resume); 857 #endif 858 859 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 860 MODULE_DESCRIPTION("TPM Driver"); 861 MODULE_VERSION("2.0"); 862 MODULE_LICENSE("GPL"); 863