b89d125eec7f3c1e99e21ad1448f68e731b9b66a
[cascardo/linux.git] / drivers / char / tpm / tpm_tis.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
33 enum tis_access {
34         TPM_ACCESS_VALID = 0x80,
35         TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
36         TPM_ACCESS_REQUEST_PENDING = 0x04,
37         TPM_ACCESS_REQUEST_USE = 0x02,
38 };
39
40 enum tis_status {
41         TPM_STS_VALID = 0x80,
42         TPM_STS_COMMAND_READY = 0x40,
43         TPM_STS_GO = 0x20,
44         TPM_STS_DATA_AVAIL = 0x10,
45         TPM_STS_DATA_EXPECT = 0x08,
46 };
47
48 enum tis_int_flags {
49         TPM_GLOBAL_INT_ENABLE = 0x80000000,
50         TPM_INTF_BURST_COUNT_STATIC = 0x100,
51         TPM_INTF_CMD_READY_INT = 0x080,
52         TPM_INTF_INT_EDGE_FALLING = 0x040,
53         TPM_INTF_INT_EDGE_RISING = 0x020,
54         TPM_INTF_INT_LEVEL_LOW = 0x010,
55         TPM_INTF_INT_LEVEL_HIGH = 0x008,
56         TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
57         TPM_INTF_STS_VALID_INT = 0x002,
58         TPM_INTF_DATA_AVAIL_INT = 0x001,
59 };
60
61 enum tis_defaults {
62         TIS_MEM_BASE = 0xFED40000,
63         TIS_MEM_LEN = 0x5000,
64         TIS_SHORT_TIMEOUT = 750,        /* ms */
65         TIS_LONG_TIMEOUT = 2000,        /* 2 sec */
66 };
67
68 struct tpm_info {
69         unsigned long start;
70         unsigned long len;
71         unsigned int irq;
72 };
73
74 static struct tpm_info tis_default_info = {
75         .start = TIS_MEM_BASE,
76         .len = TIS_MEM_LEN,
77         .irq = 0,
78 };
79
80 /* Some timeout values are needed before it is known whether the chip is
81  * TPM 1.0 or TPM 2.0.
82  */
83 #define TIS_TIMEOUT_A_MAX       max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A)
84 #define TIS_TIMEOUT_B_MAX       max(TIS_LONG_TIMEOUT, TPM2_TIMEOUT_B)
85 #define TIS_TIMEOUT_C_MAX       max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_C)
86 #define TIS_TIMEOUT_D_MAX       max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_D)
87
88 #define TPM_ACCESS(l)                   (0x0000 | ((l) << 12))
89 #define TPM_INT_ENABLE(l)               (0x0008 | ((l) << 12))
90 #define TPM_INT_VECTOR(l)               (0x000C | ((l) << 12))
91 #define TPM_INT_STATUS(l)               (0x0010 | ((l) << 12))
92 #define TPM_INTF_CAPS(l)                (0x0014 | ((l) << 12))
93 #define TPM_STS(l)                      (0x0018 | ((l) << 12))
94 #define TPM_STS3(l)                     (0x001b | ((l) << 12))
95 #define TPM_DATA_FIFO(l)                (0x0024 | ((l) << 12))
96
97 #define TPM_DID_VID(l)                  (0x0F00 | ((l) << 12))
98 #define TPM_RID(l)                      (0x0F04 | ((l) << 12))
99
100 struct priv_data {
101         bool irq_tested;
102 };
103
104 #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
105 static int has_hid(struct acpi_device *dev, const char *hid)
106 {
107         struct acpi_hardware_id *id;
108
109         list_for_each_entry(id, &dev->pnp.ids, list)
110                 if (!strcmp(hid, id->id))
111                         return 1;
112
113         return 0;
114 }
115
116 static inline int is_itpm(struct acpi_device *dev)
117 {
118         return has_hid(dev, "INTC0102");
119 }
120
121 static inline int is_fifo(struct acpi_device *dev)
122 {
123         struct acpi_table_tpm2 *tbl;
124         acpi_status st;
125
126         /* TPM 1.2 FIFO */
127         if (!has_hid(dev, "MSFT0101"))
128                 return 1;
129
130         st = acpi_get_table(ACPI_SIG_TPM2, 1,
131                             (struct acpi_table_header **) &tbl);
132         if (ACPI_FAILURE(st)) {
133                 dev_err(&dev->dev, "failed to get TPM2 ACPI table\n");
134                 return 0;
135         }
136
137         if (le32_to_cpu(tbl->start_method) != TPM2_START_FIFO)
138                 return 0;
139
140         /* TPM 2.0 FIFO */
141         return 1;
142 }
143 #else
144 static inline int is_itpm(struct acpi_device *dev)
145 {
146         return 0;
147 }
148
149 static inline int is_fifo(struct acpi_device *dev)
150 {
151         return 1;
152 }
153 #endif
154
155 /* Before we attempt to access the TPM we must see that the valid bit is set.
156  * The specification says that this bit is 0 at reset and remains 0 until the
157  * 'TPM has gone through its self test and initialization and has established
158  * correct values in the other bits.' */
159 static int wait_startup(struct tpm_chip *chip, int l)
160 {
161         unsigned long stop = jiffies + chip->vendor.timeout_a;
162         do {
163                 if (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
164                     TPM_ACCESS_VALID)
165                         return 0;
166                 msleep(TPM_TIMEOUT);
167         } while (time_before(jiffies, stop));
168         return -1;
169 }
170
171 static int check_locality(struct tpm_chip *chip, int l)
172 {
173         if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
174              (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
175             (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
176                 return chip->vendor.locality = l;
177
178         return -1;
179 }
180
181 static void release_locality(struct tpm_chip *chip, int l, int force)
182 {
183         if (force || (ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
184                       (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
185             (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID))
186                 iowrite8(TPM_ACCESS_ACTIVE_LOCALITY,
187                          chip->vendor.iobase + TPM_ACCESS(l));
188 }
189
190 static int request_locality(struct tpm_chip *chip, int l)
191 {
192         unsigned long stop, timeout;
193         long rc;
194
195         if (check_locality(chip, l) >= 0)
196                 return l;
197
198         iowrite8(TPM_ACCESS_REQUEST_USE,
199                  chip->vendor.iobase + TPM_ACCESS(l));
200
201         stop = jiffies + chip->vendor.timeout_a;
202
203         if (chip->vendor.irq) {
204 again:
205                 timeout = stop - jiffies;
206                 if ((long)timeout <= 0)
207                         return -1;
208                 rc = wait_event_interruptible_timeout(chip->vendor.int_queue,
209                                                       (check_locality
210                                                        (chip, l) >= 0),
211                                                       timeout);
212                 if (rc > 0)
213                         return l;
214                 if (rc == -ERESTARTSYS && freezing(current)) {
215                         clear_thread_flag(TIF_SIGPENDING);
216                         goto again;
217                 }
218         } else {
219                 /* wait for burstcount */
220                 do {
221                         if (check_locality(chip, l) >= 0)
222                                 return l;
223                         msleep(TPM_TIMEOUT);
224                 }
225                 while (time_before(jiffies, stop));
226         }
227         return -1;
228 }
229
230 static u8 tpm_tis_status(struct tpm_chip *chip)
231 {
232         return ioread8(chip->vendor.iobase +
233                        TPM_STS(chip->vendor.locality));
234 }
235
236 static void tpm_tis_ready(struct tpm_chip *chip)
237 {
238         /* this causes the current command to be aborted */
239         iowrite8(TPM_STS_COMMAND_READY,
240                  chip->vendor.iobase + TPM_STS(chip->vendor.locality));
241 }
242
243 static int get_burstcount(struct tpm_chip *chip)
244 {
245         unsigned long stop;
246         int burstcnt;
247
248         /* wait for burstcount */
249         /* which timeout value, spec has 2 answers (c & d) */
250         stop = jiffies + chip->vendor.timeout_d;
251         do {
252                 burstcnt = ioread8(chip->vendor.iobase +
253                                    TPM_STS(chip->vendor.locality) + 1);
254                 burstcnt += ioread8(chip->vendor.iobase +
255                                     TPM_STS(chip->vendor.locality) +
256                                     2) << 8;
257                 if (burstcnt)
258                         return burstcnt;
259                 msleep(TPM_TIMEOUT);
260         } while (time_before(jiffies, stop));
261         return -EBUSY;
262 }
263
264 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
265 {
266         int size = 0, burstcnt;
267         while (size < count &&
268                wait_for_tpm_stat(chip,
269                                  TPM_STS_DATA_AVAIL | TPM_STS_VALID,
270                                  chip->vendor.timeout_c,
271                                  &chip->vendor.read_queue, true)
272                == 0) {
273                 burstcnt = get_burstcount(chip);
274                 for (; burstcnt > 0 && size < count; burstcnt--)
275                         buf[size++] = ioread8(chip->vendor.iobase +
276                                               TPM_DATA_FIFO(chip->vendor.
277                                                             locality));
278         }
279         return size;
280 }
281
282 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
283 {
284         int size = 0;
285         int expected, status;
286
287         if (count < TPM_HEADER_SIZE) {
288                 size = -EIO;
289                 goto out;
290         }
291
292         /* read first 10 bytes, including tag, paramsize, and result */
293         if ((size =
294              recv_data(chip, buf, TPM_HEADER_SIZE)) < TPM_HEADER_SIZE) {
295                 dev_err(chip->pdev, "Unable to read header\n");
296                 goto out;
297         }
298
299         expected = be32_to_cpu(*(__be32 *) (buf + 2));
300         if (expected > count) {
301                 size = -EIO;
302                 goto out;
303         }
304
305         if ((size +=
306              recv_data(chip, &buf[TPM_HEADER_SIZE],
307                        expected - TPM_HEADER_SIZE)) < expected) {
308                 dev_err(chip->pdev, "Unable to read remainder of result\n");
309                 size = -ETIME;
310                 goto out;
311         }
312
313         wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
314                           &chip->vendor.int_queue, false);
315         status = tpm_tis_status(chip);
316         if (status & TPM_STS_DATA_AVAIL) {      /* retry? */
317                 dev_err(chip->pdev, "Error left over data\n");
318                 size = -EIO;
319                 goto out;
320         }
321
322 out:
323         tpm_tis_ready(chip);
324         release_locality(chip, chip->vendor.locality, 0);
325         return size;
326 }
327
328 static bool itpm;
329 module_param(itpm, bool, 0444);
330 MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
331
332 /*
333  * If interrupts are used (signaled by an irq set in the vendor structure)
334  * tpm.c can skip polling for the data to be available as the interrupt is
335  * waited for here
336  */
337 static int tpm_tis_send_data(struct tpm_chip *chip, u8 *buf, size_t len)
338 {
339         int rc, status, burstcnt;
340         size_t count = 0;
341
342         if (request_locality(chip, 0) < 0)
343                 return -EBUSY;
344
345         status = tpm_tis_status(chip);
346         if ((status & TPM_STS_COMMAND_READY) == 0) {
347                 tpm_tis_ready(chip);
348                 if (wait_for_tpm_stat
349                     (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
350                      &chip->vendor.int_queue, false) < 0) {
351                         rc = -ETIME;
352                         goto out_err;
353                 }
354         }
355
356         while (count < len - 1) {
357                 burstcnt = get_burstcount(chip);
358                 for (; burstcnt > 0 && count < len - 1; burstcnt--) {
359                         iowrite8(buf[count], chip->vendor.iobase +
360                                  TPM_DATA_FIFO(chip->vendor.locality));
361                         count++;
362                 }
363
364                 wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
365                                   &chip->vendor.int_queue, false);
366                 status = tpm_tis_status(chip);
367                 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
368                         rc = -EIO;
369                         goto out_err;
370                 }
371         }
372
373         /* write last byte */
374         iowrite8(buf[count],
375                  chip->vendor.iobase + TPM_DATA_FIFO(chip->vendor.locality));
376         wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c,
377                           &chip->vendor.int_queue, false);
378         status = tpm_tis_status(chip);
379         if ((status & TPM_STS_DATA_EXPECT) != 0) {
380                 rc = -EIO;
381                 goto out_err;
382         }
383
384         return 0;
385
386 out_err:
387         tpm_tis_ready(chip);
388         release_locality(chip, chip->vendor.locality, 0);
389         return rc;
390 }
391
392 static void disable_interrupts(struct tpm_chip *chip)
393 {
394         u32 intmask;
395
396         intmask =
397             ioread32(chip->vendor.iobase +
398                      TPM_INT_ENABLE(chip->vendor.locality));
399         intmask &= ~TPM_GLOBAL_INT_ENABLE;
400         iowrite32(intmask,
401                   chip->vendor.iobase +
402                   TPM_INT_ENABLE(chip->vendor.locality));
403         devm_free_irq(chip->pdev, chip->vendor.irq, chip);
404         chip->vendor.irq = 0;
405 }
406
407 /*
408  * If interrupts are used (signaled by an irq set in the vendor structure)
409  * tpm.c can skip polling for the data to be available as the interrupt is
410  * waited for here
411  */
412 static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len)
413 {
414         int rc;
415         u32 ordinal;
416         unsigned long dur;
417
418         rc = tpm_tis_send_data(chip, buf, len);
419         if (rc < 0)
420                 return rc;
421
422         /* go and do it */
423         iowrite8(TPM_STS_GO,
424                  chip->vendor.iobase + TPM_STS(chip->vendor.locality));
425
426         if (chip->vendor.irq) {
427                 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
428
429                 if (chip->flags & TPM_CHIP_FLAG_TPM2)
430                         dur = tpm2_calc_ordinal_duration(chip, ordinal);
431                 else
432                         dur = tpm_calc_ordinal_duration(chip, ordinal);
433
434                 if (wait_for_tpm_stat
435                     (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
436                      &chip->vendor.read_queue, false) < 0) {
437                         rc = -ETIME;
438                         goto out_err;
439                 }
440         }
441         return len;
442 out_err:
443         tpm_tis_ready(chip);
444         release_locality(chip, chip->vendor.locality, 0);
445         return rc;
446 }
447
448 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
449 {
450         int rc, irq;
451         struct priv_data *priv = chip->vendor.priv;
452
453         if (!chip->vendor.irq || priv->irq_tested)
454                 return tpm_tis_send_main(chip, buf, len);
455
456         /* Verify receipt of the expected IRQ */
457         irq = chip->vendor.irq;
458         chip->vendor.irq = 0;
459         rc = tpm_tis_send_main(chip, buf, len);
460         chip->vendor.irq = irq;
461         if (!priv->irq_tested)
462                 msleep(1);
463         if (!priv->irq_tested)
464                 disable_interrupts(chip);
465         priv->irq_tested = true;
466         return rc;
467 }
468
469 struct tis_vendor_timeout_override {
470         u32 did_vid;
471         unsigned long timeout_us[4];
472 };
473
474 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
475         /* Atmel 3204 */
476         { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
477                         (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
478 };
479
480 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
481                                     unsigned long *timeout_cap)
482 {
483         int i;
484         u32 did_vid;
485
486         did_vid = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
487
488         for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
489                 if (vendor_timeout_overrides[i].did_vid != did_vid)
490                         continue;
491                 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
492                        sizeof(vendor_timeout_overrides[i].timeout_us));
493                 return true;
494         }
495
496         return false;
497 }
498
499 /*
500  * Early probing for iTPM with STS_DATA_EXPECT flaw.
501  * Try sending command without itpm flag set and if that
502  * fails, repeat with itpm flag set.
503  */
504 static int probe_itpm(struct tpm_chip *chip)
505 {
506         int rc = 0;
507         u8 cmd_getticks[] = {
508                 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
509                 0x00, 0x00, 0x00, 0xf1
510         };
511         size_t len = sizeof(cmd_getticks);
512         bool rem_itpm = itpm;
513         u16 vendor = ioread16(chip->vendor.iobase + TPM_DID_VID(0));
514
515         /* probe only iTPMS */
516         if (vendor != TPM_VID_INTEL)
517                 return 0;
518
519         itpm = false;
520
521         rc = tpm_tis_send_data(chip, cmd_getticks, len);
522         if (rc == 0)
523                 goto out;
524
525         tpm_tis_ready(chip);
526         release_locality(chip, chip->vendor.locality, 0);
527
528         itpm = true;
529
530         rc = tpm_tis_send_data(chip, cmd_getticks, len);
531         if (rc == 0) {
532                 dev_info(chip->pdev, "Detected an iTPM.\n");
533                 rc = 1;
534         } else
535                 rc = -EFAULT;
536
537 out:
538         itpm = rem_itpm;
539         tpm_tis_ready(chip);
540         release_locality(chip, chip->vendor.locality, 0);
541
542         return rc;
543 }
544
545 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
546 {
547         switch (chip->vendor.manufacturer_id) {
548         case TPM_VID_WINBOND:
549                 return ((status == TPM_STS_VALID) ||
550                         (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
551         case TPM_VID_STM:
552                 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
553         default:
554                 return (status == TPM_STS_COMMAND_READY);
555         }
556 }
557
558 static const struct tpm_class_ops tpm_tis = {
559         .status = tpm_tis_status,
560         .recv = tpm_tis_recv,
561         .send = tpm_tis_send,
562         .cancel = tpm_tis_ready,
563         .update_timeouts = tpm_tis_update_timeouts,
564         .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
565         .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
566         .req_canceled = tpm_tis_req_canceled,
567 };
568
569 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
570 {
571         struct tpm_chip *chip = dev_id;
572         u32 interrupt;
573         int i;
574
575         interrupt = ioread32(chip->vendor.iobase +
576                              TPM_INT_STATUS(chip->vendor.locality));
577
578         if (interrupt == 0)
579                 return IRQ_NONE;
580
581         ((struct priv_data *)chip->vendor.priv)->irq_tested = true;
582         if (interrupt & TPM_INTF_DATA_AVAIL_INT)
583                 wake_up_interruptible(&chip->vendor.read_queue);
584         if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
585                 for (i = 0; i < 5; i++)
586                         if (check_locality(chip, i) >= 0)
587                                 break;
588         if (interrupt &
589             (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
590              TPM_INTF_CMD_READY_INT))
591                 wake_up_interruptible(&chip->vendor.int_queue);
592
593         /* Clear interrupts handled with TPM_EOI */
594         iowrite32(interrupt,
595                   chip->vendor.iobase +
596                   TPM_INT_STATUS(chip->vendor.locality));
597         ioread32(chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
598         return IRQ_HANDLED;
599 }
600
601 /* Register the IRQ and issue a command that will cause an interrupt. If an
602  * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
603  * everything and leave in polling mode. Returns 0 on success.
604  */
605 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
606                                     int flags, int irq)
607 {
608         struct priv_data *priv = chip->vendor.priv;
609         u8 original_int_vec;
610
611         if (devm_request_irq(chip->pdev, irq, tis_int_handler, flags,
612                              chip->devname, chip) != 0) {
613                 dev_info(chip->pdev, "Unable to request irq: %d for probe\n",
614                          irq);
615                 return -1;
616         }
617         chip->vendor.irq = irq;
618
619         original_int_vec = ioread8(chip->vendor.iobase +
620                                    TPM_INT_VECTOR(chip->vendor.locality));
621         iowrite8(irq,
622                  chip->vendor.iobase + TPM_INT_VECTOR(chip->vendor.locality));
623
624         /* Clear all existing */
625         iowrite32(ioread32(chip->vendor.iobase +
626                            TPM_INT_STATUS(chip->vendor.locality)),
627                   chip->vendor.iobase + TPM_INT_STATUS(chip->vendor.locality));
628
629         /* Turn on */
630         iowrite32(intmask | TPM_GLOBAL_INT_ENABLE,
631                   chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
632
633         priv->irq_tested = false;
634
635         /* Generate an interrupt by having the core call through to
636          * tpm_tis_send
637          */
638         if (chip->flags & TPM_CHIP_FLAG_TPM2)
639                 tpm2_gen_interrupt(chip);
640         else
641                 tpm_gen_interrupt(chip);
642
643         /* tpm_tis_send will either confirm the interrupt is working or it
644          * will call disable_irq which undoes all of the above.
645          */
646         if (!chip->vendor.irq) {
647                 iowrite8(original_int_vec,
648                          chip->vendor.iobase +
649                              TPM_INT_VECTOR(chip->vendor.locality));
650                 return 1;
651         }
652
653         return 0;
654 }
655
656 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
657  * do not have ACPI/etc. We typically expect the interrupt to be declared if
658  * present.
659  */
660 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
661 {
662         u8 original_int_vec;
663         int i;
664
665         original_int_vec = ioread8(chip->vendor.iobase +
666                                    TPM_INT_VECTOR(chip->vendor.locality));
667
668         if (!original_int_vec) {
669                 if (IS_ENABLED(CONFIG_X86))
670                         for (i = 3; i <= 15; i++)
671                                 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
672                                                               i))
673                                         return;
674         } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
675                                              original_int_vec))
676                 return;
677 }
678
679 static bool interrupts = true;
680 module_param(interrupts, bool, 0444);
681 MODULE_PARM_DESC(interrupts, "Enable interrupts");
682
683 static void tpm_tis_remove(struct tpm_chip *chip)
684 {
685         if (chip->flags & TPM_CHIP_FLAG_TPM2)
686                 tpm2_shutdown(chip, TPM2_SU_CLEAR);
687
688         iowrite32(~TPM_GLOBAL_INT_ENABLE &
689                   ioread32(chip->vendor.iobase +
690                            TPM_INT_ENABLE(chip->vendor.
691                                           locality)),
692                   chip->vendor.iobase +
693                   TPM_INT_ENABLE(chip->vendor.locality));
694         release_locality(chip, chip->vendor.locality, 1);
695 }
696
697 static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info,
698                         acpi_handle acpi_dev_handle)
699 {
700         u32 vendor, intfcaps, intmask;
701         int rc, probe;
702         struct tpm_chip *chip;
703         struct priv_data *priv;
704
705         priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
706         if (priv == NULL)
707                 return -ENOMEM;
708
709         chip = tpmm_chip_alloc(dev, &tpm_tis);
710         if (IS_ERR(chip))
711                 return PTR_ERR(chip);
712
713         chip->vendor.priv = priv;
714 #ifdef CONFIG_ACPI
715         chip->acpi_dev_handle = acpi_dev_handle;
716 #endif
717
718         chip->vendor.iobase = devm_ioremap(dev, tpm_info->start, tpm_info->len);
719         if (!chip->vendor.iobase)
720                 return -EIO;
721
722         /* Maximum timeouts */
723         chip->vendor.timeout_a = TIS_TIMEOUT_A_MAX;
724         chip->vendor.timeout_b = TIS_TIMEOUT_B_MAX;
725         chip->vendor.timeout_c = TIS_TIMEOUT_C_MAX;
726         chip->vendor.timeout_d = TIS_TIMEOUT_D_MAX;
727
728         if (wait_startup(chip, 0) != 0) {
729                 rc = -ENODEV;
730                 goto out_err;
731         }
732
733         /* Take control of the TPM's interrupt hardware and shut it off */
734         intmask = ioread32(chip->vendor.iobase +
735                            TPM_INT_ENABLE(chip->vendor.locality));
736         intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
737                    TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
738         intmask &= ~TPM_GLOBAL_INT_ENABLE;
739         iowrite32(intmask,
740                   chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
741
742         if (request_locality(chip, 0) != 0) {
743                 rc = -ENODEV;
744                 goto out_err;
745         }
746
747         rc = tpm2_probe(chip);
748         if (rc)
749                 goto out_err;
750
751         vendor = ioread32(chip->vendor.iobase + TPM_DID_VID(0));
752         chip->vendor.manufacturer_id = vendor;
753
754         dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
755                  (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
756                  vendor >> 16, ioread8(chip->vendor.iobase + TPM_RID(0)));
757
758         if (!itpm) {
759                 probe = probe_itpm(chip);
760                 if (probe < 0) {
761                         rc = -ENODEV;
762                         goto out_err;
763                 }
764                 itpm = !!probe;
765         }
766
767         if (itpm)
768                 dev_info(dev, "Intel iTPM workaround enabled\n");
769
770
771         /* Figure out the capabilities */
772         intfcaps =
773             ioread32(chip->vendor.iobase +
774                      TPM_INTF_CAPS(chip->vendor.locality));
775         dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
776                 intfcaps);
777         if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
778                 dev_dbg(dev, "\tBurst Count Static\n");
779         if (intfcaps & TPM_INTF_CMD_READY_INT)
780                 dev_dbg(dev, "\tCommand Ready Int Support\n");
781         if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
782                 dev_dbg(dev, "\tInterrupt Edge Falling\n");
783         if (intfcaps & TPM_INTF_INT_EDGE_RISING)
784                 dev_dbg(dev, "\tInterrupt Edge Rising\n");
785         if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
786                 dev_dbg(dev, "\tInterrupt Level Low\n");
787         if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
788                 dev_dbg(dev, "\tInterrupt Level High\n");
789         if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
790                 dev_dbg(dev, "\tLocality Change Int Support\n");
791         if (intfcaps & TPM_INTF_STS_VALID_INT)
792                 dev_dbg(dev, "\tSts Valid Int Support\n");
793         if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
794                 dev_dbg(dev, "\tData Avail Int Support\n");
795
796         /* Very early on issue a command to the TPM in polling mode to make
797          * sure it works. May as well use that command to set the proper
798          *  timeouts for the driver.
799          */
800         if (tpm_get_timeouts(chip)) {
801                 dev_err(dev, "Could not get TPM timeouts and durations\n");
802                 rc = -ENODEV;
803                 goto out_err;
804         }
805
806         /* INTERRUPT Setup */
807         init_waitqueue_head(&chip->vendor.read_queue);
808         init_waitqueue_head(&chip->vendor.int_queue);
809         if (interrupts) {
810                 if (tpm_info->irq) {
811                         tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
812                                                  tpm_info->irq);
813                         if (!chip->vendor.irq)
814                                 dev_err(chip->pdev, FW_BUG
815                                         "TPM interrupt not working, polling instead\n");
816                 } else
817                         tpm_tis_probe_irq(chip, intmask);
818         }
819
820         if (chip->flags & TPM_CHIP_FLAG_TPM2) {
821                 rc = tpm2_do_selftest(chip);
822                 if (rc == TPM2_RC_INITIALIZE) {
823                         dev_warn(dev, "Firmware has not started TPM\n");
824                         rc  = tpm2_startup(chip, TPM2_SU_CLEAR);
825                         if (!rc)
826                                 rc = tpm2_do_selftest(chip);
827                 }
828
829                 if (rc) {
830                         dev_err(dev, "TPM self test failed\n");
831                         if (rc > 0)
832                                 rc = -ENODEV;
833                         goto out_err;
834                 }
835         } else {
836                 if (tpm_do_selftest(chip)) {
837                         dev_err(dev, "TPM self test failed\n");
838                         rc = -ENODEV;
839                         goto out_err;
840                 }
841         }
842
843         return tpm_chip_register(chip);
844 out_err:
845         tpm_tis_remove(chip);
846         return rc;
847 }
848
849 #ifdef CONFIG_PM_SLEEP
850 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
851 {
852         u32 intmask;
853
854         /* reenable interrupts that device may have lost or
855            BIOS/firmware may have disabled */
856         iowrite8(chip->vendor.irq, chip->vendor.iobase +
857                  TPM_INT_VECTOR(chip->vendor.locality));
858
859         intmask =
860             ioread32(chip->vendor.iobase +
861                      TPM_INT_ENABLE(chip->vendor.locality));
862
863         intmask |= TPM_INTF_CMD_READY_INT
864             | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
865             | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
866
867         iowrite32(intmask,
868                   chip->vendor.iobase + TPM_INT_ENABLE(chip->vendor.locality));
869 }
870
871 static int tpm_tis_resume(struct device *dev)
872 {
873         struct tpm_chip *chip = dev_get_drvdata(dev);
874         int ret;
875
876         if (chip->vendor.irq)
877                 tpm_tis_reenable_interrupts(chip);
878
879         ret = tpm_pm_resume(dev);
880         if (ret)
881                 return ret;
882
883         /* TPM 1.2 requires self-test on resume. This function actually returns
884          * an error code but for unknown reason it isn't handled.
885          */
886         if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
887                 tpm_do_selftest(chip);
888
889         return 0;
890 }
891 #endif
892
893 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
894
895 #ifdef CONFIG_PNP
896 static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
897                                       const struct pnp_device_id *pnp_id)
898 {
899         struct tpm_info tpm_info = tis_default_info;
900         acpi_handle acpi_dev_handle = NULL;
901
902         tpm_info.start = pnp_mem_start(pnp_dev, 0);
903         tpm_info.len = pnp_mem_len(pnp_dev, 0);
904
905         if (pnp_irq_valid(pnp_dev, 0))
906                 tpm_info.irq = pnp_irq(pnp_dev, 0);
907         else
908                 interrupts = false;
909
910 #ifdef CONFIG_ACPI
911         if (pnp_acpi_device(pnp_dev)) {
912                 if (is_itpm(pnp_acpi_device(pnp_dev)))
913                         itpm = true;
914
915                 acpi_dev_handle = pnp_acpi_device(pnp_dev)->handle;
916         }
917 #endif
918
919         return tpm_tis_init(&pnp_dev->dev, &tpm_info, acpi_dev_handle);
920 }
921
922 static struct pnp_device_id tpm_pnp_tbl[] = {
923         {"PNP0C31", 0},         /* TPM */
924         {"ATM1200", 0},         /* Atmel */
925         {"IFX0102", 0},         /* Infineon */
926         {"BCM0101", 0},         /* Broadcom */
927         {"BCM0102", 0},         /* Broadcom */
928         {"NSC1200", 0},         /* National */
929         {"ICO0102", 0},         /* Intel */
930         /* Add new here */
931         {"", 0},                /* User Specified */
932         {"", 0}                 /* Terminator */
933 };
934 MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
935
936 static void tpm_tis_pnp_remove(struct pnp_dev *dev)
937 {
938         struct tpm_chip *chip = pnp_get_drvdata(dev);
939
940         tpm_chip_unregister(chip);
941         tpm_tis_remove(chip);
942 }
943
944 static struct pnp_driver tis_pnp_driver = {
945         .name = "tpm_tis",
946         .id_table = tpm_pnp_tbl,
947         .probe = tpm_tis_pnp_init,
948         .remove = tpm_tis_pnp_remove,
949         .driver = {
950                 .pm = &tpm_tis_pm,
951         },
952 };
953
954 #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
955 module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
956                     sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
957 MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
958 #endif
959
960 #ifdef CONFIG_ACPI
961 static int tpm_check_resource(struct acpi_resource *ares, void *data)
962 {
963         struct tpm_info *tpm_info = (struct tpm_info *) data;
964         struct resource res;
965
966         if (acpi_dev_resource_interrupt(ares, 0, &res)) {
967                 tpm_info->irq = res.start;
968         } else if (acpi_dev_resource_memory(ares, &res)) {
969                 tpm_info->start = res.start;
970                 tpm_info->len = resource_size(&res);
971         }
972
973         return 1;
974 }
975
976 static int tpm_tis_acpi_init(struct acpi_device *acpi_dev)
977 {
978         struct list_head resources;
979         struct tpm_info tpm_info = tis_default_info;
980         int ret;
981
982         if (!is_fifo(acpi_dev))
983                 return -ENODEV;
984
985         INIT_LIST_HEAD(&resources);
986         ret = acpi_dev_get_resources(acpi_dev, &resources, tpm_check_resource,
987                                      &tpm_info);
988         if (ret < 0)
989                 return ret;
990
991         acpi_dev_free_resource_list(&resources);
992
993         if (!tpm_info.irq)
994                 interrupts = false;
995
996         if (is_itpm(acpi_dev))
997                 itpm = true;
998
999         return tpm_tis_init(&acpi_dev->dev, &tpm_info, acpi_dev->handle);
1000 }
1001
1002 static int tpm_tis_acpi_remove(struct acpi_device *dev)
1003 {
1004         struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
1005
1006         tpm_chip_unregister(chip);
1007         tpm_tis_remove(chip);
1008
1009         return 0;
1010 }
1011
1012 static struct acpi_device_id tpm_acpi_tbl[] = {
1013         {"MSFT0101", 0},        /* TPM 2.0 */
1014         /* Add new here */
1015         {"", 0},                /* User Specified */
1016         {"", 0}                 /* Terminator */
1017 };
1018 MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
1019
1020 static struct acpi_driver tis_acpi_driver = {
1021         .name = "tpm_tis",
1022         .ids = tpm_acpi_tbl,
1023         .ops = {
1024                 .add = tpm_tis_acpi_init,
1025                 .remove = tpm_tis_acpi_remove,
1026         },
1027         .drv = {
1028                 .pm = &tpm_tis_pm,
1029         },
1030 };
1031 #endif
1032
1033 static struct platform_driver tis_drv = {
1034         .driver = {
1035                 .name           = "tpm_tis",
1036                 .pm             = &tpm_tis_pm,
1037         },
1038 };
1039
1040 static struct platform_device *pdev;
1041
1042 static bool force;
1043 module_param(force, bool, 0444);
1044 MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
1045 static int __init init_tis(void)
1046 {
1047         int rc;
1048 #ifdef CONFIG_PNP
1049         if (!force) {
1050                 rc = pnp_register_driver(&tis_pnp_driver);
1051                 if (rc)
1052                         return rc;
1053         }
1054 #endif
1055 #ifdef CONFIG_ACPI
1056         if (!force) {
1057                 rc = acpi_bus_register_driver(&tis_acpi_driver);
1058                 if (rc) {
1059 #ifdef CONFIG_PNP
1060                         pnp_unregister_driver(&tis_pnp_driver);
1061 #endif
1062                         return rc;
1063                 }
1064         }
1065 #endif
1066         if (!force)
1067                 return 0;
1068
1069         rc = platform_driver_register(&tis_drv);
1070         if (rc < 0)
1071                 return rc;
1072         pdev = platform_device_register_simple("tpm_tis", -1, NULL, 0);
1073         if (IS_ERR(pdev)) {
1074                 rc = PTR_ERR(pdev);
1075                 goto err_dev;
1076         }
1077         rc = tpm_tis_init(&pdev->dev, &tis_default_info, NULL);
1078         if (rc)
1079                 goto err_init;
1080         return 0;
1081 err_init:
1082         platform_device_unregister(pdev);
1083 err_dev:
1084         platform_driver_unregister(&tis_drv);
1085         return rc;
1086 }
1087
1088 static void __exit cleanup_tis(void)
1089 {
1090         struct tpm_chip *chip;
1091 #if defined(CONFIG_PNP) || defined(CONFIG_ACPI)
1092         if (!force) {
1093 #ifdef CONFIG_ACPI
1094                 acpi_bus_unregister_driver(&tis_acpi_driver);
1095 #endif
1096 #ifdef CONFIG_PNP
1097                 pnp_unregister_driver(&tis_pnp_driver);
1098 #endif
1099                 return;
1100         }
1101 #endif
1102         chip = dev_get_drvdata(&pdev->dev);
1103         tpm_chip_unregister(chip);
1104         tpm_tis_remove(chip);
1105         platform_device_unregister(pdev);
1106         platform_driver_unregister(&tis_drv);
1107 }
1108
1109 module_init(init_tis);
1110 module_exit(cleanup_tis);
1111 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1112 MODULE_DESCRIPTION("TPM Driver");
1113 MODULE_VERSION("2.0");
1114 MODULE_LICENSE("GPL");