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