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