• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004 IBM Corporation
3  * Copyright (C) 2014 Intel Corporation
4  *
5  * Authors:
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>
11  *
12  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13  *
14  * TPM chip management routines.
15  *
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
19  * License.
20  *
21  */
22 
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>
29 #include <linux/of.h>
30 #include "tpm.h"
31 #include "tpm_eventlog.h"
32 
33 DEFINE_IDR(dev_nums_idr);
34 static DEFINE_MUTEX(idr_lock);
35 
36 struct class *tpm_class;
37 dev_t tpm_devt;
38 
39 /**
40  * tpm_try_get_ops() - Get a ref to the tpm_chip
41  * @chip: Chip to ref
42  *
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.
47  *
48  * Returns -ERRNO if the chip could not be got.
49  */
tpm_try_get_ops(struct tpm_chip * chip)50 int tpm_try_get_ops(struct tpm_chip *chip)
51 {
52 	int rc = -EIO;
53 
54 	get_device(&chip->dev);
55 
56 	down_read(&chip->ops_sem);
57 	if (!chip->ops)
58 		goto out_lock;
59 
60 	if (!try_module_get(chip->dev.parent->driver->owner))
61 		goto out_lock;
62 
63 	return 0;
64 out_lock:
65 	up_read(&chip->ops_sem);
66 	put_device(&chip->dev);
67 	return rc;
68 }
69 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
70 
71 /**
72  * tpm_put_ops() - Release a ref to the tpm_chip
73  * @chip: Chip to put
74  *
75  * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
76  * be kfree'd.
77  */
tpm_put_ops(struct tpm_chip * chip)78 void tpm_put_ops(struct tpm_chip *chip)
79 {
80 	module_put(chip->dev.parent->driver->owner);
81 	up_read(&chip->ops_sem);
82 	put_device(&chip->dev);
83 }
84 EXPORT_SYMBOL_GPL(tpm_put_ops);
85 
86 /**
87  * tpm_chip_find_get() - return tpm_chip for a given chip number
88  * @chip_num: id to find
89  *
90  * The return'd chip has been tpm_try_get_ops'd and must be released via
91  * tpm_put_ops
92   */
tpm_chip_find_get(int chip_num)93 struct tpm_chip *tpm_chip_find_get(int chip_num)
94 {
95 	struct tpm_chip *chip, *res = NULL;
96 	int chip_prev;
97 
98 	mutex_lock(&idr_lock);
99 
100 	if (chip_num == TPM_ANY_NUM) {
101 		chip_num = 0;
102 		do {
103 			chip_prev = chip_num;
104 			chip = idr_get_next(&dev_nums_idr, &chip_num);
105 			if (chip && !tpm_try_get_ops(chip)) {
106 				res = chip;
107 				break;
108 			}
109 		} while (chip_prev != chip_num);
110 	} else {
111 		chip = idr_find_slowpath(&dev_nums_idr, chip_num);
112 		if (chip && !tpm_try_get_ops(chip))
113 			res = chip;
114 	}
115 
116 	mutex_unlock(&idr_lock);
117 
118 	return res;
119 }
120 
121 /**
122  * tpm_dev_release() - free chip memory and the device number
123  * @dev: the character device for the TPM chip
124  *
125  * This is used as the release function for the character device.
126  */
tpm_dev_release(struct device * dev)127 static void tpm_dev_release(struct device *dev)
128 {
129 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
130 
131 	mutex_lock(&idr_lock);
132 	idr_remove(&dev_nums_idr, chip->dev_num);
133 	mutex_unlock(&idr_lock);
134 
135 	kfree(chip);
136 }
137 
138 
139 /**
140  * tpm_class_shutdown() - prepare the TPM device for loss of power.
141  * @dev: device to which the chip is associated.
142  *
143  * Issues a TPM2_Shutdown command prior to loss of power, as required by the
144  * TPM 2.0 spec.
145  * Then, calls bus- and device- specific shutdown code.
146  *
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.
150  */
tpm_class_shutdown(struct device * dev)151 static int tpm_class_shutdown(struct device *dev)
152 {
153 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
154 
155 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
156 		down_write(&chip->ops_sem);
157 		tpm2_shutdown(chip, TPM2_SU_CLEAR);
158 		chip->ops = NULL;
159 		up_write(&chip->ops_sem);
160 	}
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
163 	 * commands.
164 	 */
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);
169 	return 0;
170 }
171 
172 
173 /**
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
177  *
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.
182  */
tpmm_chip_alloc(struct device * dev,const struct tpm_class_ops * ops)183 struct tpm_chip *tpmm_chip_alloc(struct device *dev,
184 				 const struct tpm_class_ops *ops)
185 {
186 	struct tpm_chip *chip;
187 	int rc;
188 
189 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
190 	if (chip == NULL)
191 		return ERR_PTR(-ENOMEM);
192 
193 	mutex_init(&chip->tpm_mutex);
194 	init_rwsem(&chip->ops_sem);
195 
196 	chip->ops = ops;
197 
198 	mutex_lock(&idr_lock);
199 	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
200 	mutex_unlock(&idr_lock);
201 	if (rc < 0) {
202 		dev_err(dev, "No available tpm device numbers\n");
203 		kfree(chip);
204 		return ERR_PTR(rc);
205 	}
206 	chip->dev_num = rc;
207 
208 	scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
209 
210 	dev_set_drvdata(dev, chip);
211 
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;
216 #ifdef CONFIG_ACPI
217 	chip->dev.groups = chip->groups;
218 #endif
219 
220 	if (chip->dev_num == 0)
221 		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
222 	else
223 		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
224 
225 	dev_set_name(&chip->dev, "%s", chip->devname);
226 
227 	device_initialize(&chip->dev);
228 
229 	cdev_init(&chip->cdev, &tpm_fops);
230 	chip->cdev.owner = dev->driver->owner;
231 	chip->cdev.kobj.parent = &chip->dev.kobj;
232 
233 	rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
234 	if (rc) {
235 		put_device(&chip->dev);
236 		return ERR_PTR(rc);
237 	}
238 
239 	return chip;
240 }
241 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
242 
tpm_add_char_device(struct tpm_chip * chip)243 static int tpm_add_char_device(struct tpm_chip *chip)
244 {
245 	int rc;
246 
247 	rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
248 	if (rc) {
249 		dev_err(&chip->dev,
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);
253 
254 		return rc;
255 	}
256 
257 	rc = device_add(&chip->dev);
258 	if (rc) {
259 		dev_err(&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);
263 
264 		cdev_del(&chip->cdev);
265 		return rc;
266 	}
267 
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);
272 
273 	return rc;
274 }
275 
tpm_del_char_device(struct tpm_chip * chip)276 static void tpm_del_char_device(struct tpm_chip *chip)
277 {
278 	cdev_del(&chip->cdev);
279 	device_del(&chip->dev);
280 
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);
285 
286 	/* Make the driver uncallable. */
287 	down_write(&chip->ops_sem);
288 	chip->ops = NULL;
289 	up_write(&chip->ops_sem);
290 }
291 
tpm1_chip_register(struct tpm_chip * chip)292 static int tpm1_chip_register(struct tpm_chip *chip)
293 {
294 	int rc;
295 
296 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
297 		return 0;
298 
299 	rc = tpm_sysfs_add_device(chip);
300 	if (rc)
301 		return rc;
302 
303 	chip->bios_dir = tpm_bios_log_setup(chip->devname);
304 
305 	return 0;
306 }
307 
tpm1_chip_unregister(struct tpm_chip * chip)308 static void tpm1_chip_unregister(struct tpm_chip *chip)
309 {
310 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
311 		return;
312 
313 	if (chip->bios_dir)
314 		tpm_bios_log_teardown(chip->bios_dir);
315 
316 	tpm_sysfs_del_device(chip);
317 }
318 
319 /*
320  * tpm_chip_register() - create a character device for the TPM chip
321  * @chip: TPM chip to use.
322  *
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.
326  *
327  * This function should be only called after the chip initialization is
328  * complete.
329  */
tpm_chip_register(struct tpm_chip * chip)330 int tpm_chip_register(struct tpm_chip *chip)
331 {
332 #ifdef CONFIG_OF
333 	struct device_node *np;
334 #endif
335 	int rc;
336 
337 #ifdef CONFIG_OF
338 	np = of_find_node_by_name(NULL, "vtpm");
339 	if (np) {
340 		if (of_property_read_bool(np, "powered-while-suspended"))
341 			chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
342 	}
343 	of_node_put(np);
344 #endif
345 
346 	rc = tpm1_chip_register(chip);
347 	if (rc)
348 		return rc;
349 
350 	tpm_add_ppi(chip);
351 
352 	rc = tpm_add_char_device(chip);
353 	if (rc)
354 		goto out_err;
355 
356 	chip->flags |= TPM_CHIP_FLAG_REGISTERED;
357 
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);
363 			return rc;
364 		}
365 	}
366 
367 	return 0;
368 out_err:
369 	tpm1_chip_unregister(chip);
370 	return rc;
371 }
372 EXPORT_SYMBOL_GPL(tpm_chip_register);
373 
374 /*
375  * tpm_chip_unregister() - release the TPM driver
376  * @chip: TPM chip to use.
377  *
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().
380  *
381  * Once this function returns the driver call backs in 'op's will not be
382  * running and will no longer start.
383  *
384  * NOTE: This function should be only called before deinitializing chip
385  * resources.
386  */
tpm_chip_unregister(struct tpm_chip * chip)387 void tpm_chip_unregister(struct tpm_chip *chip)
388 {
389 	if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
390 		return;
391 
392 	if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
393 		sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
394 
395 	tpm1_chip_unregister(chip);
396 	tpm_del_char_device(chip);
397 }
398 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
399