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