forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/drivers/char/tpm/tpm_tis_core.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2005, 2006 IBM Corporation
34 * Copyright (C) 2014, 2015 Intel Corporation
....@@ -13,11 +14,6 @@
1314 *
1415 * This device driver implements the TPM interface as defined in
1516 * 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.
2117 */
2218 #include <linux/init.h>
2319 #include <linux/module.h>
....@@ -52,6 +48,7 @@
5248 unsigned long timeout, wait_queue_head_t *queue,
5349 bool check_cancel)
5450 {
51
+ struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
5552 unsigned long stop;
5653 long rc;
5754 u8 status;
....@@ -84,8 +81,8 @@
8481 }
8582 } else {
8683 do {
87
- usleep_range(TPM_TIMEOUT_USECS_MIN,
88
- TPM_TIMEOUT_USECS_MAX);
84
+ usleep_range(priv->timeout_min,
85
+ priv->timeout_max);
8986 status = chip->ops->status(chip);
9087 if ((status & mask) == mask)
9188 return 0;
....@@ -139,16 +136,27 @@
139136 return false;
140137 }
141138
142
-static int release_locality(struct tpm_chip *chip, int l)
139
+static int __tpm_tis_relinquish_locality(struct tpm_tis_data *priv, int l)
143140 {
144
- struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
145
-
146141 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
147142
148143 return 0;
149144 }
150145
151
-static int request_locality(struct tpm_chip *chip, int l)
146
+static int tpm_tis_relinquish_locality(struct tpm_chip *chip, int l)
147
+{
148
+ struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
149
+
150
+ mutex_lock(&priv->locality_count_mutex);
151
+ priv->locality_count--;
152
+ if (priv->locality_count == 0)
153
+ __tpm_tis_relinquish_locality(priv, l);
154
+ mutex_unlock(&priv->locality_count_mutex);
155
+
156
+ return 0;
157
+}
158
+
159
+static int __tpm_tis_request_locality(struct tpm_chip *chip, int l)
152160 {
153161 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
154162 unsigned long stop, timeout;
....@@ -189,6 +197,20 @@
189197 return -1;
190198 }
191199
200
+static int tpm_tis_request_locality(struct tpm_chip *chip, int l)
201
+{
202
+ struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
203
+ int ret = 0;
204
+
205
+ mutex_lock(&priv->locality_count_mutex);
206
+ if (priv->locality_count == 0)
207
+ ret = __tpm_tis_request_locality(chip, l);
208
+ if (!ret)
209
+ priv->locality_count++;
210
+ mutex_unlock(&priv->locality_count_mutex);
211
+ return ret;
212
+}
213
+
192214 static u8 tpm_tis_status(struct tpm_chip *chip)
193215 {
194216 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
....@@ -198,6 +220,28 @@
198220 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
199221 if (rc < 0)
200222 return 0;
223
+
224
+ if (unlikely((status & TPM_STS_READ_ZERO) != 0)) {
225
+ if (!test_and_set_bit(TPM_TIS_INVALID_STATUS, &priv->flags)) {
226
+ /*
227
+ * If this trips, the chances are the read is
228
+ * returning 0xff because the locality hasn't been
229
+ * acquired. Usually because tpm_try_get_ops() hasn't
230
+ * been called before doing a TPM operation.
231
+ */
232
+ dev_err(&chip->dev, "invalid TPM_STS.x 0x%02x, dumping stack for forensics\n",
233
+ status);
234
+
235
+ /*
236
+ * Dump stack for forensics, as invalid TPM_STS.x could be
237
+ * potentially triggered by impaired tpm_try_get_ops() or
238
+ * tpm_find_get_ops().
239
+ */
240
+ dump_stack();
241
+ }
242
+
243
+ return 0;
244
+ }
201245
202246 return status;
203247 }
....@@ -270,6 +314,7 @@
270314 int size = 0;
271315 int status;
272316 u32 expected;
317
+ int rc;
273318
274319 if (count < TPM_HEADER_SIZE) {
275320 size = -EIO;
....@@ -289,8 +334,13 @@
289334 goto out;
290335 }
291336
292
- size += recv_data(chip, &buf[TPM_HEADER_SIZE],
293
- expected - TPM_HEADER_SIZE);
337
+ rc = recv_data(chip, &buf[TPM_HEADER_SIZE],
338
+ expected - TPM_HEADER_SIZE);
339
+ if (rc < 0) {
340
+ size = rc;
341
+ goto out;
342
+ }
343
+ size += rc;
294344 if (size < expected) {
295345 dev_err(&chip->dev, "Unable to read remainder of result\n");
296346 size = -ETIME;
....@@ -419,10 +469,17 @@
419469 int rc;
420470 u32 ordinal;
421471 unsigned long dur;
472
+ unsigned int try;
422473
423
- rc = tpm_tis_send_data(chip, buf, len);
424
- if (rc < 0)
425
- return rc;
474
+ for (try = 0; try < TPM_RETRY; try++) {
475
+ rc = tpm_tis_send_data(chip, buf, len);
476
+ if (rc >= 0)
477
+ /* Data transfer done successfully */
478
+ break;
479
+ else if (rc != -EIO)
480
+ /* Data transfer failed, not recoverable */
481
+ return rc;
482
+ }
426483
427484 /* go and do it */
428485 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
....@@ -432,11 +489,7 @@
432489 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
433490 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
434491
435
- if (chip->flags & TPM_CHIP_FLAG_TPM2)
436
- dur = tpm2_calc_ordinal_duration(chip, ordinal);
437
- else
438
- dur = tpm_calc_ordinal_duration(chip, ordinal);
439
-
492
+ dur = tpm_calc_ordinal_duration(chip, ordinal);
440493 if (wait_for_tpm_stat
441494 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
442495 &priv->read_queue, false) < 0) {
....@@ -473,6 +526,84 @@
473526 return rc;
474527 }
475528
529
+struct tis_vendor_durations_override {
530
+ u32 did_vid;
531
+ struct tpm1_version version;
532
+ unsigned long durations[3];
533
+};
534
+
535
+static const struct tis_vendor_durations_override vendor_dur_overrides[] = {
536
+ /* STMicroelectronics 0x104a */
537
+ { 0x0000104a,
538
+ { 1, 2, 8, 28 },
539
+ { (2 * 60 * HZ), (2 * 60 * HZ), (2 * 60 * HZ) } },
540
+};
541
+
542
+static void tpm_tis_update_durations(struct tpm_chip *chip,
543
+ unsigned long *duration_cap)
544
+{
545
+ struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
546
+ struct tpm1_version *version;
547
+ u32 did_vid;
548
+ int i, rc;
549
+ cap_t cap;
550
+
551
+ chip->duration_adjusted = false;
552
+
553
+ if (chip->ops->clk_enable != NULL)
554
+ chip->ops->clk_enable(chip, true);
555
+
556
+ rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
557
+ if (rc < 0) {
558
+ dev_warn(&chip->dev, "%s: failed to read did_vid. %d\n",
559
+ __func__, rc);
560
+ goto out;
561
+ }
562
+
563
+ /* Try to get a TPM version 1.2 or 1.1 TPM_CAP_VERSION_INFO */
564
+ rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
565
+ "attempting to determine the 1.2 version",
566
+ sizeof(cap.version2));
567
+ if (!rc) {
568
+ version = &cap.version2.version;
569
+ } else {
570
+ rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
571
+ "attempting to determine the 1.1 version",
572
+ sizeof(cap.version1));
573
+
574
+ if (rc)
575
+ goto out;
576
+
577
+ version = &cap.version1;
578
+ }
579
+
580
+ for (i = 0; i != ARRAY_SIZE(vendor_dur_overrides); i++) {
581
+ if (vendor_dur_overrides[i].did_vid != did_vid)
582
+ continue;
583
+
584
+ if ((version->major ==
585
+ vendor_dur_overrides[i].version.major) &&
586
+ (version->minor ==
587
+ vendor_dur_overrides[i].version.minor) &&
588
+ (version->rev_major ==
589
+ vendor_dur_overrides[i].version.rev_major) &&
590
+ (version->rev_minor ==
591
+ vendor_dur_overrides[i].version.rev_minor)) {
592
+
593
+ memcpy(duration_cap,
594
+ vendor_dur_overrides[i].durations,
595
+ sizeof(vendor_dur_overrides[i].durations));
596
+
597
+ chip->duration_adjusted = true;
598
+ goto out;
599
+ }
600
+ }
601
+
602
+out:
603
+ if (chip->ops->clk_enable != NULL)
604
+ chip->ops->clk_enable(chip, false);
605
+}
606
+
476607 struct tis_vendor_timeout_override {
477608 u32 did_vid;
478609 unsigned long timeout_us[4];
....@@ -484,35 +615,38 @@
484615 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
485616 };
486617
487
-static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
618
+static void tpm_tis_update_timeouts(struct tpm_chip *chip,
488619 unsigned long *timeout_cap)
489620 {
490621 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
491622 int i, rc;
492623 u32 did_vid;
493624
625
+ chip->timeout_adjusted = false;
626
+
494627 if (chip->ops->clk_enable != NULL)
495628 chip->ops->clk_enable(chip, true);
496629
497630 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
498
- if (rc < 0)
631
+ if (rc < 0) {
632
+ dev_warn(&chip->dev, "%s: failed to read did_vid: %d\n",
633
+ __func__, rc);
499634 goto out;
635
+ }
500636
501637 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
502638 if (vendor_timeout_overrides[i].did_vid != did_vid)
503639 continue;
504640 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
505641 sizeof(vendor_timeout_overrides[i].timeout_us));
506
- rc = true;
642
+ chip->timeout_adjusted = true;
507643 }
508
-
509
- rc = false;
510644
511645 out:
512646 if (chip->ops->clk_enable != NULL)
513647 chip->ops->clk_enable(chip, false);
514648
515
- return rc;
649
+ return;
516650 }
517651
518652 /*
....@@ -542,7 +676,7 @@
542676 if (vendor != TPM_VID_INTEL)
543677 return 0;
544678
545
- if (request_locality(chip, 0) != 0)
679
+ if (tpm_tis_request_locality(chip, 0) != 0)
546680 return -EBUSY;
547681
548682 rc = tpm_tis_send_data(chip, cmd_getticks, len);
....@@ -563,7 +697,7 @@
563697
564698 out:
565699 tpm_tis_ready(chip);
566
- release_locality(chip, priv->locality);
700
+ tpm_tis_relinquish_locality(chip, priv->locality);
567701
568702 return rc;
569703 }
....@@ -610,7 +744,9 @@
610744 wake_up_interruptible(&priv->int_queue);
611745
612746 /* Clear interrupts handled with TPM_EOI */
747
+ tpm_tis_request_locality(chip, 0);
613748 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
749
+ tpm_tis_relinquish_locality(chip, 0);
614750 if (rc < 0)
615751 return IRQ_NONE;
616752
....@@ -618,17 +754,17 @@
618754 return IRQ_HANDLED;
619755 }
620756
621
-static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
757
+static void tpm_tis_gen_interrupt(struct tpm_chip *chip)
622758 {
623759 const char *desc = "attempting to generate an interrupt";
624760 u32 cap2;
625761 cap_t cap;
762
+ int ret;
626763
627764 if (chip->flags & TPM_CHIP_FLAG_TPM2)
628
- return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
765
+ ret = tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
629766 else
630
- return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
631
- 0);
767
+ ret = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc, 0);
632768 }
633769
634770 /* Register the IRQ and issue a command that will cause an interrupt. If an
....@@ -643,60 +779,66 @@
643779 int rc;
644780 u32 int_status;
645781
646
- if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
647
- dev_name(&chip->dev), chip) != 0) {
782
+
783
+ rc = devm_request_threaded_irq(chip->dev.parent, irq, NULL,
784
+ tis_int_handler, IRQF_ONESHOT | flags,
785
+ dev_name(&chip->dev), chip);
786
+ if (rc) {
648787 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
649788 irq);
650789 return -1;
651790 }
652791 priv->irq = irq;
653792
654
- rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
655
- &original_int_vec);
793
+ rc = tpm_tis_request_locality(chip, 0);
656794 if (rc < 0)
657795 return rc;
796
+
797
+ rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
798
+ &original_int_vec);
799
+ if (rc < 0) {
800
+ tpm_tis_relinquish_locality(chip, priv->locality);
801
+ return rc;
802
+ }
658803
659804 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
660805 if (rc < 0)
661
- return rc;
806
+ goto restore_irqs;
662807
663808 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
664809 if (rc < 0)
665
- return rc;
810
+ goto restore_irqs;
666811
667812 /* Clear all existing */
668813 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
669814 if (rc < 0)
670
- return rc;
671
-
815
+ goto restore_irqs;
672816 /* Turn on */
673817 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
674818 intmask | TPM_GLOBAL_INT_ENABLE);
675819 if (rc < 0)
676
- return rc;
820
+ goto restore_irqs;
677821
678822 priv->irq_tested = false;
679823
680824 /* Generate an interrupt by having the core call through to
681825 * tpm_tis_send
682826 */
683
- rc = tpm_tis_gen_interrupt(chip);
684
- if (rc < 0)
685
- return rc;
827
+ tpm_tis_gen_interrupt(chip);
686828
829
+restore_irqs:
687830 /* tpm_tis_send will either confirm the interrupt is working or it
688831 * will call disable_irq which undoes all of the above.
689832 */
690833 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
691
- rc = tpm_tis_write8(priv, original_int_vec,
692
- TPM_INT_VECTOR(priv->locality));
693
- if (rc < 0)
694
- return rc;
695
-
696
- return 1;
834
+ tpm_tis_write8(priv, original_int_vec,
835
+ TPM_INT_VECTOR(priv->locality));
836
+ rc = -1;
697837 }
698838
699
- return 0;
839
+ tpm_tis_relinquish_locality(chip, priv->locality);
840
+
841
+ return rc;
700842 }
701843
702844 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
....@@ -806,11 +948,12 @@
806948 .send = tpm_tis_send,
807949 .cancel = tpm_tis_ready,
808950 .update_timeouts = tpm_tis_update_timeouts,
951
+ .update_durations = tpm_tis_update_durations,
809952 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
810953 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
811954 .req_canceled = tpm_tis_req_canceled,
812
- .request_locality = request_locality,
813
- .relinquish_locality = release_locality,
955
+ .request_locality = tpm_tis_request_locality,
956
+ .relinquish_locality = tpm_tis_relinquish_locality,
814957 .clk_enable = tpm_tis_clkrun_enable,
815958 };
816959
....@@ -841,8 +984,25 @@
841984 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
842985 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
843986 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
987
+ priv->timeout_min = TPM_TIMEOUT_USECS_MIN;
988
+ priv->timeout_max = TPM_TIMEOUT_USECS_MAX;
844989 priv->phy_ops = phy_ops;
990
+ priv->locality_count = 0;
991
+ mutex_init(&priv->locality_count_mutex);
992
+
845993 dev_set_drvdata(&chip->dev, priv);
994
+
995
+ rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
996
+ if (rc < 0)
997
+ return rc;
998
+
999
+ priv->manufacturer_id = vendor;
1000
+
1001
+ if (priv->manufacturer_id == TPM_VID_ATML &&
1002
+ !(chip->flags & TPM_CHIP_FLAG_TPM2)) {
1003
+ priv->timeout_min = TIS_TIMEOUT_MIN_ATML;
1004
+ priv->timeout_max = TIS_TIMEOUT_MAX_ATML;
1005
+ }
8461006
8471007 if (is_bsw()) {
8481008 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
....@@ -875,24 +1035,22 @@
8751035 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
8761036 intmask &= ~TPM_GLOBAL_INT_ENABLE;
8771037
878
- rc = request_locality(chip, 0);
1038
+ rc = tpm_tis_request_locality(chip, 0);
8791039 if (rc < 0) {
8801040 rc = -ENODEV;
8811041 goto out_err;
8821042 }
8831043
8841044 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
885
- release_locality(chip, 0);
1045
+ tpm_tis_relinquish_locality(chip, 0);
8861046
887
- rc = tpm2_probe(chip);
1047
+ rc = tpm_chip_start(chip);
8881048 if (rc)
8891049 goto out_err;
890
-
891
- rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
892
- if (rc < 0)
1050
+ rc = tpm2_probe(chip);
1051
+ tpm_chip_stop(chip);
1052
+ if (rc)
8931053 goto out_err;
894
-
895
- priv->manufacturer_id = vendor;
8961054
8971055 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
8981056 if (rc < 0)
....@@ -938,27 +1096,41 @@
9381096 init_waitqueue_head(&priv->read_queue);
9391097 init_waitqueue_head(&priv->int_queue);
9401098 if (irq != -1) {
941
- /* Before doing irq testing issue a command to the TPM in polling mode
1099
+ /*
1100
+ * Before doing irq testing issue a command to the TPM in polling mode
9421101 * to make sure it works. May as well use that command to set the
9431102 * proper timeouts for the driver.
9441103 */
945
- if (tpm_get_timeouts(chip)) {
1104
+
1105
+ rc = tpm_tis_request_locality(chip, 0);
1106
+ if (rc < 0)
1107
+ goto out_err;
1108
+
1109
+ rc = tpm_get_timeouts(chip);
1110
+
1111
+ tpm_tis_relinquish_locality(chip, 0);
1112
+
1113
+ if (rc) {
9461114 dev_err(dev, "Could not get TPM timeouts and durations\n");
9471115 rc = -ENODEV;
9481116 goto out_err;
9491117 }
9501118
951
- if (irq) {
1119
+ if (irq)
9521120 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
9531121 irq);
954
- if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
955
- dev_err(&chip->dev, FW_BUG
1122
+ else
1123
+ tpm_tis_probe_irq(chip, intmask);
1124
+
1125
+ if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
1126
+ dev_err(&chip->dev, FW_BUG
9561127 "TPM interrupt not working, polling instead\n");
9571128
958
- disable_interrupts(chip);
959
- }
960
- } else {
961
- tpm_tis_probe_irq(chip, intmask);
1129
+ rc = tpm_tis_request_locality(chip, 0);
1130
+ if (rc < 0)
1131
+ goto out_err;
1132
+ disable_interrupts(chip);
1133
+ tpm_tis_relinquish_locality(chip, 0);
9621134 }
9631135 }
9641136
....@@ -1019,20 +1191,27 @@
10191191 struct tpm_chip *chip = dev_get_drvdata(dev);
10201192 int ret;
10211193
1194
+ ret = tpm_tis_request_locality(chip, 0);
1195
+ if (ret < 0)
1196
+ return ret;
1197
+
10221198 if (chip->flags & TPM_CHIP_FLAG_IRQ)
10231199 tpm_tis_reenable_interrupts(chip);
10241200
10251201 ret = tpm_pm_resume(dev);
10261202 if (ret)
1027
- return ret;
1203
+ goto out;
10281204
1029
- /* TPM 1.2 requires self-test on resume. This function actually returns
1205
+ /*
1206
+ * TPM 1.2 requires self-test on resume. This function actually returns
10301207 * an error code but for unknown reason it isn't handled.
10311208 */
10321209 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
1033
- tpm_do_selftest(chip);
1210
+ tpm1_do_selftest(chip);
1211
+out:
1212
+ tpm_tis_relinquish_locality(chip, 0);
10341213
1035
- return 0;
1214
+ return ret;
10361215 }
10371216 EXPORT_SYMBOL_GPL(tpm_tis_resume);
10381217 #endif