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 */
wait_startup(struct tpm_chip * chip,int l)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
check_locality(struct tpm_chip * chip,int l)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
release_locality(struct tpm_chip * chip,int l,int force)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
request_locality(struct tpm_chip * chip,int l)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
tpm_tis_status(struct tpm_chip * chip)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
tpm_tis_ready(struct tpm_chip * chip)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
get_burstcount(struct tpm_chip * chip)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
recv_data(struct tpm_chip * chip,u8 * buf,size_t count)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
tpm_tis_recv(struct tpm_chip * chip,u8 * buf,size_t count)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 */
tpm_tis_send_data(struct tpm_chip * chip,const u8 * buf,size_t len)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
disable_interrupts(struct tpm_chip * chip)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 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
333 if (rc < 0)
334 intmask = 0;
335
336 intmask &= ~TPM_GLOBAL_INT_ENABLE;
337 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
338
339 devm_free_irq(chip->dev.parent, priv->irq, chip);
340 priv->irq = 0;
341 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
342 }
343
344 /*
345 * If interrupts are used (signaled by an irq set in the vendor structure)
346 * tpm.c can skip polling for the data to be available as the interrupt is
347 * waited for here
348 */
tpm_tis_send_main(struct tpm_chip * chip,const u8 * buf,size_t len)349 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
350 {
351 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
352 int rc;
353 u32 ordinal;
354 unsigned long dur;
355
356 rc = tpm_tis_send_data(chip, buf, len);
357 if (rc < 0)
358 return rc;
359
360 /* go and do it */
361 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
362 if (rc < 0)
363 goto out_err;
364
365 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
366 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
367
368 if (chip->flags & TPM_CHIP_FLAG_TPM2)
369 dur = tpm2_calc_ordinal_duration(chip, ordinal);
370 else
371 dur = tpm_calc_ordinal_duration(chip, ordinal);
372
373 if (wait_for_tpm_stat
374 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
375 &priv->read_queue, false) < 0) {
376 rc = -ETIME;
377 goto out_err;
378 }
379 }
380 return len;
381 out_err:
382 tpm_tis_ready(chip);
383 release_locality(chip, priv->locality, 0);
384 return rc;
385 }
386
tpm_tis_send(struct tpm_chip * chip,u8 * buf,size_t len)387 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
388 {
389 int rc, irq;
390 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
391
392 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
393 return tpm_tis_send_main(chip, buf, len);
394
395 /* Verify receipt of the expected IRQ */
396 irq = priv->irq;
397 priv->irq = 0;
398 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
399 rc = tpm_tis_send_main(chip, buf, len);
400 priv->irq = irq;
401 chip->flags |= TPM_CHIP_FLAG_IRQ;
402 if (!priv->irq_tested)
403 msleep(1);
404 if (!priv->irq_tested)
405 disable_interrupts(chip);
406 priv->irq_tested = true;
407 return rc;
408 }
409
410 struct tis_vendor_timeout_override {
411 u32 did_vid;
412 unsigned long timeout_us[4];
413 };
414
415 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
416 /* Atmel 3204 */
417 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
418 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
419 };
420
tpm_tis_update_timeouts(struct tpm_chip * chip,unsigned long * timeout_cap)421 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
422 unsigned long *timeout_cap)
423 {
424 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
425 int i, rc;
426 u32 did_vid;
427
428 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
429 if (rc < 0)
430 return rc;
431
432 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
433 if (vendor_timeout_overrides[i].did_vid != did_vid)
434 continue;
435 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
436 sizeof(vendor_timeout_overrides[i].timeout_us));
437 return true;
438 }
439
440 return false;
441 }
442
443 /*
444 * Early probing for iTPM with STS_DATA_EXPECT flaw.
445 * Try sending command without itpm flag set and if that
446 * fails, repeat with itpm flag set.
447 */
probe_itpm(struct tpm_chip * chip)448 static int probe_itpm(struct tpm_chip *chip)
449 {
450 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
451 int rc = 0;
452 u8 cmd_getticks[] = {
453 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
454 0x00, 0x00, 0x00, 0xf1
455 };
456 size_t len = sizeof(cmd_getticks);
457 u16 vendor;
458
459 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
460 if (rc < 0)
461 return rc;
462
463 /* probe only iTPMS */
464 if (vendor != TPM_VID_INTEL)
465 return 0;
466
467 rc = tpm_tis_send_data(chip, cmd_getticks, len);
468 if (rc == 0)
469 goto out;
470
471 tpm_tis_ready(chip);
472 release_locality(chip, priv->locality, 0);
473
474 rc = tpm_tis_send_data(chip, cmd_getticks, len);
475 if (rc == 0) {
476 dev_info(&chip->dev, "Detected an iTPM.\n");
477 rc = 1;
478 } else
479 rc = -EFAULT;
480
481 out:
482 tpm_tis_ready(chip);
483 release_locality(chip, priv->locality, 0);
484
485 return rc;
486 }
487
tpm_tis_req_canceled(struct tpm_chip * chip,u8 status)488 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
489 {
490 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
491
492 switch (priv->manufacturer_id) {
493 case TPM_VID_WINBOND:
494 return ((status == TPM_STS_VALID) ||
495 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
496 case TPM_VID_STM:
497 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
498 default:
499 return (status == TPM_STS_COMMAND_READY);
500 }
501 }
502
tis_int_handler(int dummy,void * dev_id)503 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
504 {
505 struct tpm_chip *chip = dev_id;
506 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
507 u32 interrupt;
508 int i, rc;
509
510 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
511 if (rc < 0)
512 return IRQ_NONE;
513
514 if (interrupt == 0)
515 return IRQ_NONE;
516
517 priv->irq_tested = true;
518 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
519 wake_up_interruptible(&priv->read_queue);
520 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
521 for (i = 0; i < 5; i++)
522 if (check_locality(chip, i) >= 0)
523 break;
524 if (interrupt &
525 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
526 TPM_INTF_CMD_READY_INT))
527 wake_up_interruptible(&priv->int_queue);
528
529 /* Clear interrupts handled with TPM_EOI */
530 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
531 if (rc < 0)
532 return IRQ_NONE;
533
534 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
535 return IRQ_HANDLED;
536 }
537
tpm_tis_gen_interrupt(struct tpm_chip * chip)538 static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
539 {
540 const char *desc = "attempting to generate an interrupt";
541 u32 cap2;
542 cap_t cap;
543
544 if (chip->flags & TPM_CHIP_FLAG_TPM2)
545 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
546 else
547 return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc);
548 }
549
550 /* Register the IRQ and issue a command that will cause an interrupt. If an
551 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
552 * everything and leave in polling mode. Returns 0 on success.
553 */
tpm_tis_probe_irq_single(struct tpm_chip * chip,u32 intmask,int flags,int irq)554 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
555 int flags, int irq)
556 {
557 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
558 u8 original_int_vec;
559 int rc;
560 u32 int_status;
561
562 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
563 dev_name(&chip->dev), chip) != 0) {
564 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
565 irq);
566 return -1;
567 }
568 priv->irq = irq;
569
570 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
571 &original_int_vec);
572 if (rc < 0)
573 return rc;
574
575 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
576 if (rc < 0)
577 return rc;
578
579 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
580 if (rc < 0)
581 return rc;
582
583 /* Clear all existing */
584 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
585 if (rc < 0)
586 return rc;
587
588 /* Turn on */
589 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
590 intmask | TPM_GLOBAL_INT_ENABLE);
591 if (rc < 0)
592 return rc;
593
594 priv->irq_tested = false;
595
596 /* Generate an interrupt by having the core call through to
597 * tpm_tis_send
598 */
599 rc = tpm_tis_gen_interrupt(chip);
600 if (rc < 0)
601 return rc;
602
603 /* tpm_tis_send will either confirm the interrupt is working or it
604 * will call disable_irq which undoes all of the above.
605 */
606 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
607 rc = tpm_tis_write8(priv, original_int_vec,
608 TPM_INT_VECTOR(priv->locality));
609 if (rc < 0)
610 return rc;
611
612 return 1;
613 }
614
615 return 0;
616 }
617
618 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
619 * do not have ACPI/etc. We typically expect the interrupt to be declared if
620 * present.
621 */
tpm_tis_probe_irq(struct tpm_chip * chip,u32 intmask)622 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
623 {
624 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
625 u8 original_int_vec;
626 int i, rc;
627
628 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
629 &original_int_vec);
630 if (rc < 0)
631 return;
632
633 if (!original_int_vec) {
634 if (IS_ENABLED(CONFIG_X86))
635 for (i = 3; i <= 15; i++)
636 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
637 i))
638 return;
639 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
640 original_int_vec))
641 return;
642 }
643
tpm_tis_remove(struct tpm_chip * chip)644 void tpm_tis_remove(struct tpm_chip *chip)
645 {
646 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
647 u32 reg = TPM_INT_ENABLE(priv->locality);
648 u32 interrupt;
649 int rc;
650
651 rc = tpm_tis_read32(priv, reg, &interrupt);
652 if (rc < 0)
653 interrupt = 0;
654
655 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
656 release_locality(chip, priv->locality, 1);
657 }
658 EXPORT_SYMBOL_GPL(tpm_tis_remove);
659
660 static const struct tpm_class_ops tpm_tis = {
661 .flags = TPM_OPS_AUTO_STARTUP,
662 .status = tpm_tis_status,
663 .recv = tpm_tis_recv,
664 .send = tpm_tis_send,
665 .cancel = tpm_tis_ready,
666 .update_timeouts = tpm_tis_update_timeouts,
667 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
668 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
669 .req_canceled = tpm_tis_req_canceled,
670 };
671
tpm_tis_core_init(struct device * dev,struct tpm_tis_data * priv,int irq,const struct tpm_tis_phy_ops * phy_ops,acpi_handle acpi_dev_handle)672 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
673 const struct tpm_tis_phy_ops *phy_ops,
674 acpi_handle acpi_dev_handle)
675 {
676 u32 vendor, intfcaps, intmask;
677 u8 rid;
678 int rc, probe;
679 struct tpm_chip *chip;
680
681 chip = tpmm_chip_alloc(dev, &tpm_tis);
682 if (IS_ERR(chip))
683 return PTR_ERR(chip);
684
685 #ifdef CONFIG_ACPI
686 chip->acpi_dev_handle = acpi_dev_handle;
687 #endif
688
689 /* Maximum timeouts */
690 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
691 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
692 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
693 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
694 priv->phy_ops = phy_ops;
695 dev_set_drvdata(&chip->dev, priv);
696
697 if (wait_startup(chip, 0) != 0) {
698 rc = -ENODEV;
699 goto out_err;
700 }
701
702 /* Take control of the TPM's interrupt hardware and shut it off */
703 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
704 if (rc < 0)
705 goto out_err;
706
707 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
708 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
709 intmask &= ~TPM_GLOBAL_INT_ENABLE;
710 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
711
712 if (request_locality(chip, 0) != 0) {
713 rc = -ENODEV;
714 goto out_err;
715 }
716
717 rc = tpm2_probe(chip);
718 if (rc)
719 goto out_err;
720
721 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
722 if (rc < 0)
723 goto out_err;
724
725 priv->manufacturer_id = vendor;
726
727 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
728 if (rc < 0)
729 goto out_err;
730
731 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
732 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
733 vendor >> 16, rid);
734
735 if (!(priv->flags & TPM_TIS_ITPM_POSSIBLE)) {
736 probe = probe_itpm(chip);
737 if (probe < 0) {
738 rc = -ENODEV;
739 goto out_err;
740 }
741
742 if (!!probe)
743 priv->flags |= TPM_TIS_ITPM_POSSIBLE;
744 }
745
746 /* Figure out the capabilities */
747 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
748 if (rc < 0)
749 goto out_err;
750
751 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
752 intfcaps);
753 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
754 dev_dbg(dev, "\tBurst Count Static\n");
755 if (intfcaps & TPM_INTF_CMD_READY_INT)
756 dev_dbg(dev, "\tCommand Ready Int Support\n");
757 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
758 dev_dbg(dev, "\tInterrupt Edge Falling\n");
759 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
760 dev_dbg(dev, "\tInterrupt Edge Rising\n");
761 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
762 dev_dbg(dev, "\tInterrupt Level Low\n");
763 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
764 dev_dbg(dev, "\tInterrupt Level High\n");
765 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
766 dev_dbg(dev, "\tLocality Change Int Support\n");
767 if (intfcaps & TPM_INTF_STS_VALID_INT)
768 dev_dbg(dev, "\tSts Valid Int Support\n");
769 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
770 dev_dbg(dev, "\tData Avail Int Support\n");
771
772 /* Very early on issue a command to the TPM in polling mode to make
773 * sure it works. May as well use that command to set the proper
774 * timeouts for the driver.
775 */
776 if (tpm_get_timeouts(chip)) {
777 dev_err(dev, "Could not get TPM timeouts and durations\n");
778 rc = -ENODEV;
779 goto out_err;
780 }
781
782 /* INTERRUPT Setup */
783 init_waitqueue_head(&priv->read_queue);
784 init_waitqueue_head(&priv->int_queue);
785 if (irq != -1) {
786 if (irq) {
787 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
788 irq);
789 if (!(chip->flags & TPM_CHIP_FLAG_IRQ))
790 dev_err(&chip->dev, FW_BUG
791 "TPM interrupt not working, polling instead\n");
792 } else {
793 tpm_tis_probe_irq(chip, intmask);
794 }
795 }
796
797 return tpm_chip_register(chip);
798 out_err:
799 tpm_tis_remove(chip);
800 return rc;
801 }
802 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
803
804 #ifdef CONFIG_PM_SLEEP
tpm_tis_reenable_interrupts(struct tpm_chip * chip)805 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
806 {
807 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
808 u32 intmask;
809 int rc;
810
811 /* reenable interrupts that device may have lost or
812 * BIOS/firmware may have disabled
813 */
814 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
815 if (rc < 0)
816 return;
817
818 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
819 if (rc < 0)
820 return;
821
822 intmask |= TPM_INTF_CMD_READY_INT
823 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
824 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
825
826 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
827 }
828
tpm_tis_resume(struct device * dev)829 int tpm_tis_resume(struct device *dev)
830 {
831 struct tpm_chip *chip = dev_get_drvdata(dev);
832 int ret;
833
834 if (chip->flags & TPM_CHIP_FLAG_IRQ)
835 tpm_tis_reenable_interrupts(chip);
836
837 ret = tpm_pm_resume(dev);
838 if (ret)
839 return ret;
840
841 /* TPM 1.2 requires self-test on resume. This function actually returns
842 * an error code but for unknown reason it isn't handled.
843 */
844 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
845 tpm_do_selftest(chip);
846
847 return 0;
848 }
849 EXPORT_SYMBOL_GPL(tpm_tis_resume);
850 #endif
851
852 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
853 MODULE_DESCRIPTION("TPM Driver");
854 MODULE_VERSION("2.0");
855 MODULE_LICENSE("GPL");
856