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