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