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