1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 IBM Corporation
4 * Copyright (C) 2014 Intel Corporation
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>
13 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
15 * TPM chip management routines.
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>
28 DEFINE_IDR(dev_nums_idr);
29 static DEFINE_MUTEX(idr_lock);
31 struct class *tpm_class;
32 struct class *tpmrm_class;
35 static int tpm_request_locality(struct tpm_chip *chip)
39 if (!chip->ops->request_locality)
42 rc = chip->ops->request_locality(chip, 0);
50 static void tpm_relinquish_locality(struct tpm_chip *chip)
54 if (!chip->ops->relinquish_locality)
57 rc = chip->ops->relinquish_locality(chip, chip->locality);
59 dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
64 static int tpm_cmd_ready(struct tpm_chip *chip)
66 if (!chip->ops->cmd_ready)
69 return chip->ops->cmd_ready(chip);
72 static int tpm_go_idle(struct tpm_chip *chip)
74 if (!chip->ops->go_idle)
77 return chip->ops->go_idle(chip);
80 static void tpm_clk_enable(struct tpm_chip *chip)
82 if (chip->ops->clk_enable)
83 chip->ops->clk_enable(chip, true);
86 static void tpm_clk_disable(struct tpm_chip *chip)
88 if (chip->ops->clk_enable)
89 chip->ops->clk_enable(chip, false);
93 * tpm_chip_start() - power on the TPM
94 * @chip: a TPM chip to use
97 * * The response length - OK
98 * * -errno - A system error
100 int tpm_chip_start(struct tpm_chip *chip)
104 tpm_clk_enable(chip);
106 if (chip->locality == -1) {
107 ret = tpm_request_locality(chip);
109 tpm_clk_disable(chip);
114 ret = tpm_cmd_ready(chip);
116 tpm_relinquish_locality(chip);
117 tpm_clk_disable(chip);
123 EXPORT_SYMBOL_GPL(tpm_chip_start);
126 * tpm_chip_stop() - power off the TPM
127 * @chip: a TPM chip to use
130 * * The response length - OK
131 * * -errno - A system error
133 void tpm_chip_stop(struct tpm_chip *chip)
136 tpm_relinquish_locality(chip);
137 tpm_clk_disable(chip);
139 EXPORT_SYMBOL_GPL(tpm_chip_stop);
142 * tpm_try_get_ops() - Get a ref to the tpm_chip
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.
150 * Returns -ERRNO if the chip could not be got.
152 int tpm_try_get_ops(struct tpm_chip *chip)
156 get_device(&chip->dev);
158 down_read(&chip->ops_sem);
162 mutex_lock(&chip->tpm_mutex);
163 rc = tpm_chip_start(chip);
169 mutex_unlock(&chip->tpm_mutex);
171 up_read(&chip->ops_sem);
172 put_device(&chip->dev);
175 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
178 * tpm_put_ops() - Release a ref to the tpm_chip
181 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
184 void tpm_put_ops(struct tpm_chip *chip)
187 mutex_unlock(&chip->tpm_mutex);
188 up_read(&chip->ops_sem);
189 put_device(&chip->dev);
191 EXPORT_SYMBOL_GPL(tpm_put_ops);
194 * tpm_default_chip() - find a TPM chip and get a reference to it
196 struct tpm_chip *tpm_default_chip(void)
198 struct tpm_chip *chip, *res = NULL;
202 mutex_lock(&idr_lock);
205 chip_prev = chip_num;
206 chip = idr_get_next(&dev_nums_idr, &chip_num);
208 get_device(&chip->dev);
212 } while (chip_prev != chip_num);
214 mutex_unlock(&idr_lock);
218 EXPORT_SYMBOL_GPL(tpm_default_chip);
221 * tpm_find_get_ops() - find and reserve a TPM chip
222 * @chip: a &struct tpm_chip instance, %NULL for the default chip
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
231 * A reserved &struct tpm_chip instance.
232 * %NULL if a chip is not found.
233 * %NULL if the chip is not available.
235 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
240 if (!tpm_try_get_ops(chip))
245 chip = tpm_default_chip();
248 rc = tpm_try_get_ops(chip);
249 /* release additional reference we got from tpm_default_chip() */
250 put_device(&chip->dev);
257 * tpm_dev_release() - free chip memory and the device number
258 * @dev: the character device for the TPM chip
260 * This is used as the release function for the character device.
262 static void tpm_dev_release(struct device *dev)
264 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
266 mutex_lock(&idr_lock);
267 idr_remove(&dev_nums_idr, chip->dev_num);
268 mutex_unlock(&idr_lock);
270 kfree(chip->log.bios_event_log);
271 kfree(chip->work_space.context_buf);
272 kfree(chip->work_space.session_buf);
273 kfree(chip->allocated_banks);
277 static void tpm_devs_release(struct device *dev)
279 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
281 /* release the master device reference */
282 put_device(&chip->dev);
286 * tpm_class_shutdown() - prepare the TPM device for loss of power.
287 * @dev: device to which the chip is associated.
289 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
291 * Then, calls bus- and device- specific shutdown code.
293 * XXX: This codepath relies on the fact that sysfs is not enabled for
294 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
295 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
297 static int tpm_class_shutdown(struct device *dev)
299 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
301 down_write(&chip->ops_sem);
302 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
303 if (!tpm_chip_start(chip)) {
304 tpm2_shutdown(chip, TPM2_SU_CLEAR);
309 up_write(&chip->ops_sem);
315 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
316 * @pdev: device to which the chip is associated
317 * At this point pdev mst be initialized, but does not have to
319 * @ops: struct tpm_class_ops instance
321 * Allocates a new struct tpm_chip instance and assigns a free
322 * device number for it. Must be paired with put_device(&chip->dev).
324 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
325 const struct tpm_class_ops *ops)
327 struct tpm_chip *chip;
330 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
332 return ERR_PTR(-ENOMEM);
334 mutex_init(&chip->tpm_mutex);
335 init_rwsem(&chip->ops_sem);
339 mutex_lock(&idr_lock);
340 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
341 mutex_unlock(&idr_lock);
343 dev_err(pdev, "No available tpm device numbers\n");
349 device_initialize(&chip->dev);
350 device_initialize(&chip->devs);
352 chip->dev.class = tpm_class;
353 chip->dev.class->shutdown_pre = tpm_class_shutdown;
354 chip->dev.release = tpm_dev_release;
355 chip->dev.parent = pdev;
356 chip->dev.groups = chip->groups;
358 chip->devs.parent = pdev;
359 chip->devs.class = tpmrm_class;
360 chip->devs.release = tpm_devs_release;
361 /* get extra reference on main device to hold on
362 * behalf of devs. This holds the chip structure
363 * while cdevs is in use. The corresponding put
364 * is in the tpm_devs_release (TPM2 only)
366 if (chip->flags & TPM_CHIP_FLAG_TPM2)
367 get_device(&chip->dev);
369 if (chip->dev_num == 0)
370 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
372 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
375 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
377 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
380 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
385 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
387 cdev_init(&chip->cdev, &tpm_fops);
388 cdev_init(&chip->cdevs, &tpmrm_fops);
389 chip->cdev.owner = THIS_MODULE;
390 chip->cdevs.owner = THIS_MODULE;
392 chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
393 if (!chip->work_space.context_buf) {
397 chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
398 if (!chip->work_space.session_buf) {
407 put_device(&chip->devs);
408 put_device(&chip->dev);
411 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
414 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
415 * @pdev: parent device to which the chip is associated
416 * @ops: struct tpm_class_ops instance
418 * Same as tpm_chip_alloc except devm is used to do the put_device
420 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
421 const struct tpm_class_ops *ops)
423 struct tpm_chip *chip;
426 chip = tpm_chip_alloc(pdev, ops);
430 rc = devm_add_action_or_reset(pdev,
431 (void (*)(void *)) put_device,
436 dev_set_drvdata(pdev, chip);
440 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
442 static int tpm_add_char_device(struct tpm_chip *chip)
446 rc = cdev_device_add(&chip->cdev, &chip->dev);
449 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
450 dev_name(&chip->dev), MAJOR(chip->dev.devt),
451 MINOR(chip->dev.devt), rc);
455 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
456 rc = cdev_device_add(&chip->cdevs, &chip->devs);
459 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
460 dev_name(&chip->devs), MAJOR(chip->devs.devt),
461 MINOR(chip->devs.devt), rc);
466 /* Make the chip available. */
467 mutex_lock(&idr_lock);
468 idr_replace(&dev_nums_idr, chip, chip->dev_num);
469 mutex_unlock(&idr_lock);
474 static void tpm_del_char_device(struct tpm_chip *chip)
476 cdev_device_del(&chip->cdev, &chip->dev);
478 /* Make the chip unavailable. */
479 mutex_lock(&idr_lock);
480 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
481 mutex_unlock(&idr_lock);
483 /* Make the driver uncallable. */
484 down_write(&chip->ops_sem);
485 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
486 if (!tpm_chip_start(chip)) {
487 tpm2_shutdown(chip, TPM2_SU_CLEAR);
492 up_write(&chip->ops_sem);
495 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
497 struct attribute **i;
499 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
502 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
504 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
505 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
508 /* For compatibility with legacy sysfs paths we provide symlinks from the
509 * parent dev directory to selected names within the tpm chip directory. Old
510 * kernel versions created these files directly under the parent.
512 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
514 struct attribute **i;
517 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
520 rc = __compat_only_sysfs_link_entry_to_kobj(
521 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
522 if (rc && rc != -ENOENT)
525 /* All the names from tpm-sysfs */
526 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
527 rc = __compat_only_sysfs_link_entry_to_kobj(
528 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
530 tpm_del_legacy_sysfs(chip);
538 static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
540 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
542 return tpm_get_random(chip, data, max);
545 static int tpm_add_hwrng(struct tpm_chip *chip)
547 if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
550 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
551 "tpm-rng-%d", chip->dev_num);
552 chip->hwrng.name = chip->hwrng_name;
553 chip->hwrng.read = tpm_hwrng_read;
554 return hwrng_register(&chip->hwrng);
557 static int tpm_get_pcr_allocation(struct tpm_chip *chip)
561 rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
562 tpm2_get_pcr_allocation(chip) :
563 tpm1_get_pcr_allocation(chip);
572 * tpm_chip_register() - create a character device for the TPM chip
573 * @chip: TPM chip to use.
575 * Creates a character device for the TPM chip and adds sysfs attributes for
576 * the device. As the last step this function adds the chip to the list of TPM
577 * chips available for in-kernel use.
579 * This function should be only called after the chip initialization is
582 int tpm_chip_register(struct tpm_chip *chip)
586 rc = tpm_chip_start(chip);
589 rc = tpm_auto_startup(chip);
595 rc = tpm_get_pcr_allocation(chip);
600 tpm_sysfs_add_device(chip);
602 rc = tpm_bios_log_setup(chip);
603 if (rc != 0 && rc != -ENODEV)
608 rc = tpm_add_hwrng(chip);
612 rc = tpm_add_char_device(chip);
616 rc = tpm_add_legacy_sysfs(chip);
618 tpm_chip_unregister(chip);
625 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
626 hwrng_unregister(&chip->hwrng);
628 tpm_bios_log_teardown(chip);
632 EXPORT_SYMBOL_GPL(tpm_chip_register);
635 * tpm_chip_unregister() - release the TPM driver
636 * @chip: TPM chip to use.
638 * Takes the chip first away from the list of available TPM chips and then
639 * cleans up all the resources reserved by tpm_chip_register().
641 * Once this function returns the driver call backs in 'op's will not be
642 * running and will no longer start.
644 * NOTE: This function should be only called before deinitializing chip
647 void tpm_chip_unregister(struct tpm_chip *chip)
649 tpm_del_legacy_sysfs(chip);
650 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
651 hwrng_unregister(&chip->hwrng);
652 tpm_bios_log_teardown(chip);
653 if (chip->flags & TPM_CHIP_FLAG_TPM2)
654 cdev_device_del(&chip->cdevs, &chip->devs);
655 tpm_del_char_device(chip);
657 EXPORT_SYMBOL_GPL(tpm_chip_unregister);