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>
31 #include "tpm_eventlog.h"
33 DEFINE_IDR(dev_nums_idr);
34 static DEFINE_MUTEX(idr_lock);
36 struct class *tpm_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_slowpath(&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);
136 * tpm_class_shutdown() - prepare the TPM device for loss of power.
137 * @dev: device to which the chip is associated.
139 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
141 * Then, calls bus- and device- specific shutdown code.
143 * XXX: This codepath relies on the fact that sysfs is not enabled for
144 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
145 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
147 static int tpm_class_shutdown(struct device *dev)
149 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
151 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
152 down_write(&chip->ops_sem);
153 tpm2_shutdown(chip, TPM2_SU_CLEAR);
155 up_write(&chip->ops_sem);
157 /* Allow bus- and device-specific code to run. Note: since chip->ops
158 * is NULL, more-specific shutdown code will not be able to issue TPM
161 if (dev->bus && dev->bus->shutdown)
162 dev->bus->shutdown(dev);
163 else if (dev->driver && dev->driver->shutdown)
164 dev->driver->shutdown(dev);
170 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
171 * @pdev: device to which the chip is associated
172 * At this point pdev mst be initialized, but does not have to
174 * @ops: struct tpm_class_ops instance
176 * Allocates a new struct tpm_chip instance and assigns a free
177 * device number for it. Must be paired with put_device(&chip->dev).
179 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
180 const struct tpm_class_ops *ops)
182 struct tpm_chip *chip;
185 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
187 return ERR_PTR(-ENOMEM);
189 mutex_init(&chip->tpm_mutex);
190 init_rwsem(&chip->ops_sem);
194 mutex_lock(&idr_lock);
195 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
196 mutex_unlock(&idr_lock);
198 dev_err(pdev, "No available tpm device numbers\n");
204 device_initialize(&chip->dev);
206 chip->dev.class = tpm_class;
207 chip->dev.class->shutdown = tpm_class_shutdown;
208 chip->dev.release = tpm_dev_release;
209 chip->dev.parent = pdev;
210 chip->dev.groups = chip->groups;
212 if (chip->dev_num == 0)
213 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
215 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
217 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
222 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
224 cdev_init(&chip->cdev, &tpm_fops);
225 chip->cdev.owner = THIS_MODULE;
226 chip->cdev.kobj.parent = &chip->dev.kobj;
231 put_device(&chip->dev);
234 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
237 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
238 * @pdev: parent device to which the chip is associated
239 * @ops: struct tpm_class_ops instance
241 * Same as tpm_chip_alloc except devm is used to do the put_device
243 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
244 const struct tpm_class_ops *ops)
246 struct tpm_chip *chip;
249 chip = tpm_chip_alloc(pdev, ops);
253 rc = devm_add_action_or_reset(pdev,
254 (void (*)(void *)) put_device,
259 dev_set_drvdata(pdev, chip);
263 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
265 static int tpm_add_char_device(struct tpm_chip *chip)
269 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
272 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
273 dev_name(&chip->dev), MAJOR(chip->dev.devt),
274 MINOR(chip->dev.devt), rc);
279 rc = device_add(&chip->dev);
282 "unable to device_register() %s, major %d, minor %d, err=%d\n",
283 dev_name(&chip->dev), MAJOR(chip->dev.devt),
284 MINOR(chip->dev.devt), rc);
286 cdev_del(&chip->cdev);
290 /* Make the chip available. */
291 mutex_lock(&idr_lock);
292 idr_replace(&dev_nums_idr, chip, chip->dev_num);
293 mutex_unlock(&idr_lock);
298 static void tpm_del_char_device(struct tpm_chip *chip)
300 cdev_del(&chip->cdev);
301 device_del(&chip->dev);
303 /* Make the chip unavailable. */
304 mutex_lock(&idr_lock);
305 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
306 mutex_unlock(&idr_lock);
308 /* Make the driver uncallable. */
309 down_write(&chip->ops_sem);
310 if (chip->flags & TPM_CHIP_FLAG_TPM2)
311 tpm2_shutdown(chip, TPM2_SU_CLEAR);
313 up_write(&chip->ops_sem);
316 static int tpm1_chip_register(struct tpm_chip *chip)
318 if (chip->flags & TPM_CHIP_FLAG_TPM2)
321 tpm_sysfs_add_device(chip);
323 chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
328 static void tpm1_chip_unregister(struct tpm_chip *chip)
330 if (chip->flags & TPM_CHIP_FLAG_TPM2)
334 tpm_bios_log_teardown(chip->bios_dir);
337 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
339 struct attribute **i;
341 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
344 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
346 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
347 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
350 /* For compatibility with legacy sysfs paths we provide symlinks from the
351 * parent dev directory to selected names within the tpm chip directory. Old
352 * kernel versions created these files directly under the parent.
354 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
356 struct attribute **i;
359 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
362 rc = __compat_only_sysfs_link_entry_to_kobj(
363 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
364 if (rc && rc != -ENOENT)
367 /* All the names from tpm-sysfs */
368 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
369 rc = __compat_only_sysfs_link_entry_to_kobj(
370 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
372 tpm_del_legacy_sysfs(chip);
380 * tpm_chip_register() - create a character device for the TPM chip
381 * @chip: TPM chip to use.
383 * Creates a character device for the TPM chip and adds sysfs attributes for
384 * the device. As the last step this function adds the chip to the list of TPM
385 * chips available for in-kernel use.
387 * This function should be only called after the chip initialization is
390 int tpm_chip_register(struct tpm_chip *chip)
393 struct device_node *np;
398 np = of_find_node_by_name(NULL, "vtpm");
400 if (of_property_read_bool(np, "powered-while-suspended"))
401 chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
406 if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
407 if (chip->flags & TPM_CHIP_FLAG_TPM2)
408 rc = tpm2_auto_startup(chip);
410 rc = tpm1_auto_startup(chip);
415 rc = tpm1_chip_register(chip);
421 rc = tpm_add_char_device(chip);
423 tpm1_chip_unregister(chip);
427 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
429 rc = tpm_add_legacy_sysfs(chip);
431 tpm_chip_unregister(chip);
437 EXPORT_SYMBOL_GPL(tpm_chip_register);
440 * tpm_chip_unregister() - release the TPM driver
441 * @chip: TPM chip to use.
443 * Takes the chip first away from the list of available TPM chips and then
444 * cleans up all the resources reserved by tpm_chip_register().
446 * Once this function returns the driver call backs in 'op's will not be
447 * running and will no longer start.
449 * NOTE: This function should be only called before deinitializing chip
452 void tpm_chip_unregister(struct tpm_chip *chip)
454 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
457 tpm_del_legacy_sysfs(chip);
459 tpm1_chip_unregister(chip);
460 tpm_del_char_device(chip);
462 EXPORT_SYMBOL_GPL(tpm_chip_unregister);