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