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);
137 static void tpm_devs_release(struct device *dev)
139 struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
141 /* release the master device reference */
142 put_device(&chip->dev);
146 * tpm_class_shutdown() - prepare the TPM device for loss of power.
147 * @dev: device to which the chip is associated.
149 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
151 * Then, calls bus- and device- specific shutdown code.
153 * XXX: This codepath relies on the fact that sysfs is not enabled for
154 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
155 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
157 static int tpm_class_shutdown(struct device *dev)
159 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
161 down_write(&chip->ops_sem);
162 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
163 tpm2_shutdown(chip, TPM2_SU_CLEAR);
167 up_write(&chip->ops_sem);
173 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
174 * @pdev: device to which the chip is associated
175 * At this point pdev mst be initialized, but does not have to
177 * @ops: struct tpm_class_ops instance
179 * Allocates a new struct tpm_chip instance and assigns a free
180 * device number for it. Must be paired with put_device(&chip->dev).
182 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
183 const struct tpm_class_ops *ops)
185 struct tpm_chip *chip;
188 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
190 return ERR_PTR(-ENOMEM);
192 mutex_init(&chip->tpm_mutex);
193 init_rwsem(&chip->ops_sem);
197 mutex_lock(&idr_lock);
198 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
199 mutex_unlock(&idr_lock);
201 dev_err(pdev, "No available tpm device numbers\n");
207 device_initialize(&chip->dev);
208 device_initialize(&chip->devs);
210 chip->dev.class = tpm_class;
211 chip->dev.class->shutdown_pre = tpm_class_shutdown;
212 chip->dev.release = tpm_dev_release;
213 chip->dev.parent = pdev;
214 chip->dev.groups = chip->groups;
216 chip->devs.parent = pdev;
217 chip->devs.class = tpmrm_class;
218 chip->devs.release = tpm_devs_release;
219 /* get extra reference on main device to hold on
220 * behalf of devs. This holds the chip structure
221 * while cdevs is in use. The corresponding put
222 * is in the tpm_devs_release (TPM2 only)
224 if (chip->flags & TPM_CHIP_FLAG_TPM2)
225 get_device(&chip->dev);
227 if (chip->dev_num == 0)
228 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
230 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
233 MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
235 rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
238 rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
243 chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
245 cdev_init(&chip->cdev, &tpm_fops);
246 cdev_init(&chip->cdevs, &tpmrm_fops);
247 chip->cdev.owner = THIS_MODULE;
248 chip->cdevs.owner = THIS_MODULE;
250 rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
260 put_device(&chip->devs);
261 put_device(&chip->dev);
264 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
267 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
268 * @pdev: parent device to which the chip is associated
269 * @ops: struct tpm_class_ops instance
271 * Same as tpm_chip_alloc except devm is used to do the put_device
273 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
274 const struct tpm_class_ops *ops)
276 struct tpm_chip *chip;
279 chip = tpm_chip_alloc(pdev, ops);
283 rc = devm_add_action_or_reset(pdev,
284 (void (*)(void *)) put_device,
289 dev_set_drvdata(pdev, chip);
293 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
295 static int tpm_add_char_device(struct tpm_chip *chip)
299 rc = cdev_device_add(&chip->cdev, &chip->dev);
302 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
303 dev_name(&chip->dev), MAJOR(chip->dev.devt),
304 MINOR(chip->dev.devt), rc);
308 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
309 rc = cdev_device_add(&chip->cdevs, &chip->devs);
312 "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
313 dev_name(&chip->devs), MAJOR(chip->devs.devt),
314 MINOR(chip->devs.devt), rc);
319 /* Make the chip available. */
320 mutex_lock(&idr_lock);
321 idr_replace(&dev_nums_idr, chip, chip->dev_num);
322 mutex_unlock(&idr_lock);
327 static void tpm_del_char_device(struct tpm_chip *chip)
329 cdev_device_del(&chip->cdev, &chip->dev);
331 /* Make the chip unavailable. */
332 mutex_lock(&idr_lock);
333 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
334 mutex_unlock(&idr_lock);
336 /* Make the driver uncallable. */
337 down_write(&chip->ops_sem);
338 if (chip->flags & TPM_CHIP_FLAG_TPM2)
339 tpm2_shutdown(chip, TPM2_SU_CLEAR);
341 up_write(&chip->ops_sem);
344 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
346 struct attribute **i;
348 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
351 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
353 for (i = chip->groups[0]->attrs; *i != NULL; ++i)
354 sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
357 /* For compatibility with legacy sysfs paths we provide symlinks from the
358 * parent dev directory to selected names within the tpm chip directory. Old
359 * kernel versions created these files directly under the parent.
361 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
363 struct attribute **i;
366 if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
369 rc = __compat_only_sysfs_link_entry_to_kobj(
370 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
371 if (rc && rc != -ENOENT)
374 /* All the names from tpm-sysfs */
375 for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
376 rc = __compat_only_sysfs_link_entry_to_kobj(
377 &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
379 tpm_del_legacy_sysfs(chip);
387 * tpm_chip_register() - create a character device for the TPM chip
388 * @chip: TPM chip to use.
390 * Creates a character device for the TPM chip and adds sysfs attributes for
391 * the device. As the last step this function adds the chip to the list of TPM
392 * chips available for in-kernel use.
394 * This function should be only called after the chip initialization is
397 int tpm_chip_register(struct tpm_chip *chip)
401 if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
402 if (chip->flags & TPM_CHIP_FLAG_TPM2)
403 rc = tpm2_auto_startup(chip);
405 rc = tpm1_auto_startup(chip);
410 tpm_sysfs_add_device(chip);
412 rc = tpm_bios_log_setup(chip);
413 if (rc != 0 && rc != -ENODEV)
418 rc = tpm_add_char_device(chip);
420 tpm_bios_log_teardown(chip);
424 rc = tpm_add_legacy_sysfs(chip);
426 tpm_chip_unregister(chip);
432 EXPORT_SYMBOL_GPL(tpm_chip_register);
435 * tpm_chip_unregister() - release the TPM driver
436 * @chip: TPM chip to use.
438 * Takes the chip first away from the list of available TPM chips and then
439 * cleans up all the resources reserved by tpm_chip_register().
441 * Once this function returns the driver call backs in 'op's will not be
442 * running and will no longer start.
444 * NOTE: This function should be only called before deinitializing chip
447 void tpm_chip_unregister(struct tpm_chip *chip)
449 tpm_del_legacy_sysfs(chip);
450 tpm_bios_log_teardown(chip);
451 if (chip->flags & TPM_CHIP_FLAG_TPM2)
452 cdev_device_del(&chip->cdevs, &chip->devs);
453 tpm_del_char_device(chip);
455 EXPORT_SYMBOL_GPL(tpm_chip_unregister);