2 * Copyright (C) 2004 IBM Corporation
3 * Copyright (C) 2014 Intel Corporation
6 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
7 * Leendert van Doorn <leendert@watson.ibm.com>
8 * Dave Safford <safford@watson.ibm.com>
9 * Reiner Sailer <sailer@watson.ibm.com>
10 * Kylene Hall <kjhall@us.ibm.com>
12 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
14 * TPM chip management routines.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation, version 2 of the
23 #include <linux/poll.h>
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include <linux/spinlock.h>
27 #include <linux/freezer.h>
28 #include <linux/major.h>
30 #include "tpm_eventlog.h"
32 DEFINE_IDR(dev_nums_idr);
33 static DEFINE_MUTEX(idr_lock);
35 struct class *tpm_class;
36 struct class *tpmrm_class;
40 * tpm_try_get_ops() - Get a ref to the tpm_chip
43 * The caller must already have some kind of locking to ensure that chip is
44 * valid. This function will lock the chip so that the ops member can be
45 * accessed safely. The locking prevents tpm_chip_unregister from
46 * completing, so it should not be held for long periods.
48 * Returns -ERRNO if the chip could not be got.
50 int tpm_try_get_ops(struct tpm_chip *chip)
54 get_device(&chip->dev);
56 down_read(&chip->ops_sem);
62 up_read(&chip->ops_sem);
63 put_device(&chip->dev);
66 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
69 * tpm_put_ops() - Release a ref to the tpm_chip
72 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
75 void tpm_put_ops(struct tpm_chip *chip)
77 up_read(&chip->ops_sem);
78 put_device(&chip->dev);
80 EXPORT_SYMBOL_GPL(tpm_put_ops);
83 * tpm_chip_find_get() - return tpm_chip for a given chip number
84 * @chip_num: id to find
86 * The return'd chip has been tpm_try_get_ops'd and must be released via
89 struct tpm_chip *tpm_chip_find_get(int chip_num)
91 struct tpm_chip *chip, *res = NULL;
94 mutex_lock(&idr_lock);
96 if (chip_num == TPM_ANY_NUM) {
100 chip = idr_get_next(&dev_nums_idr, &chip_num);
101 if (chip && !tpm_try_get_ops(chip)) {
105 } while (chip_prev != chip_num);
107 chip = idr_find(&dev_nums_idr, chip_num);
108 if (chip && !tpm_try_get_ops(chip))
112 mutex_unlock(&idr_lock);
118 * tpm_dev_release() - free chip memory and the device number
119 * @dev: the character device for the TPM chip
121 * This is used as the release function for the character device.
123 static void tpm_dev_release(struct device *dev)
125 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
127 mutex_lock(&idr_lock);
128 idr_remove(&dev_nums_idr, chip->dev_num);
129 mutex_unlock(&idr_lock);
131 kfree(chip->log.bios_event_log);
132 kfree(chip->work_space.context_buf);
133 kfree(chip->work_space.session_buf);
138 * tpm_class_shutdown() - prepare the TPM device for loss of power.
139 * @dev: device to which the chip is associated.
141 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
143 * Then, calls bus- and device- specific shutdown code.
145 * XXX: This codepath relies on the fact that sysfs is not enabled for
146 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
147 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
149 static int tpm_class_shutdown(struct device *dev)
151 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
153 down_write(&chip->ops_sem);
154 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
155 tpm2_shutdown(chip, TPM2_SU_CLEAR);
159 up_write(&chip->ops_sem);
165 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
166 * @pdev: device to which the chip is associated
167 * At this point pdev mst be initialized, but does not have to
169 * @ops: struct tpm_class_ops instance
171 * Allocates a new struct tpm_chip instance and assigns a free
172 * device number for it. Must be paired with put_device(&chip->dev).
174 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
175 const struct tpm_class_ops *ops)
177 struct tpm_chip *chip;
180 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
182 return ERR_PTR(-ENOMEM);
184 mutex_init(&chip->tpm_mutex);
185 init_rwsem(&chip->ops_sem);
189 mutex_lock(&idr_lock);
190 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
191 mutex_unlock(&idr_lock);
193 dev_err(pdev, "No available tpm device numbers\n");
199 device_initialize(&chip->dev);
201 chip->dev.class = tpm_class;
202 chip->dev.class->shutdown_pre = tpm_class_shutdown;
203 chip->dev.release = tpm_dev_release;
204 chip->dev.parent = pdev;
205 chip->dev.groups = chip->groups;
207 if (chip->dev_num == 0)
208 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
210 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
212 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
217 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
219 cdev_init(&chip->cdev, &tpm_fops);
220 chip->cdev.owner = THIS_MODULE;
222 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
232 put_device(&chip->dev);
235 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
238 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
239 * @pdev: parent device to which the chip is associated
240 * @ops: struct tpm_class_ops instance
242 * Same as tpm_chip_alloc except devm is used to do the put_device
244 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
245 const struct tpm_class_ops *ops)
247 struct tpm_chip *chip;
250 chip = tpm_chip_alloc(pdev, ops);
254 rc = devm_add_action_or_reset(pdev,
255 (void (*)(void *)) put_device,
260 dev_set_drvdata(pdev, chip);
264 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
266 static int tpm_add_char_device(struct tpm_chip *chip)
270 rc = cdev_device_add(&chip->cdev, &chip->dev);
273 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
274 dev_name(&chip->dev), MAJOR(chip->dev.devt),
275 MINOR(chip->dev.devt), rc);
279 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
280 rc = tpm_devs_add(chip);
285 /* Make the chip available. */
286 mutex_lock(&idr_lock);
287 idr_replace(&dev_nums_idr, chip, chip->dev_num);
288 mutex_unlock(&idr_lock);
293 cdev_device_del(&chip->cdev, &chip->dev);
297 static void tpm_del_char_device(struct tpm_chip *chip)
299 cdev_device_del(&chip->cdev, &chip->dev);
301 /* Make the chip unavailable. */
302 mutex_lock(&idr_lock);
303 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
304 mutex_unlock(&idr_lock);
306 /* Make the driver uncallable. */
307 down_write(&chip->ops_sem);
308 if (chip->flags & TPM_CHIP_FLAG_TPM2)
309 tpm2_shutdown(chip, TPM2_SU_CLEAR);
311 up_write(&chip->ops_sem);
314 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
316 struct attribute **i;
318 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
321 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
323 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
324 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
327 /* For compatibility with legacy sysfs paths we provide symlinks from the
328 * parent dev directory to selected names within the tpm chip directory. Old
329 * kernel versions created these files directly under the parent.
331 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
333 struct attribute **i;
336 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
339 rc = __compat_only_sysfs_link_entry_to_kobj(
340 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
341 if (rc && rc != -ENOENT)
344 /* All the names from tpm-sysfs */
345 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
346 rc = __compat_only_sysfs_link_entry_to_kobj(
347 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
349 tpm_del_legacy_sysfs(chip);
357 * tpm_chip_register() - create a character device for the TPM chip
358 * @chip: TPM chip to use.
360 * Creates a character device for the TPM chip and adds sysfs attributes for
361 * the device. As the last step this function adds the chip to the list of TPM
362 * chips available for in-kernel use.
364 * This function should be only called after the chip initialization is
367 int tpm_chip_register(struct tpm_chip *chip)
371 if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
372 if (chip->flags & TPM_CHIP_FLAG_TPM2)
373 rc = tpm2_auto_startup(chip);
375 rc = tpm1_auto_startup(chip);
380 tpm_sysfs_add_device(chip);
382 rc = tpm_bios_log_setup(chip);
383 if (rc != 0 && rc != -ENODEV)
388 rc = tpm_add_char_device(chip);
390 tpm_bios_log_teardown(chip);
394 rc = tpm_add_legacy_sysfs(chip);
396 tpm_chip_unregister(chip);
402 EXPORT_SYMBOL_GPL(tpm_chip_register);
405 * tpm_chip_unregister() - release the TPM driver
406 * @chip: TPM chip to use.
408 * Takes the chip first away from the list of available TPM chips and then
409 * cleans up all the resources reserved by tpm_chip_register().
411 * Once this function returns the driver call backs in 'op's will not be
412 * running and will no longer start.
414 * NOTE: This function should be only called before deinitializing chip
417 void tpm_chip_unregister(struct tpm_chip *chip)
419 tpm_del_legacy_sysfs(chip);
420 tpm_bios_log_teardown(chip);
421 if (chip->flags & TPM_CHIP_FLAG_TPM2)
422 tpm_devs_remove(chip);
423 tpm_del_char_device(chip);
425 EXPORT_SYMBOL_GPL(tpm_chip_unregister);