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);
60 if (!try_module_get(chip->dev.parent->driver->owner))
65 up_read(&chip->ops_sem);
66 put_device(&chip->dev);
69 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
72 * tpm_put_ops() - Release a ref to the tpm_chip
75 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
78 void tpm_put_ops(struct tpm_chip *chip)
80 module_put(chip->dev.parent->driver->owner);
81 up_read(&chip->ops_sem);
82 put_device(&chip->dev);
84 EXPORT_SYMBOL_GPL(tpm_put_ops);
87 * tpm_chip_find_get() - return tpm_chip for a given chip number
88 * @chip_num: id to find
90 * The return'd chip has been tpm_try_get_ops'd and must be released via
93 struct tpm_chip *tpm_chip_find_get(int chip_num)
95 struct tpm_chip *chip, *res = NULL;
98 mutex_lock(&idr_lock);
100 if (chip_num == TPM_ANY_NUM) {
103 chip_prev = chip_num;
104 chip = idr_get_next(&dev_nums_idr, &chip_num);
105 if (chip && !tpm_try_get_ops(chip)) {
109 } while (chip_prev != chip_num);
111 chip = idr_find_slowpath(&dev_nums_idr, chip_num);
112 if (chip && !tpm_try_get_ops(chip))
116 mutex_unlock(&idr_lock);
122 * tpm_dev_release() - free chip memory and the device number
123 * @dev: the character device for the TPM chip
125 * This is used as the release function for the character device.
127 static void tpm_dev_release(struct device *dev)
129 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
131 mutex_lock(&idr_lock);
132 idr_remove(&dev_nums_idr, chip->dev_num);
133 mutex_unlock(&idr_lock);
140 * tpm_class_shutdown() - prepare the TPM device for loss of power.
141 * @dev: device to which the chip is associated.
143 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
145 * Then, calls bus- and device- specific shutdown code.
147 * XXX: This codepath relies on the fact that sysfs is not enabled for
148 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
149 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
151 static int tpm_class_shutdown(struct device *dev)
153 struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
155 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
156 down_write(&chip->ops_sem);
157 tpm2_shutdown(chip, TPM2_SU_CLEAR);
159 up_write(&chip->ops_sem);
161 /* Allow bus- and device-specific code to run. Note: since chip->ops
162 * is NULL, more-specific shutdown code will not be able to issue TPM
165 if (dev->bus && dev->bus->shutdown)
166 dev->bus->shutdown(dev);
167 else if (dev->driver && dev->driver->shutdown)
168 dev->driver->shutdown(dev);
174 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
175 * @dev: device to which the chip is associated
176 * @ops: struct tpm_class_ops instance
178 * Allocates a new struct tpm_chip instance and assigns a free
179 * device number for it. Caller does not have to worry about
180 * freeing the allocated resources. When the devices is removed
181 * devres calls tpmm_chip_remove() to do the job.
183 struct tpm_chip *tpmm_chip_alloc(struct device *dev,
184 const struct tpm_class_ops *ops)
186 struct tpm_chip *chip;
189 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
191 return ERR_PTR(-ENOMEM);
193 mutex_init(&chip->tpm_mutex);
194 init_rwsem(&chip->ops_sem);
198 mutex_lock(&idr_lock);
199 rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
200 mutex_unlock(&idr_lock);
202 dev_err(dev, "No available tpm device numbers\n");
208 scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
210 dev_set_drvdata(dev, chip);
212 chip->dev.class = tpm_class;
213 chip->dev.class->shutdown = tpm_class_shutdown;
214 chip->dev.release = tpm_dev_release;
215 chip->dev.parent = dev;
217 chip->dev.groups = chip->groups;
220 if (chip->dev_num == 0)
221 chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
223 chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
225 dev_set_name(&chip->dev, "%s", chip->devname);
227 device_initialize(&chip->dev);
229 cdev_init(&chip->cdev, &tpm_fops);
230 chip->cdev.owner = dev->driver->owner;
231 chip->cdev.kobj.parent = &chip->dev.kobj;
233 rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
235 put_device(&chip->dev);
241 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
243 static int tpm_add_char_device(struct tpm_chip *chip)
247 rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
250 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
251 chip->devname, MAJOR(chip->dev.devt),
252 MINOR(chip->dev.devt), rc);
257 rc = device_add(&chip->dev);
260 "unable to device_register() %s, major %d, minor %d, err=%d\n",
261 chip->devname, MAJOR(chip->dev.devt),
262 MINOR(chip->dev.devt), rc);
264 cdev_del(&chip->cdev);
268 /* Make the chip available. */
269 mutex_lock(&idr_lock);
270 idr_replace(&dev_nums_idr, chip, chip->dev_num);
271 mutex_unlock(&idr_lock);
276 static void tpm_del_char_device(struct tpm_chip *chip)
278 cdev_del(&chip->cdev);
279 device_del(&chip->dev);
281 /* Make the chip unavailable. */
282 mutex_lock(&idr_lock);
283 idr_replace(&dev_nums_idr, NULL, chip->dev_num);
284 mutex_unlock(&idr_lock);
286 /* Make the driver uncallable. */
287 down_write(&chip->ops_sem);
289 up_write(&chip->ops_sem);
292 static int tpm1_chip_register(struct tpm_chip *chip)
296 if (chip->flags & TPM_CHIP_FLAG_TPM2)
299 rc = tpm_sysfs_add_device(chip);
303 chip->bios_dir = tpm_bios_log_setup(chip->devname);
308 static void tpm1_chip_unregister(struct tpm_chip *chip)
310 if (chip->flags & TPM_CHIP_FLAG_TPM2)
314 tpm_bios_log_teardown(chip->bios_dir);
316 tpm_sysfs_del_device(chip);
320 * tpm_chip_register() - create a character device for the TPM chip
321 * @chip: TPM chip to use.
323 * Creates a character device for the TPM chip and adds sysfs attributes for
324 * the device. As the last step this function adds the chip to the list of TPM
325 * chips available for in-kernel use.
327 * This function should be only called after the chip initialization is
330 int tpm_chip_register(struct tpm_chip *chip)
333 struct device_node *np;
338 np = of_find_node_by_name(NULL, "vtpm");
340 if (of_property_read_bool(np, "powered-while-suspended"))
341 chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
346 rc = tpm1_chip_register(chip);
352 rc = tpm_add_char_device(chip);
356 chip->flags |= TPM_CHIP_FLAG_REGISTERED;
358 if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
359 rc = __compat_only_sysfs_link_entry_to_kobj(
360 &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
361 if (rc && rc != -ENOENT) {
362 tpm_chip_unregister(chip);
369 tpm1_chip_unregister(chip);
372 EXPORT_SYMBOL_GPL(tpm_chip_register);
375 * tpm_chip_unregister() - release the TPM driver
376 * @chip: TPM chip to use.
378 * Takes the chip first away from the list of available TPM chips and then
379 * cleans up all the resources reserved by tpm_chip_register().
381 * Once this function returns the driver call backs in 'op's will not be
382 * running and will no longer start.
384 * NOTE: This function should be only called before deinitializing chip
387 void tpm_chip_unregister(struct tpm_chip *chip)
389 if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
392 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
393 sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
395 tpm1_chip_unregister(chip);
396 tpm_del_char_device(chip);
398 EXPORT_SYMBOL_GPL(tpm_chip_unregister);