xref: /openbmc/linux/drivers/char/tpm/tpm-chip.c (revision 801b27e8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2004 IBM Corporation
4  * Copyright (C) 2014 Intel Corporation
5  *
6  * Authors:
7  * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
8  * Leendert van Doorn <leendert@watson.ibm.com>
9  * Dave Safford <safford@watson.ibm.com>
10  * Reiner Sailer <sailer@watson.ibm.com>
11  * Kylene Hall <kjhall@us.ibm.com>
12  *
13  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
14  *
15  * TPM chip management routines.
16  */
17 
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/mutex.h>
21 #include <linux/spinlock.h>
22 #include <linux/freezer.h>
23 #include <linux/major.h>
24 #include <linux/tpm_eventlog.h>
25 #include <linux/hw_random.h>
26 #include "tpm.h"
27 
28 DEFINE_IDR(dev_nums_idr);
29 static DEFINE_MUTEX(idr_lock);
30 
31 struct class *tpm_class;
32 struct class *tpmrm_class;
33 dev_t tpm_devt;
34 
35 static int tpm_request_locality(struct tpm_chip *chip)
36 {
37 	int rc;
38 
39 	if (!chip->ops->request_locality)
40 		return 0;
41 
42 	rc = chip->ops->request_locality(chip, 0);
43 	if (rc < 0)
44 		return rc;
45 
46 	chip->locality = rc;
47 	return 0;
48 }
49 
50 static void tpm_relinquish_locality(struct tpm_chip *chip)
51 {
52 	int rc;
53 
54 	if (!chip->ops->relinquish_locality)
55 		return;
56 
57 	rc = chip->ops->relinquish_locality(chip, chip->locality);
58 	if (rc)
59 		dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
60 
61 	chip->locality = -1;
62 }
63 
64 static int tpm_cmd_ready(struct tpm_chip *chip)
65 {
66 	if (!chip->ops->cmd_ready)
67 		return 0;
68 
69 	return chip->ops->cmd_ready(chip);
70 }
71 
72 static int tpm_go_idle(struct tpm_chip *chip)
73 {
74 	if (!chip->ops->go_idle)
75 		return 0;
76 
77 	return chip->ops->go_idle(chip);
78 }
79 
80 static void tpm_clk_enable(struct tpm_chip *chip)
81 {
82 	if (chip->ops->clk_enable)
83 		chip->ops->clk_enable(chip, true);
84 }
85 
86 static void tpm_clk_disable(struct tpm_chip *chip)
87 {
88 	if (chip->ops->clk_enable)
89 		chip->ops->clk_enable(chip, false);
90 }
91 
92 /**
93  * tpm_chip_start() - power on the TPM
94  * @chip:	a TPM chip to use
95  *
96  * Return:
97  * * The response length	- OK
98  * * -errno			- A system error
99  */
100 int tpm_chip_start(struct tpm_chip *chip)
101 {
102 	int ret;
103 
104 	tpm_clk_enable(chip);
105 
106 	if (chip->locality == -1) {
107 		ret = tpm_request_locality(chip);
108 		if (ret) {
109 			tpm_clk_disable(chip);
110 			return ret;
111 		}
112 	}
113 
114 	ret = tpm_cmd_ready(chip);
115 	if (ret) {
116 		tpm_relinquish_locality(chip);
117 		tpm_clk_disable(chip);
118 		return ret;
119 	}
120 
121 	return 0;
122 }
123 EXPORT_SYMBOL_GPL(tpm_chip_start);
124 
125 /**
126  * tpm_chip_stop() - power off the TPM
127  * @chip:	a TPM chip to use
128  *
129  * Return:
130  * * The response length	- OK
131  * * -errno			- A system error
132  */
133 void tpm_chip_stop(struct tpm_chip *chip)
134 {
135 	tpm_go_idle(chip);
136 	tpm_relinquish_locality(chip);
137 	tpm_clk_disable(chip);
138 }
139 EXPORT_SYMBOL_GPL(tpm_chip_stop);
140 
141 /**
142  * tpm_try_get_ops() - Get a ref to the tpm_chip
143  * @chip: Chip to ref
144  *
145  * The caller must already have some kind of locking to ensure that chip is
146  * valid. This function will lock the chip so that the ops member can be
147  * accessed safely. The locking prevents tpm_chip_unregister from
148  * completing, so it should not be held for long periods.
149  *
150  * Returns -ERRNO if the chip could not be got.
151  */
152 int tpm_try_get_ops(struct tpm_chip *chip)
153 {
154 	int rc = -EIO;
155 
156 	get_device(&chip->dev);
157 
158 	down_read(&chip->ops_sem);
159 	if (!chip->ops)
160 		goto out_ops;
161 
162 	mutex_lock(&chip->tpm_mutex);
163 	rc = tpm_chip_start(chip);
164 	if (rc)
165 		goto out_lock;
166 
167 	return 0;
168 out_lock:
169 	mutex_unlock(&chip->tpm_mutex);
170 out_ops:
171 	up_read(&chip->ops_sem);
172 	put_device(&chip->dev);
173 	return rc;
174 }
175 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
176 
177 /**
178  * tpm_put_ops() - Release a ref to the tpm_chip
179  * @chip: Chip to put
180  *
181  * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
182  * be kfree'd.
183  */
184 void tpm_put_ops(struct tpm_chip *chip)
185 {
186 	tpm_chip_stop(chip);
187 	mutex_unlock(&chip->tpm_mutex);
188 	up_read(&chip->ops_sem);
189 	put_device(&chip->dev);
190 }
191 EXPORT_SYMBOL_GPL(tpm_put_ops);
192 
193 /**
194  * tpm_default_chip() - find a TPM chip and get a reference to it
195  */
196 struct tpm_chip *tpm_default_chip(void)
197 {
198 	struct tpm_chip *chip, *res = NULL;
199 	int chip_num = 0;
200 	int chip_prev;
201 
202 	mutex_lock(&idr_lock);
203 
204 	do {
205 		chip_prev = chip_num;
206 		chip = idr_get_next(&dev_nums_idr, &chip_num);
207 		if (chip) {
208 			get_device(&chip->dev);
209 			res = chip;
210 			break;
211 		}
212 	} while (chip_prev != chip_num);
213 
214 	mutex_unlock(&idr_lock);
215 
216 	return res;
217 }
218 EXPORT_SYMBOL_GPL(tpm_default_chip);
219 
220 /**
221  * tpm_find_get_ops() - find and reserve a TPM chip
222  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
223  *
224  * Finds a TPM chip and reserves its class device and operations. The chip must
225  * be released with tpm_put_ops() after use.
226  * This function is for internal use only. It supports existing TPM callers
227  * by accepting NULL, but those callers should be converted to pass in a chip
228  * directly.
229  *
230  * Return:
231  * A reserved &struct tpm_chip instance.
232  * %NULL if a chip is not found.
233  * %NULL if the chip is not available.
234  */
235 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
236 {
237 	int rc;
238 
239 	if (chip) {
240 		if (!tpm_try_get_ops(chip))
241 			return chip;
242 		return NULL;
243 	}
244 
245 	chip = tpm_default_chip();
246 	if (!chip)
247 		return NULL;
248 	rc = tpm_try_get_ops(chip);
249 	/* release additional reference we got from tpm_default_chip() */
250 	put_device(&chip->dev);
251 	if (rc)
252 		return NULL;
253 	return chip;
254 }
255 
256 /**
257  * tpm_dev_release() - free chip memory and the device number
258  * @dev: the character device for the TPM chip
259  *
260  * This is used as the release function for the character device.
261  */
262 static void tpm_dev_release(struct device *dev)
263 {
264 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
265 
266 	mutex_lock(&idr_lock);
267 	idr_remove(&dev_nums_idr, chip->dev_num);
268 	mutex_unlock(&idr_lock);
269 
270 	kfree(chip->work_space.context_buf);
271 	kfree(chip->work_space.session_buf);
272 	kfree(chip->allocated_banks);
273 	kfree(chip);
274 }
275 
276 /**
277  * tpm_class_shutdown() - prepare the TPM device for loss of power.
278  * @dev: device to which the chip is associated.
279  *
280  * Issues a TPM2_Shutdown command prior to loss of power, as required by the
281  * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code.
282  *
283  * Return: always 0 (i.e. success)
284  */
285 int tpm_class_shutdown(struct device *dev)
286 {
287 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
288 
289 	down_write(&chip->ops_sem);
290 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
291 		if (!tpm_chip_start(chip)) {
292 			tpm2_shutdown(chip, TPM2_SU_CLEAR);
293 			tpm_chip_stop(chip);
294 		}
295 	}
296 	chip->ops = NULL;
297 	up_write(&chip->ops_sem);
298 
299 	return 0;
300 }
301 
302 /**
303  * tpm_chip_alloc() - allocate a new struct tpm_chip instance
304  * @pdev: device to which the chip is associated
305  *        At this point pdev mst be initialized, but does not have to
306  *        be registered
307  * @ops: struct tpm_class_ops instance
308  *
309  * Allocates a new struct tpm_chip instance and assigns a free
310  * device number for it. Must be paired with put_device(&chip->dev).
311  */
312 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
313 				const struct tpm_class_ops *ops)
314 {
315 	struct tpm_chip *chip;
316 	int rc;
317 
318 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
319 	if (chip == NULL)
320 		return ERR_PTR(-ENOMEM);
321 
322 	mutex_init(&chip->tpm_mutex);
323 	init_rwsem(&chip->ops_sem);
324 
325 	chip->ops = ops;
326 
327 	mutex_lock(&idr_lock);
328 	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
329 	mutex_unlock(&idr_lock);
330 	if (rc < 0) {
331 		dev_err(pdev, "No available tpm device numbers\n");
332 		kfree(chip);
333 		return ERR_PTR(rc);
334 	}
335 	chip->dev_num = rc;
336 
337 	device_initialize(&chip->dev);
338 
339 	chip->dev.class = tpm_class;
340 	chip->dev.release = tpm_dev_release;
341 	chip->dev.parent = pdev;
342 	chip->dev.groups = chip->groups;
343 
344 	if (chip->dev_num == 0)
345 		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
346 	else
347 		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
348 
349 	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
350 	if (rc)
351 		goto out;
352 
353 	if (!pdev)
354 		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
355 
356 	cdev_init(&chip->cdev, &tpm_fops);
357 	chip->cdev.owner = THIS_MODULE;
358 
359 	rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
360 	if (rc) {
361 		rc = -ENOMEM;
362 		goto out;
363 	}
364 
365 	chip->locality = -1;
366 	return chip;
367 
368 out:
369 	put_device(&chip->dev);
370 	return ERR_PTR(rc);
371 }
372 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
373 
374 static void tpm_put_device(void *dev)
375 {
376 	put_device(dev);
377 }
378 
379 /**
380  * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
381  * @pdev: parent device to which the chip is associated
382  * @ops: struct tpm_class_ops instance
383  *
384  * Same as tpm_chip_alloc except devm is used to do the put_device
385  */
386 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
387 				 const struct tpm_class_ops *ops)
388 {
389 	struct tpm_chip *chip;
390 	int rc;
391 
392 	chip = tpm_chip_alloc(pdev, ops);
393 	if (IS_ERR(chip))
394 		return chip;
395 
396 	rc = devm_add_action_or_reset(pdev,
397 				      tpm_put_device,
398 				      &chip->dev);
399 	if (rc)
400 		return ERR_PTR(rc);
401 
402 	dev_set_drvdata(pdev, chip);
403 
404 	return chip;
405 }
406 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
407 
408 static int tpm_add_char_device(struct tpm_chip *chip)
409 {
410 	int rc;
411 
412 	rc = cdev_device_add(&chip->cdev, &chip->dev);
413 	if (rc) {
414 		dev_err(&chip->dev,
415 			"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
416 			dev_name(&chip->dev), MAJOR(chip->dev.devt),
417 			MINOR(chip->dev.devt), rc);
418 		return rc;
419 	}
420 
421 	if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
422 		rc = tpm_devs_add(chip);
423 		if (rc)
424 			goto err_del_cdev;
425 	}
426 
427 	/* Make the chip available. */
428 	mutex_lock(&idr_lock);
429 	idr_replace(&dev_nums_idr, chip, chip->dev_num);
430 	mutex_unlock(&idr_lock);
431 
432 	return 0;
433 
434 err_del_cdev:
435 	cdev_device_del(&chip->cdev, &chip->dev);
436 	return rc;
437 }
438 
439 static void tpm_del_char_device(struct tpm_chip *chip)
440 {
441 	cdev_device_del(&chip->cdev, &chip->dev);
442 
443 	/* Make the chip unavailable. */
444 	mutex_lock(&idr_lock);
445 	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
446 	mutex_unlock(&idr_lock);
447 
448 	/* Make the driver uncallable. */
449 	down_write(&chip->ops_sem);
450 
451 	/*
452 	 * Check if chip->ops is still valid: In case that the controller
453 	 * drivers shutdown handler unregisters the controller in its
454 	 * shutdown handler we are called twice and chip->ops to NULL.
455 	 */
456 	if (chip->ops) {
457 		if (chip->flags & TPM_CHIP_FLAG_TPM2) {
458 			if (!tpm_chip_start(chip)) {
459 				tpm2_shutdown(chip, TPM2_SU_CLEAR);
460 				tpm_chip_stop(chip);
461 			}
462 		}
463 		chip->ops = NULL;
464 	}
465 	up_write(&chip->ops_sem);
466 }
467 
468 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
469 {
470 	struct attribute **i;
471 
472 	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) ||
473 	    tpm_is_firmware_upgrade(chip))
474 		return;
475 
476 	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
477 
478 	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
479 		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
480 }
481 
482 /* For compatibility with legacy sysfs paths we provide symlinks from the
483  * parent dev directory to selected names within the tpm chip directory. Old
484  * kernel versions created these files directly under the parent.
485  */
486 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
487 {
488 	struct attribute **i;
489 	int rc;
490 
491 	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) ||
492 		tpm_is_firmware_upgrade(chip))
493 		return 0;
494 
495 	rc = compat_only_sysfs_link_entry_to_kobj(
496 		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL);
497 	if (rc && rc != -ENOENT)
498 		return rc;
499 
500 	/* All the names from tpm-sysfs */
501 	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
502 		rc = compat_only_sysfs_link_entry_to_kobj(
503 		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL);
504 		if (rc) {
505 			tpm_del_legacy_sysfs(chip);
506 			return rc;
507 		}
508 	}
509 
510 	return 0;
511 }
512 
513 /*
514  * Some AMD fTPM versions may cause stutter
515  * https://www.amd.com/en/support/kb/faq/pa-410
516  *
517  * Fixes are available in two series of fTPM firmware:
518  * 6.x.y.z series: 6.0.18.6 +
519  * 3.x.y.z series: 3.57.y.5 +
520  */
521 #ifdef CONFIG_X86
522 static bool tpm_amd_is_rng_defective(struct tpm_chip *chip)
523 {
524 	u32 val1, val2;
525 	u64 version;
526 	int ret;
527 
528 	if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
529 		return false;
530 
531 	ret = tpm_request_locality(chip);
532 	if (ret)
533 		return false;
534 
535 	ret = tpm2_get_tpm_pt(chip, TPM2_PT_MANUFACTURER, &val1, NULL);
536 	if (ret)
537 		goto release;
538 	if (val1 != 0x414D4400U /* AMD */) {
539 		ret = -ENODEV;
540 		goto release;
541 	}
542 	ret = tpm2_get_tpm_pt(chip, TPM2_PT_FIRMWARE_VERSION_1, &val1, NULL);
543 	if (ret)
544 		goto release;
545 	ret = tpm2_get_tpm_pt(chip, TPM2_PT_FIRMWARE_VERSION_2, &val2, NULL);
546 
547 release:
548 	tpm_relinquish_locality(chip);
549 
550 	if (ret)
551 		return false;
552 
553 	version = ((u64)val1 << 32) | val2;
554 	if ((version >> 48) == 6) {
555 		if (version >= 0x0006000000180006ULL)
556 			return false;
557 	} else if ((version >> 48) == 3) {
558 		if (version >= 0x0003005700000005ULL)
559 			return false;
560 	} else {
561 		return false;
562 	}
563 
564 	dev_warn(&chip->dev,
565 		 "AMD fTPM version 0x%llx causes system stutter; hwrng disabled\n",
566 		 version);
567 
568 	return true;
569 }
570 #else
571 static inline bool tpm_amd_is_rng_defective(struct tpm_chip *chip)
572 {
573 	return false;
574 }
575 #endif /* CONFIG_X86 */
576 
577 static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
578 {
579 	struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
580 
581 	/* Give back zero bytes, as TPM chip has not yet fully resumed: */
582 	if (chip->flags & TPM_CHIP_FLAG_SUSPENDED)
583 		return 0;
584 
585 	return tpm_get_random(chip, data, max);
586 }
587 
588 static int tpm_add_hwrng(struct tpm_chip *chip)
589 {
590 	if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM) || tpm_is_firmware_upgrade(chip) ||
591 	    tpm_amd_is_rng_defective(chip))
592 		return 0;
593 
594 	snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
595 		 "tpm-rng-%d", chip->dev_num);
596 	chip->hwrng.name = chip->hwrng_name;
597 	chip->hwrng.read = tpm_hwrng_read;
598 	return hwrng_register(&chip->hwrng);
599 }
600 
601 static int tpm_get_pcr_allocation(struct tpm_chip *chip)
602 {
603 	int rc;
604 
605 	if (tpm_is_firmware_upgrade(chip))
606 		return 0;
607 
608 	rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
609 	     tpm2_get_pcr_allocation(chip) :
610 	     tpm1_get_pcr_allocation(chip);
611 
612 	if (rc > 0)
613 		return -ENODEV;
614 
615 	return rc;
616 }
617 
618 /*
619  * tpm_chip_bootstrap() - Boostrap TPM chip after power on
620  * @chip: TPM chip to use.
621  *
622  * Initialize TPM chip after power on. This a one-shot function: subsequent
623  * calls will have no effect.
624  */
625 int tpm_chip_bootstrap(struct tpm_chip *chip)
626 {
627 	int rc;
628 
629 	if (chip->flags & TPM_CHIP_FLAG_BOOTSTRAPPED)
630 		return 0;
631 
632 	rc = tpm_chip_start(chip);
633 	if (rc)
634 		return rc;
635 
636 	rc = tpm_auto_startup(chip);
637 	if (rc)
638 		goto stop;
639 
640 	rc = tpm_get_pcr_allocation(chip);
641 stop:
642 	tpm_chip_stop(chip);
643 
644 	/*
645 	 * Unconditionally set, as driver initialization should cease, when the
646 	 * boostrapping process fails.
647 	 */
648 	chip->flags |= TPM_CHIP_FLAG_BOOTSTRAPPED;
649 
650 	return rc;
651 }
652 EXPORT_SYMBOL_GPL(tpm_chip_bootstrap);
653 
654 /*
655  * tpm_chip_register() - create a character device for the TPM chip
656  * @chip: TPM chip to use.
657  *
658  * Creates a character device for the TPM chip and adds sysfs attributes for
659  * the device. As the last step this function adds the chip to the list of TPM
660  * chips available for in-kernel use.
661  *
662  * This function should be only called after the chip initialization is
663  * complete.
664  */
665 int tpm_chip_register(struct tpm_chip *chip)
666 {
667 	int rc;
668 
669 	rc = tpm_chip_bootstrap(chip);
670 	if (rc)
671 		return rc;
672 
673 	tpm_sysfs_add_device(chip);
674 
675 	tpm_bios_log_setup(chip);
676 
677 	tpm_add_ppi(chip);
678 
679 	rc = tpm_add_hwrng(chip);
680 	if (rc)
681 		goto out_ppi;
682 
683 	rc = tpm_add_char_device(chip);
684 	if (rc)
685 		goto out_hwrng;
686 
687 	rc = tpm_add_legacy_sysfs(chip);
688 	if (rc) {
689 		tpm_chip_unregister(chip);
690 		return rc;
691 	}
692 
693 	return 0;
694 
695 out_hwrng:
696 	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
697 		hwrng_unregister(&chip->hwrng);
698 out_ppi:
699 	tpm_bios_log_teardown(chip);
700 
701 	return rc;
702 }
703 EXPORT_SYMBOL_GPL(tpm_chip_register);
704 
705 /*
706  * tpm_chip_unregister() - release the TPM driver
707  * @chip: TPM chip to use.
708  *
709  * Takes the chip first away from the list of available TPM chips and then
710  * cleans up all the resources reserved by tpm_chip_register().
711  *
712  * Once this function returns the driver call backs in 'op's will not be
713  * running and will no longer start.
714  *
715  * NOTE: This function should be only called before deinitializing chip
716  * resources.
717  */
718 void tpm_chip_unregister(struct tpm_chip *chip)
719 {
720 	tpm_del_legacy_sysfs(chip);
721 	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip) &&
722 	    !tpm_amd_is_rng_defective(chip))
723 		hwrng_unregister(&chip->hwrng);
724 	tpm_bios_log_teardown(chip);
725 	if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
726 		tpm_devs_remove(chip);
727 	tpm_del_char_device(chip);
728 }
729 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
730