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)
55 } while (time_before(jiffies, stop));
59 static int 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))
71 return priv->locality = l;
76 static void release_locality(struct tpm_chip *chip, int l, int force)
78 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
82 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
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);
93 static int request_locality(struct tpm_chip *chip, int l)
95 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
96 unsigned long stop, timeout;
99 if (check_locality(chip, l) >= 0)
102 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
106 stop = jiffies + chip->timeout_a;
108 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
110 timeout = stop - jiffies;
111 if ((long)timeout <= 0)
113 rc = wait_event_interruptible_timeout(priv->int_queue,
119 if (rc == -ERESTARTSYS && freezing(current)) {
120 clear_thread_flag(TIF_SIGPENDING);
124 /* wait for burstcount */
126 if (check_locality(chip, l) >= 0)
129 } while (time_before(jiffies, stop));
134 static u8 tpm_tis_status(struct tpm_chip *chip)
136 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
140 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
147 static void tpm_tis_ready(struct tpm_chip *chip)
149 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
151 /* this causes the current command to be aborted */
152 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
155 static int get_burstcount(struct tpm_chip *chip)
157 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
162 /* wait for burstcount */
163 if (chip->flags & TPM_CHIP_FLAG_TPM2)
164 stop = jiffies + chip->timeout_a;
166 stop = jiffies + chip->timeout_d;
168 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
172 burstcnt = (value >> 8) & 0xFFFF;
176 } while (time_before(jiffies, stop));
180 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
182 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
183 int size = 0, burstcnt, rc;
185 while (size < count &&
186 wait_for_tpm_stat(chip,
187 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
189 &priv->read_queue, true) == 0) {
190 burstcnt = get_burstcount(chip);
192 dev_err(&chip->dev, "Unable to read burstcount\n");
195 burstcnt = min_t(int, burstcnt, count - size);
197 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
198 burstcnt, buf + size);
207 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
209 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
214 if (count < TPM_HEADER_SIZE) {
219 size = recv_data(chip, buf, TPM_HEADER_SIZE);
220 /* read first 10 bytes, including tag, paramsize, and result */
221 if (size < TPM_HEADER_SIZE) {
222 dev_err(&chip->dev, "Unable to read header\n");
226 expected = be32_to_cpu(*(__be32 *) (buf + 2));
227 if (expected > count || expected < TPM_HEADER_SIZE) {
232 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
233 expected - TPM_HEADER_SIZE);
234 if (size < expected) {
235 dev_err(&chip->dev, "Unable to read remainder of result\n");
240 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
241 &priv->int_queue, false);
242 status = tpm_tis_status(chip);
243 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
244 dev_err(&chip->dev, "Error left over data\n");
251 release_locality(chip, priv->locality, 0);
256 * If interrupts are used (signaled by an irq set in the vendor structure)
257 * tpm.c can skip polling for the data to be available as the interrupt is
260 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
262 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
263 int rc, status, burstcnt;
265 bool itpm = priv->flags & TPM_TIS_ITPM_POSSIBLE;
267 if (request_locality(chip, 0) < 0)
270 status = tpm_tis_status(chip);
271 if ((status & TPM_STS_COMMAND_READY) == 0) {
273 if (wait_for_tpm_stat
274 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
275 &priv->int_queue, false) < 0) {
281 while (count < len - 1) {
282 burstcnt = get_burstcount(chip);
284 dev_err(&chip->dev, "Unable to read burstcount\n");
288 burstcnt = min_t(int, burstcnt, len - count - 1);
289 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
290 burstcnt, buf + count);
296 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
297 &priv->int_queue, false);
298 status = tpm_tis_status(chip);
299 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
305 /* write last byte */
306 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
310 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
311 &priv->int_queue, false);
312 status = tpm_tis_status(chip);
313 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
322 release_locality(chip, priv->locality, 0);
326 static void disable_interrupts(struct tpm_chip *chip)
328 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
335 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
339 intmask &= ~TPM_GLOBAL_INT_ENABLE;
340 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
342 devm_free_irq(chip->dev.parent, priv->irq, chip);
344 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
348 * If interrupts are used (signaled by an irq set in the vendor structure)
349 * tpm.c can skip polling for the data to be available as the interrupt is
352 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
354 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
359 rc = tpm_tis_send_data(chip, buf, len);
364 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
368 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
369 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
371 if (chip->flags & TPM_CHIP_FLAG_TPM2)
372 dur = tpm2_calc_ordinal_duration(chip, ordinal);
374 dur = tpm_calc_ordinal_duration(chip, ordinal);
376 if (wait_for_tpm_stat
377 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
378 &priv->read_queue, false) < 0) {
386 release_locality(chip, priv->locality, 0);
390 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
393 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
395 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
396 return tpm_tis_send_main(chip, buf, len);
398 /* Verify receipt of the expected IRQ */
401 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
402 rc = tpm_tis_send_main(chip, buf, len);
404 chip->flags |= TPM_CHIP_FLAG_IRQ;
405 if (!priv->irq_tested)
407 if (!priv->irq_tested)
408 disable_interrupts(chip);
409 priv->irq_tested = true;
413 struct tis_vendor_timeout_override {
415 unsigned long timeout_us[4];
418 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
420 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
421 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
424 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
425 unsigned long *timeout_cap)
427 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
431 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
435 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
436 if (vendor_timeout_overrides[i].did_vid != did_vid)
438 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
439 sizeof(vendor_timeout_overrides[i].timeout_us));
447 * Early probing for iTPM with STS_DATA_EXPECT flaw.
448 * Try sending command without itpm flag set and if that
449 * fails, repeat with itpm flag set.
451 static int probe_itpm(struct tpm_chip *chip)
453 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
455 u8 cmd_getticks[] = {
456 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
457 0x00, 0x00, 0x00, 0xf1
459 size_t len = sizeof(cmd_getticks);
462 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
466 /* probe only iTPMS */
467 if (vendor != TPM_VID_INTEL)
470 rc = tpm_tis_send_data(chip, cmd_getticks, len);
475 release_locality(chip, priv->locality, 0);
477 rc = tpm_tis_send_data(chip, cmd_getticks, len);
479 dev_info(&chip->dev, "Detected an iTPM.\n");
486 release_locality(chip, priv->locality, 0);
491 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
493 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
495 switch (priv->manufacturer_id) {
496 case TPM_VID_WINBOND:
497 return ((status == TPM_STS_VALID) ||
498 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
500 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
502 return (status == TPM_STS_COMMAND_READY);
506 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
508 struct tpm_chip *chip = dev_id;
509 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
513 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
520 priv->irq_tested = true;
521 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
522 wake_up_interruptible(&priv->read_queue);
523 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
524 for (i = 0; i < 5; i++)
525 if (check_locality(chip, i) >= 0)
528 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
529 TPM_INTF_CMD_READY_INT))
530 wake_up_interruptible(&priv->int_queue);
532 /* Clear interrupts handled with TPM_EOI */
533 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
537 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
541 static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
543 const char *desc = "attempting to generate an interrupt";
547 if (chip->flags & TPM_CHIP_FLAG_TPM2)
548 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
550 return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc);
553 /* Register the IRQ and issue a command that will cause an interrupt. If an
554 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
555 * everything and leave in polling mode. Returns 0 on success.
557 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
560 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
565 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
566 dev_name(&chip->dev), chip) != 0) {
567 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
573 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
578 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
582 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
586 /* Clear all existing */
587 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
592 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
593 intmask | TPM_GLOBAL_INT_ENABLE);
597 priv->irq_tested = false;
599 /* Generate an interrupt by having the core call through to
602 rc = tpm_tis_gen_interrupt(chip);
606 /* tpm_tis_send will either confirm the interrupt is working or it
607 * will call disable_irq which undoes all of the above.
609 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
610 rc = tpm_tis_write8(priv, original_int_vec,
611 TPM_INT_VECTOR(priv->locality));
621 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
622 * do not have ACPI/etc. We typically expect the interrupt to be declared if
625 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
627 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
631 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
636 if (!original_int_vec) {
637 if (IS_ENABLED(CONFIG_X86))
638 for (i = 3; i <= 15; i++)
639 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
642 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
647 void tpm_tis_remove(struct tpm_chip *chip)
649 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
650 u32 reg = TPM_INT_ENABLE(priv->locality);
654 rc = tpm_tis_read32(priv, reg, &interrupt);
658 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
659 release_locality(chip, priv->locality, 1);
661 EXPORT_SYMBOL_GPL(tpm_tis_remove);
663 static const struct tpm_class_ops tpm_tis = {
664 .flags = TPM_OPS_AUTO_STARTUP,
665 .status = tpm_tis_status,
666 .recv = tpm_tis_recv,
667 .send = tpm_tis_send,
668 .cancel = tpm_tis_ready,
669 .update_timeouts = tpm_tis_update_timeouts,
670 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
671 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
672 .req_canceled = tpm_tis_req_canceled,
675 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
676 const struct tpm_tis_phy_ops *phy_ops,
677 acpi_handle acpi_dev_handle)
679 u32 vendor, intfcaps, intmask;
682 struct tpm_chip *chip;
684 chip = tpmm_chip_alloc(dev, &tpm_tis);
686 return PTR_ERR(chip);
689 chip->acpi_dev_handle = acpi_dev_handle;
692 /* Maximum timeouts */
693 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
694 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
695 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
696 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
697 priv->phy_ops = phy_ops;
698 dev_set_drvdata(&chip->dev, priv);
700 if (wait_startup(chip, 0) != 0) {
705 /* Take control of the TPM's interrupt hardware and shut it off */
706 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
710 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
711 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
712 intmask &= ~TPM_GLOBAL_INT_ENABLE;
713 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
715 if (request_locality(chip, 0) != 0) {
720 rc = tpm2_probe(chip);
724 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
728 priv->manufacturer_id = vendor;
730 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
734 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
735 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
738 if (!(priv->flags & TPM_TIS_ITPM_POSSIBLE)) {
739 probe = probe_itpm(chip);
746 priv->flags |= TPM_TIS_ITPM_POSSIBLE;
749 /* Figure out the capabilities */
750 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
754 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
756 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
757 dev_dbg(dev, "\tBurst Count Static\n");
758 if (intfcaps & TPM_INTF_CMD_READY_INT)
759 dev_dbg(dev, "\tCommand Ready Int Support\n");
760 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
761 dev_dbg(dev, "\tInterrupt Edge Falling\n");
762 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
763 dev_dbg(dev, "\tInterrupt Edge Rising\n");
764 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
765 dev_dbg(dev, "\tInterrupt Level Low\n");
766 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
767 dev_dbg(dev, "\tInterrupt Level High\n");
768 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
769 dev_dbg(dev, "\tLocality Change Int Support\n");
770 if (intfcaps & TPM_INTF_STS_VALID_INT)
771 dev_dbg(dev, "\tSts Valid Int Support\n");
772 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
773 dev_dbg(dev, "\tData Avail Int Support\n");
775 /* Very early on issue a command to the TPM in polling mode to make
776 * sure it works. May as well use that command to set the proper
777 * timeouts for the driver.
779 if (tpm_get_timeouts(chip)) {
780 dev_err(dev, "Could not get TPM timeouts and durations\n");
785 /* INTERRUPT Setup */
786 init_waitqueue_head(&priv->read_queue);
787 init_waitqueue_head(&priv->int_queue);
790 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
792 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
793 dev_err(&chip->dev, FW_BUG
794 "TPM interrupt not working, polling instead\n");
796 disable_interrupts(chip);
799 tpm_tis_probe_irq(chip, intmask);
803 return tpm_chip_register(chip);
805 tpm_tis_remove(chip);
808 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
810 #ifdef CONFIG_PM_SLEEP
811 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
813 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
817 /* reenable interrupts that device may have lost or
818 * BIOS/firmware may have disabled
820 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
824 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
828 intmask |= TPM_INTF_CMD_READY_INT
829 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
830 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
832 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
835 int tpm_tis_resume(struct device *dev)
837 struct tpm_chip *chip = dev_get_drvdata(dev);
840 if (chip->flags & TPM_CHIP_FLAG_IRQ)
841 tpm_tis_reenable_interrupts(chip);
843 ret = tpm_pm_resume(dev);
847 /* TPM 1.2 requires self-test on resume. This function actually returns
848 * an error code but for unknown reason it isn't handled.
850 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
851 tpm_do_selftest(chip);
855 EXPORT_SYMBOL_GPL(tpm_tis_resume);
858 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
859 MODULE_DESCRIPTION("TPM Driver");
860 MODULE_VERSION("2.0");
861 MODULE_LICENSE("GPL");