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