1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 IBM Corporation
4 * Copyright (C) 2014 Intel Corporation
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);
81 * tpm_chip_start() - power on the TPM
82 * @chip: a TPM chip to use
85 * * The response length - OK
86 * * -errno - A system error
88 int tpm_chip_start(struct tpm_chip *chip)
92 if (chip->ops->clk_enable)
93 chip->ops->clk_enable(chip, true);
95 if (chip->locality == -1) {
96 ret = tpm_request_locality(chip);
98 chip->ops->clk_enable(chip, false);
103 ret = tpm_cmd_ready(chip);
105 tpm_relinquish_locality(chip);
106 if (chip->ops->clk_enable)
107 chip->ops->clk_enable(chip, false);
113 EXPORT_SYMBOL_GPL(tpm_chip_start);
116 * tpm_chip_stop() - power off the TPM
117 * @chip: a TPM chip to use
120 * * The response length - OK
121 * * -errno - A system error
123 void tpm_chip_stop(struct tpm_chip *chip)
126 tpm_relinquish_locality(chip);
127 if (chip->ops->clk_enable)
128 chip->ops->clk_enable(chip, false);
130 EXPORT_SYMBOL_GPL(tpm_chip_stop);
133 * tpm_try_get_ops() - Get a ref to the tpm_chip
136 * The caller must already have some kind of locking to ensure that chip is
137 * valid. This function will lock the chip so that the ops member can be
138 * accessed safely. The locking prevents tpm_chip_unregister from
139 * completing, so it should not be held for long periods.
141 * Returns -ERRNO if the chip could not be got.
143 int tpm_try_get_ops(struct tpm_chip *chip)
147 get_device(&chip->dev);
149 down_read(&chip->ops_sem);
153 mutex_lock(&chip->tpm_mutex);
154 rc = tpm_chip_start(chip);
160 mutex_unlock(&chip->tpm_mutex);
162 up_read(&chip->ops_sem);
163 put_device(&chip->dev);
166 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
169 * tpm_put_ops() - Release a ref to the tpm_chip
172 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
175 void tpm_put_ops(struct tpm_chip *chip)
178 mutex_unlock(&chip->tpm_mutex);
179 up_read(&chip->ops_sem);
180 put_device(&chip->dev);
182 EXPORT_SYMBOL_GPL(tpm_put_ops);
185 * tpm_default_chip() - find a TPM chip and get a reference to it
187 struct tpm_chip *tpm_default_chip(void)
189 struct tpm_chip *chip, *res = NULL;
193 mutex_lock(&idr_lock);
196 chip_prev = chip_num;
197 chip = idr_get_next(&dev_nums_idr, &chip_num);
199 get_device(&chip->dev);
203 } while (chip_prev != chip_num);
205 mutex_unlock(&idr_lock);
209 EXPORT_SYMBOL_GPL(tpm_default_chip);
212 * tpm_find_get_ops() - find and reserve a TPM chip
213 * @chip: a &struct tpm_chip instance, %NULL for the default chip
215 * Finds a TPM chip and reserves its class device and operations. The chip must
216 * be released with tpm_put_ops() after use.
217 * This function is for internal use only. It supports existing TPM callers
218 * by accepting NULL, but those callers should be converted to pass in a chip
222 * A reserved &struct tpm_chip instance.
223 * %NULL if a chip is not found.
224 * %NULL if the chip is not available.
226 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
231 if (!tpm_try_get_ops(chip))
236 chip = tpm_default_chip();
239 rc = tpm_try_get_ops(chip);
240 /* release additional reference we got from tpm_default_chip() */
241 put_device(&chip->dev);
248 * tpm_dev_release() - free chip memory and the device number
249 * @dev: the character device for the TPM chip
251 * This is used as the release function for the character device.
253 static void tpm_dev_release(struct device *dev)
255 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
257 mutex_lock(&idr_lock);
258 idr_remove(&dev_nums_idr, chip->dev_num);
259 mutex_unlock(&idr_lock);
261 kfree(chip->log.bios_event_log);
262 kfree(chip->work_space.context_buf);
263 kfree(chip->work_space.session_buf);
264 kfree(chip->allocated_banks);
268 static void tpm_devs_release(struct device *dev)
270 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
272 /* release the master device reference */
273 put_device(&chip->dev);
277 * tpm_class_shutdown() - prepare the TPM device for loss of power.
278 * @dev: device to which the chip is associated.
280 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
282 * Then, calls bus- and device- specific shutdown code.
284 * XXX: This codepath relies on the fact that sysfs is not enabled for
285 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
286 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
288 static int tpm_class_shutdown(struct device *dev)
290 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
292 down_write(&chip->ops_sem);
293 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
294 if (!tpm_chip_start(chip)) {
295 tpm2_shutdown(chip, TPM2_SU_CLEAR);
300 up_write(&chip->ops_sem);
306 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
307 * @pdev: device to which the chip is associated
308 * At this point pdev mst be initialized, but does not have to
310 * @ops: struct tpm_class_ops instance
312 * Allocates a new struct tpm_chip instance and assigns a free
313 * device number for it. Must be paired with put_device(&chip->dev).
315 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
316 const struct tpm_class_ops *ops)
318 struct tpm_chip *chip;
321 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
323 return ERR_PTR(-ENOMEM);
325 mutex_init(&chip->tpm_mutex);
326 init_rwsem(&chip->ops_sem);
330 mutex_lock(&idr_lock);
331 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
332 mutex_unlock(&idr_lock);
334 dev_err(pdev, "No available tpm device numbers\n");
340 device_initialize(&chip->dev);
341 device_initialize(&chip->devs);
343 chip->dev.class = tpm_class;
344 chip->dev.class->shutdown_pre = tpm_class_shutdown;
345 chip->dev.release = tpm_dev_release;
346 chip->dev.parent = pdev;
347 chip->dev.groups = chip->groups;
349 chip->devs.parent = pdev;
350 chip->devs.class = tpmrm_class;
351 chip->devs.release = tpm_devs_release;
352 /* get extra reference on main device to hold on
353 * behalf of devs. This holds the chip structure
354 * while cdevs is in use. The corresponding put
355 * is in the tpm_devs_release (TPM2 only)
357 if (chip->flags & TPM_CHIP_FLAG_TPM2)
358 get_device(&chip->dev);
360 if (chip->dev_num == 0)
361 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
363 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
366 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
368 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
371 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
376 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
378 cdev_init(&chip->cdev, &tpm_fops);
379 cdev_init(&chip->cdevs, &tpmrm_fops);
380 chip->cdev.owner = THIS_MODULE;
381 chip->cdevs.owner = THIS_MODULE;
383 chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
384 if (!chip->work_space.context_buf) {
388 chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
389 if (!chip->work_space.session_buf) {
398 put_device(&chip->devs);
399 put_device(&chip->dev);
402 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
405 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
406 * @pdev: parent device to which the chip is associated
407 * @ops: struct tpm_class_ops instance
409 * Same as tpm_chip_alloc except devm is used to do the put_device
411 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
412 const struct tpm_class_ops *ops)
414 struct tpm_chip *chip;
417 chip = tpm_chip_alloc(pdev, ops);
421 rc = devm_add_action_or_reset(pdev,
422 (void (*)(void *)) put_device,
427 dev_set_drvdata(pdev, chip);
431 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
433 static int tpm_add_char_device(struct tpm_chip *chip)
437 rc = cdev_device_add(&chip->cdev, &chip->dev);
440 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
441 dev_name(&chip->dev), MAJOR(chip->dev.devt),
442 MINOR(chip->dev.devt), rc);
446 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
447 rc = cdev_device_add(&chip->cdevs, &chip->devs);
450 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
451 dev_name(&chip->devs), MAJOR(chip->devs.devt),
452 MINOR(chip->devs.devt), rc);
457 /* Make the chip available. */
458 mutex_lock(&idr_lock);
459 idr_replace(&dev_nums_idr, chip, chip->dev_num);
460 mutex_unlock(&idr_lock);
465 static void tpm_del_char_device(struct tpm_chip *chip)
467 cdev_device_del(&chip->cdev, &chip->dev);
469 /* Make the chip unavailable. */
470 mutex_lock(&idr_lock);
471 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
472 mutex_unlock(&idr_lock);
474 /* Make the driver uncallable. */
475 down_write(&chip->ops_sem);
476 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
477 if (!tpm_chip_start(chip)) {
478 tpm2_shutdown(chip, TPM2_SU_CLEAR);
483 up_write(&chip->ops_sem);
486 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
488 struct attribute **i;
490 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
493 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
495 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
496 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
499 /* For compatibility with legacy sysfs paths we provide symlinks from the
500 * parent dev directory to selected names within the tpm chip directory. Old
501 * kernel versions created these files directly under the parent.
503 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
505 struct attribute **i;
508 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
511 rc = __compat_only_sysfs_link_entry_to_kobj(
512 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
513 if (rc && rc != -ENOENT)
516 /* All the names from tpm-sysfs */
517 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
518 rc = __compat_only_sysfs_link_entry_to_kobj(
519 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
521 tpm_del_legacy_sysfs(chip);
529 static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
531 struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
533 return tpm_get_random(chip, data, max);
536 static int tpm_add_hwrng(struct tpm_chip *chip)
538 if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
541 snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
542 "tpm-rng-%d", chip->dev_num);
543 chip->hwrng.name = chip->hwrng_name;
544 chip->hwrng.read = tpm_hwrng_read;
545 return hwrng_register(&chip->hwrng);
549 * tpm_chip_register() - create a character device for the TPM chip
550 * @chip: TPM chip to use.
552 * Creates a character device for the TPM chip and adds sysfs attributes for
553 * the device. As the last step this function adds the chip to the list of TPM
554 * chips available for in-kernel use.
556 * This function should be only called after the chip initialization is
559 int tpm_chip_register(struct tpm_chip *chip)
563 rc = tpm_chip_start(chip);
566 rc = tpm_auto_startup(chip);
571 tpm_sysfs_add_device(chip);
573 rc = tpm_bios_log_setup(chip);
574 if (rc != 0 && rc != -ENODEV)
579 rc = tpm_add_hwrng(chip);
583 rc = tpm_add_char_device(chip);
587 rc = tpm_add_legacy_sysfs(chip);
589 tpm_chip_unregister(chip);
596 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
597 hwrng_unregister(&chip->hwrng);
599 tpm_bios_log_teardown(chip);
603 EXPORT_SYMBOL_GPL(tpm_chip_register);
606 * tpm_chip_unregister() - release the TPM driver
607 * @chip: TPM chip to use.
609 * Takes the chip first away from the list of available TPM chips and then
610 * cleans up all the resources reserved by tpm_chip_register().
612 * Once this function returns the driver call backs in 'op's will not be
613 * running and will no longer start.
615 * NOTE: This function should be only called before deinitializing chip
618 void tpm_chip_unregister(struct tpm_chip *chip)
620 tpm_del_legacy_sysfs(chip);
621 if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
622 hwrng_unregister(&chip->hwrng);
623 tpm_bios_log_teardown(chip);
624 if (chip->flags & TPM_CHIP_FLAG_TPM2)
625 cdev_device_del(&chip->cdevs, &chip->devs);
626 tpm_del_char_device(chip);
628 EXPORT_SYMBOL_GPL(tpm_chip_unregister);