2 * Copyright (C) 2005, 2006 IBM Corporation
3 * Copyright (C) 2014, 2015 Intel Corporation
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11 * Device driver for TCG/TCPA TPM (trusted platform module).
12 * Specifications at www.trustedcomputinggroup.org
14 * This device driver implements the TPM interface as defined in
15 * the TCG TPM Interface Spec version 1.2, revision 1.0.
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
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>
32 #include "tpm_tis_core.h"
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.'
39 static int wait_startup(struct tpm_chip *chip, int l)
41 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
42 unsigned long stop = jiffies + chip->timeout_a;
48 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
52 if (access & TPM_ACCESS_VALID)
54 tpm_msleep(TPM_TIMEOUT);
55 } while (time_before(jiffies, stop));
59 static bool check_locality(struct tpm_chip *chip, int l)
61 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
65 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
69 if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
70 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
78 static void release_locality(struct tpm_chip *chip, int l)
80 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
82 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
85 static int request_locality(struct tpm_chip *chip, int l)
87 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
88 unsigned long stop, timeout;
91 if (check_locality(chip, l))
94 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
98 stop = jiffies + chip->timeout_a;
100 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
102 timeout = stop - jiffies;
103 if ((long)timeout <= 0)
105 rc = wait_event_interruptible_timeout(priv->int_queue,
111 if (rc == -ERESTARTSYS && freezing(current)) {
112 clear_thread_flag(TIF_SIGPENDING);
116 /* wait for burstcount */
118 if (check_locality(chip, l))
120 tpm_msleep(TPM_TIMEOUT);
121 } while (time_before(jiffies, stop));
126 static u8 tpm_tis_status(struct tpm_chip *chip)
128 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
132 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
139 static void tpm_tis_ready(struct tpm_chip *chip)
141 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
143 /* this causes the current command to be aborted */
144 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
147 static int get_burstcount(struct tpm_chip *chip)
149 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
154 /* wait for burstcount */
155 if (chip->flags & TPM_CHIP_FLAG_TPM2)
156 stop = jiffies + chip->timeout_a;
158 stop = jiffies + chip->timeout_d;
160 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
164 burstcnt = (value >> 8) & 0xFFFF;
167 tpm_msleep(TPM_TIMEOUT);
168 } while (time_before(jiffies, stop));
172 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
174 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
175 int size = 0, burstcnt, rc;
177 while (size < count) {
178 rc = wait_for_tpm_stat(chip,
179 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
181 &priv->read_queue, true);
184 burstcnt = get_burstcount(chip);
186 dev_err(&chip->dev, "Unable to read burstcount\n");
189 burstcnt = min_t(int, burstcnt, count - size);
191 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
192 burstcnt, buf + size);
201 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
203 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
205 int expected, status;
207 if (count < TPM_HEADER_SIZE) {
212 size = recv_data(chip, buf, TPM_HEADER_SIZE);
213 /* read first 10 bytes, including tag, paramsize, and result */
214 if (size < TPM_HEADER_SIZE) {
215 dev_err(&chip->dev, "Unable to read header\n");
219 expected = be32_to_cpu(*(__be32 *) (buf + 2));
220 if (expected > count) {
225 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
226 expected - TPM_HEADER_SIZE);
227 if (size < expected) {
228 dev_err(&chip->dev, "Unable to read remainder of result\n");
233 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
234 &priv->int_queue, false) < 0) {
238 status = tpm_tis_status(chip);
239 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
240 dev_err(&chip->dev, "Error left over data\n");
251 * If interrupts are used (signaled by an irq set in the vendor structure)
252 * tpm.c can skip polling for the data to be available as the interrupt is
255 static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
257 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
258 int rc, status, burstcnt;
260 bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
262 status = tpm_tis_status(chip);
263 if ((status & TPM_STS_COMMAND_READY) == 0) {
265 if (wait_for_tpm_stat
266 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
267 &priv->int_queue, false) < 0) {
273 while (count < len - 1) {
274 burstcnt = get_burstcount(chip);
276 dev_err(&chip->dev, "Unable to read burstcount\n");
280 burstcnt = min_t(int, burstcnt, len - count - 1);
281 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
282 burstcnt, buf + count);
288 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
289 &priv->int_queue, false) < 0) {
293 status = tpm_tis_status(chip);
294 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
300 /* write last byte */
301 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
305 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
306 &priv->int_queue, false) < 0) {
310 status = tpm_tis_status(chip);
311 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
323 static void disable_interrupts(struct tpm_chip *chip)
325 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
329 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
333 intmask &= ~TPM_GLOBAL_INT_ENABLE;
334 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
336 devm_free_irq(chip->dev.parent, priv->irq, chip);
338 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
342 * If interrupts are used (signaled by an irq set in the vendor structure)
343 * tpm.c can skip polling for the data to be available as the interrupt is
346 static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
348 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
353 rc = tpm_tis_send_data(chip, buf, len);
358 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
362 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
363 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
365 if (chip->flags & TPM_CHIP_FLAG_TPM2)
366 dur = tpm2_calc_ordinal_duration(chip, ordinal);
368 dur = tpm_calc_ordinal_duration(chip, ordinal);
370 if (wait_for_tpm_stat
371 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
372 &priv->read_queue, false) < 0) {
383 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
386 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
388 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
389 return tpm_tis_send_main(chip, buf, len);
391 /* Verify receipt of the expected IRQ */
394 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
395 rc = tpm_tis_send_main(chip, buf, len);
397 chip->flags |= TPM_CHIP_FLAG_IRQ;
398 if (!priv->irq_tested)
400 if (!priv->irq_tested)
401 disable_interrupts(chip);
402 priv->irq_tested = true;
406 struct tis_vendor_timeout_override {
408 unsigned long timeout_us[4];
411 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
413 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
414 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
417 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
418 unsigned long *timeout_cap)
420 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
424 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
428 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
429 if (vendor_timeout_overrides[i].did_vid != did_vid)
431 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
432 sizeof(vendor_timeout_overrides[i].timeout_us));
440 * Early probing for iTPM with STS_DATA_EXPECT flaw.
441 * Try sending command without itpm flag set and if that
442 * fails, repeat with itpm flag set.
444 static int probe_itpm(struct tpm_chip *chip)
446 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
448 u8 cmd_getticks[] = {
449 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
450 0x00, 0x00, 0x00, 0xf1
452 size_t len = sizeof(cmd_getticks);
455 if (priv->flags & TPM_TIS_ITPM_WORKAROUND)
458 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
462 /* probe only iTPMS */
463 if (vendor != TPM_VID_INTEL)
466 if (request_locality(chip, 0) != 0)
469 rc = tpm_tis_send_data(chip, cmd_getticks, len);
475 priv->flags |= TPM_TIS_ITPM_WORKAROUND;
477 rc = tpm_tis_send_data(chip, cmd_getticks, len);
479 dev_info(&chip->dev, "Detected an iTPM.\n");
481 priv->flags &= ~TPM_TIS_ITPM_WORKAROUND;
487 release_locality(chip, priv->locality);
492 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
494 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
496 switch (priv->manufacturer_id) {
497 case TPM_VID_WINBOND:
498 return ((status == TPM_STS_VALID) ||
499 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
501 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
503 return (status == TPM_STS_COMMAND_READY);
507 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
509 struct tpm_chip *chip = dev_id;
510 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
514 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
521 priv->irq_tested = true;
522 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
523 wake_up_interruptible(&priv->read_queue);
524 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
525 for (i = 0; i < 5; i++)
526 if (check_locality(chip, i))
529 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
530 TPM_INTF_CMD_READY_INT))
531 wake_up_interruptible(&priv->int_queue);
533 /* Clear interrupts handled with TPM_EOI */
534 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
538 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
542 static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
544 const char *desc = "attempting to generate an interrupt";
548 if (chip->flags & TPM_CHIP_FLAG_TPM2)
549 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
551 return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
555 /* Register the IRQ and issue a command that will cause an interrupt. If an
556 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
557 * everything and leave in polling mode. Returns 0 on success.
559 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
562 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
567 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
568 dev_name(&chip->dev), chip) != 0) {
569 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
575 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
580 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
584 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
588 /* Clear all existing */
589 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
594 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
595 intmask | TPM_GLOBAL_INT_ENABLE);
599 priv->irq_tested = false;
601 /* Generate an interrupt by having the core call through to
604 rc = tpm_tis_gen_interrupt(chip);
608 /* tpm_tis_send will either confirm the interrupt is working or it
609 * will call disable_irq which undoes all of the above.
611 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
612 rc = tpm_tis_write8(priv, original_int_vec,
613 TPM_INT_VECTOR(priv->locality));
623 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
624 * do not have ACPI/etc. We typically expect the interrupt to be declared if
627 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
629 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
633 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
638 if (!original_int_vec) {
639 if (IS_ENABLED(CONFIG_X86))
640 for (i = 3; i <= 15; i++)
641 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
644 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
649 void tpm_tis_remove(struct tpm_chip *chip)
651 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
652 u32 reg = TPM_INT_ENABLE(priv->locality);
656 rc = tpm_tis_read32(priv, reg, &interrupt);
660 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
662 EXPORT_SYMBOL_GPL(tpm_tis_remove);
664 static const struct tpm_class_ops tpm_tis = {
665 .flags = TPM_OPS_AUTO_STARTUP,
666 .status = tpm_tis_status,
667 .recv = tpm_tis_recv,
668 .send = tpm_tis_send,
669 .cancel = tpm_tis_ready,
670 .update_timeouts = tpm_tis_update_timeouts,
671 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
672 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
673 .req_canceled = tpm_tis_req_canceled,
674 .request_locality = request_locality,
675 .relinquish_locality = release_locality,
678 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
679 const struct tpm_tis_phy_ops *phy_ops,
680 acpi_handle acpi_dev_handle)
682 u32 vendor, intfcaps, intmask;
685 struct tpm_chip *chip;
687 chip = tpmm_chip_alloc(dev, &tpm_tis);
689 return PTR_ERR(chip);
692 chip->acpi_dev_handle = acpi_dev_handle;
695 /* Maximum timeouts */
696 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
697 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
698 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
699 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
700 priv->phy_ops = phy_ops;
701 dev_set_drvdata(&chip->dev, priv);
703 if (wait_startup(chip, 0) != 0) {
708 /* Take control of the TPM's interrupt hardware and shut it off */
709 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
713 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
714 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
715 intmask &= ~TPM_GLOBAL_INT_ENABLE;
716 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
718 rc = tpm2_probe(chip);
722 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
726 priv->manufacturer_id = vendor;
728 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
732 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
733 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
736 probe = probe_itpm(chip);
742 /* Figure out the capabilities */
743 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
747 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
749 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
750 dev_dbg(dev, "\tBurst Count Static\n");
751 if (intfcaps & TPM_INTF_CMD_READY_INT)
752 dev_dbg(dev, "\tCommand Ready Int Support\n");
753 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
754 dev_dbg(dev, "\tInterrupt Edge Falling\n");
755 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
756 dev_dbg(dev, "\tInterrupt Edge Rising\n");
757 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
758 dev_dbg(dev, "\tInterrupt Level Low\n");
759 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
760 dev_dbg(dev, "\tInterrupt Level High\n");
761 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
762 dev_dbg(dev, "\tLocality Change Int Support\n");
763 if (intfcaps & TPM_INTF_STS_VALID_INT)
764 dev_dbg(dev, "\tSts Valid Int Support\n");
765 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
766 dev_dbg(dev, "\tData Avail Int Support\n");
768 /* INTERRUPT Setup */
769 init_waitqueue_head(&priv->read_queue);
770 init_waitqueue_head(&priv->int_queue);
772 /* Before doing irq testing issue a command to the TPM in polling mode
773 * to make sure it works. May as well use that command to set the
774 * proper timeouts for the driver.
776 if (tpm_get_timeouts(chip)) {
777 dev_err(dev, "Could not get TPM timeouts and durations\n");
783 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
785 if (!(chip->flags & TPM_CHIP_FLAG_IRQ))
786 dev_err(&chip->dev, FW_BUG
787 "TPM interrupt not working, polling instead\n");
789 tpm_tis_probe_irq(chip, intmask);
793 return tpm_chip_register(chip);
795 tpm_tis_remove(chip);
798 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
800 #ifdef CONFIG_PM_SLEEP
801 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
803 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
807 /* reenable interrupts that device may have lost or
808 * BIOS/firmware may have disabled
810 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
814 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
818 intmask |= TPM_INTF_CMD_READY_INT
819 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
820 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
822 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
825 int tpm_tis_resume(struct device *dev)
827 struct tpm_chip *chip = dev_get_drvdata(dev);
830 if (chip->flags & TPM_CHIP_FLAG_IRQ)
831 tpm_tis_reenable_interrupts(chip);
833 ret = tpm_pm_resume(dev);
837 /* TPM 1.2 requires self-test on resume. This function actually returns
838 * an error code but for unknown reason it isn't handled.
840 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
841 tpm_do_selftest(chip);
845 EXPORT_SYMBOL_GPL(tpm_tis_resume);
848 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
849 MODULE_DESCRIPTION("TPM Driver");
850 MODULE_VERSION("2.0");
851 MODULE_LICENSE("GPL");