xref: /linux/drivers/char/tpm/tpm-chip.c (revision 088e88be5a380cc4e81963a9a02815da465d144f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2004 IBM Corporation
4  * Copyright (C) 2014 Intel Corporation
5  *
6  * Authors:
7  * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
8  * Leendert van Doorn <leendert@watson.ibm.com>
9  * Dave Safford <safford@watson.ibm.com>
10  * Reiner Sailer <sailer@watson.ibm.com>
11  * Kylene Hall <kjhall@us.ibm.com>
12  *
13  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
14  *
15  * TPM chip management routines.
16  */
17 
18 #include <linux/poll.h>
19 #include <linux/slab.h>
20 #include <linux/mutex.h>
21 #include <linux/spinlock.h>
22 #include <linux/freezer.h>
23 #include <linux/major.h>
24 #include <linux/tpm_eventlog.h>
25 #include <linux/hw_random.h>
26 #include "tpm.h"
27 
28 DEFINE_IDR(dev_nums_idr);
29 static DEFINE_MUTEX(idr_lock);
30 
31 struct class *tpm_class;
32 struct class *tpmrm_class;
33 dev_t tpm_devt;
34 
35 static int tpm_request_locality(struct tpm_chip *chip)
36 {
37 	int rc;
38 
39 	if (!chip->ops->request_locality)
40 		return 0;
41 
42 	rc = chip->ops->request_locality(chip, 0);
43 	if (rc < 0)
44 		return rc;
45 
46 	chip->locality = rc;
47 	return 0;
48 }
49 
50 static void tpm_relinquish_locality(struct tpm_chip *chip)
51 {
52 	int rc;
53 
54 	if (!chip->ops->relinquish_locality)
55 		return;
56 
57 	rc = chip->ops->relinquish_locality(chip, chip->locality);
58 	if (rc)
59 		dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
60 
61 	chip->locality = -1;
62 }
63 
64 static int tpm_cmd_ready(struct tpm_chip *chip)
65 {
66 	if (!chip->ops->cmd_ready)
67 		return 0;
68 
69 	return chip->ops->cmd_ready(chip);
70 }
71 
72 static int tpm_go_idle(struct tpm_chip *chip)
73 {
74 	if (!chip->ops->go_idle)
75 		return 0;
76 
77 	return chip->ops->go_idle(chip);
78 }
79 
80 /**
81  * tpm_chip_start() - power on the TPM
82  * @chip:	a TPM chip to use
83  *
84  * Return:
85  * * The response length	- OK
86  * * -errno			- A system error
87  */
88 int tpm_chip_start(struct tpm_chip *chip)
89 {
90 	int ret;
91 
92 	if (chip->ops->clk_enable)
93 		chip->ops->clk_enable(chip, true);
94 
95 	if (chip->locality == -1) {
96 		ret = tpm_request_locality(chip);
97 		if (ret) {
98 			chip->ops->clk_enable(chip, false);
99 			return ret;
100 		}
101 	}
102 
103 	ret = tpm_cmd_ready(chip);
104 	if (ret) {
105 		tpm_relinquish_locality(chip);
106 		if (chip->ops->clk_enable)
107 			chip->ops->clk_enable(chip, false);
108 		return ret;
109 	}
110 
111 	return 0;
112 }
113 EXPORT_SYMBOL_GPL(tpm_chip_start);
114 
115 /**
116  * tpm_chip_stop() - power off the TPM
117  * @chip:	a TPM chip to use
118  *
119  * Return:
120  * * The response length	- OK
121  * * -errno			- A system error
122  */
123 void tpm_chip_stop(struct tpm_chip *chip)
124 {
125 	tpm_go_idle(chip);
126 	tpm_relinquish_locality(chip);
127 	if (chip->ops->clk_enable)
128 		chip->ops->clk_enable(chip, false);
129 }
130 EXPORT_SYMBOL_GPL(tpm_chip_stop);
131 
132 /**
133  * tpm_try_get_ops() - Get a ref to the tpm_chip
134  * @chip: Chip to ref
135  *
136  * The caller must already have some kind of locking to ensure that chip is
137  * valid. This function will lock the chip so that the ops member can be
138  * accessed safely. The locking prevents tpm_chip_unregister from
139  * completing, so it should not be held for long periods.
140  *
141  * Returns -ERRNO if the chip could not be got.
142  */
143 int tpm_try_get_ops(struct tpm_chip *chip)
144 {
145 	int rc = -EIO;
146 
147 	get_device(&chip->dev);
148 
149 	down_read(&chip->ops_sem);
150 	if (!chip->ops)
151 		goto out_ops;
152 
153 	mutex_lock(&chip->tpm_mutex);
154 	rc = tpm_chip_start(chip);
155 	if (rc)
156 		goto out_lock;
157 
158 	return 0;
159 out_lock:
160 	mutex_unlock(&chip->tpm_mutex);
161 out_ops:
162 	up_read(&chip->ops_sem);
163 	put_device(&chip->dev);
164 	return rc;
165 }
166 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
167 
168 /**
169  * tpm_put_ops() - Release a ref to the tpm_chip
170  * @chip: Chip to put
171  *
172  * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
173  * be kfree'd.
174  */
175 void tpm_put_ops(struct tpm_chip *chip)
176 {
177 	tpm_chip_stop(chip);
178 	mutex_unlock(&chip->tpm_mutex);
179 	up_read(&chip->ops_sem);
180 	put_device(&chip->dev);
181 }
182 EXPORT_SYMBOL_GPL(tpm_put_ops);
183 
184 /**
185  * tpm_default_chip() - find a TPM chip and get a reference to it
186  */
187 struct tpm_chip *tpm_default_chip(void)
188 {
189 	struct tpm_chip *chip, *res = NULL;
190 	int chip_num = 0;
191 	int chip_prev;
192 
193 	mutex_lock(&idr_lock);
194 
195 	do {
196 		chip_prev = chip_num;
197 		chip = idr_get_next(&dev_nums_idr, &chip_num);
198 		if (chip) {
199 			get_device(&chip->dev);
200 			res = chip;
201 			break;
202 		}
203 	} while (chip_prev != chip_num);
204 
205 	mutex_unlock(&idr_lock);
206 
207 	return res;
208 }
209 EXPORT_SYMBOL_GPL(tpm_default_chip);
210 
211 /**
212  * tpm_find_get_ops() - find and reserve a TPM chip
213  * @chip:	a &struct tpm_chip instance, %NULL for the default chip
214  *
215  * Finds a TPM chip and reserves its class device and operations. The chip must
216  * be released with tpm_put_ops() after use.
217  * This function is for internal use only. It supports existing TPM callers
218  * by accepting NULL, but those callers should be converted to pass in a chip
219  * directly.
220  *
221  * Return:
222  * A reserved &struct tpm_chip instance.
223  * %NULL if a chip is not found.
224  * %NULL if the chip is not available.
225  */
226 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
227 {
228 	int rc;
229 
230 	if (chip) {
231 		if (!tpm_try_get_ops(chip))
232 			return chip;
233 		return NULL;
234 	}
235 
236 	chip = tpm_default_chip();
237 	if (!chip)
238 		return NULL;
239 	rc = tpm_try_get_ops(chip);
240 	/* release additional reference we got from tpm_default_chip() */
241 	put_device(&chip->dev);
242 	if (rc)
243 		return NULL;
244 	return chip;
245 }
246 
247 /**
248  * tpm_dev_release() - free chip memory and the device number
249  * @dev: the character device for the TPM chip
250  *
251  * This is used as the release function for the character device.
252  */
253 static void tpm_dev_release(struct device *dev)
254 {
255 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
256 
257 	mutex_lock(&idr_lock);
258 	idr_remove(&dev_nums_idr, chip->dev_num);
259 	mutex_unlock(&idr_lock);
260 
261 	kfree(chip->log.bios_event_log);
262 	kfree(chip->work_space.context_buf);
263 	kfree(chip->work_space.session_buf);
264 	kfree(chip->allocated_banks);
265 	kfree(chip);
266 }
267 
268 static void tpm_devs_release(struct device *dev)
269 {
270 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);
271 
272 	/* release the master device reference */
273 	put_device(&chip->dev);
274 }
275 
276 /**
277  * tpm_class_shutdown() - prepare the TPM device for loss of power.
278  * @dev: device to which the chip is associated.
279  *
280  * Issues a TPM2_Shutdown command prior to loss of power, as required by the
281  * TPM 2.0 spec.
282  * Then, calls bus- and device- specific shutdown code.
283  *
284  * XXX: This codepath relies on the fact that sysfs is not enabled for
285  * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
286  * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
287  */
288 static int tpm_class_shutdown(struct device *dev)
289 {
290 	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
291 
292 	down_write(&chip->ops_sem);
293 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
294 		if (!tpm_chip_start(chip)) {
295 			tpm2_shutdown(chip, TPM2_SU_CLEAR);
296 			tpm_chip_stop(chip);
297 		}
298 	}
299 	chip->ops = NULL;
300 	up_write(&chip->ops_sem);
301 
302 	return 0;
303 }
304 
305 /**
306  * tpm_chip_alloc() - allocate a new struct tpm_chip instance
307  * @pdev: device to which the chip is associated
308  *        At this point pdev mst be initialized, but does not have to
309  *        be registered
310  * @ops: struct tpm_class_ops instance
311  *
312  * Allocates a new struct tpm_chip instance and assigns a free
313  * device number for it. Must be paired with put_device(&chip->dev).
314  */
315 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
316 				const struct tpm_class_ops *ops)
317 {
318 	struct tpm_chip *chip;
319 	int rc;
320 
321 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
322 	if (chip == NULL)
323 		return ERR_PTR(-ENOMEM);
324 
325 	mutex_init(&chip->tpm_mutex);
326 	init_rwsem(&chip->ops_sem);
327 
328 	chip->ops = ops;
329 
330 	mutex_lock(&idr_lock);
331 	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
332 	mutex_unlock(&idr_lock);
333 	if (rc < 0) {
334 		dev_err(pdev, "No available tpm device numbers\n");
335 		kfree(chip);
336 		return ERR_PTR(rc);
337 	}
338 	chip->dev_num = rc;
339 
340 	device_initialize(&chip->dev);
341 	device_initialize(&chip->devs);
342 
343 	chip->dev.class = tpm_class;
344 	chip->dev.class->shutdown_pre = tpm_class_shutdown;
345 	chip->dev.release = tpm_dev_release;
346 	chip->dev.parent = pdev;
347 	chip->dev.groups = chip->groups;
348 
349 	chip->devs.parent = pdev;
350 	chip->devs.class = tpmrm_class;
351 	chip->devs.release = tpm_devs_release;
352 	/* get extra reference on main device to hold on
353 	 * behalf of devs.  This holds the chip structure
354 	 * while cdevs is in use.  The corresponding put
355 	 * is in the tpm_devs_release (TPM2 only)
356 	 */
357 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
358 		get_device(&chip->dev);
359 
360 	if (chip->dev_num == 0)
361 		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
362 	else
363 		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
364 
365 	chip->devs.devt =
366 		MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);
367 
368 	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
369 	if (rc)
370 		goto out;
371 	rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
372 	if (rc)
373 		goto out;
374 
375 	if (!pdev)
376 		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
377 
378 	cdev_init(&chip->cdev, &tpm_fops);
379 	cdev_init(&chip->cdevs, &tpmrm_fops);
380 	chip->cdev.owner = THIS_MODULE;
381 	chip->cdevs.owner = THIS_MODULE;
382 
383 	chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
384 	if (!chip->work_space.context_buf) {
385 		rc = -ENOMEM;
386 		goto out;
387 	}
388 	chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
389 	if (!chip->work_space.session_buf) {
390 		rc = -ENOMEM;
391 		goto out;
392 	}
393 
394 	chip->locality = -1;
395 	return chip;
396 
397 out:
398 	put_device(&chip->devs);
399 	put_device(&chip->dev);
400 	return ERR_PTR(rc);
401 }
402 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
403 
404 /**
405  * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
406  * @pdev: parent device to which the chip is associated
407  * @ops: struct tpm_class_ops instance
408  *
409  * Same as tpm_chip_alloc except devm is used to do the put_device
410  */
411 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
412 				 const struct tpm_class_ops *ops)
413 {
414 	struct tpm_chip *chip;
415 	int rc;
416 
417 	chip = tpm_chip_alloc(pdev, ops);
418 	if (IS_ERR(chip))
419 		return chip;
420 
421 	rc = devm_add_action_or_reset(pdev,
422 				      (void (*)(void *)) put_device,
423 				      &chip->dev);
424 	if (rc)
425 		return ERR_PTR(rc);
426 
427 	dev_set_drvdata(pdev, chip);
428 
429 	return chip;
430 }
431 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
432 
433 static int tpm_add_char_device(struct tpm_chip *chip)
434 {
435 	int rc;
436 
437 	rc = cdev_device_add(&chip->cdev, &chip->dev);
438 	if (rc) {
439 		dev_err(&chip->dev,
440 			"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
441 			dev_name(&chip->dev), MAJOR(chip->dev.devt),
442 			MINOR(chip->dev.devt), rc);
443 		return rc;
444 	}
445 
446 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
447 		rc = cdev_device_add(&chip->cdevs, &chip->devs);
448 		if (rc) {
449 			dev_err(&chip->devs,
450 				"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
451 				dev_name(&chip->devs), MAJOR(chip->devs.devt),
452 				MINOR(chip->devs.devt), rc);
453 			return rc;
454 		}
455 	}
456 
457 	/* Make the chip available. */
458 	mutex_lock(&idr_lock);
459 	idr_replace(&dev_nums_idr, chip, chip->dev_num);
460 	mutex_unlock(&idr_lock);
461 
462 	return rc;
463 }
464 
465 static void tpm_del_char_device(struct tpm_chip *chip)
466 {
467 	cdev_device_del(&chip->cdev, &chip->dev);
468 
469 	/* Make the chip unavailable. */
470 	mutex_lock(&idr_lock);
471 	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
472 	mutex_unlock(&idr_lock);
473 
474 	/* Make the driver uncallable. */
475 	down_write(&chip->ops_sem);
476 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
477 		if (!tpm_chip_start(chip)) {
478 			tpm2_shutdown(chip, TPM2_SU_CLEAR);
479 			tpm_chip_stop(chip);
480 		}
481 	}
482 	chip->ops = NULL;
483 	up_write(&chip->ops_sem);
484 }
485 
486 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
487 {
488 	struct attribute **i;
489 
490 	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
491 		return;
492 
493 	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
494 
495 	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
496 		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
497 }
498 
499 /* For compatibility with legacy sysfs paths we provide symlinks from the
500  * parent dev directory to selected names within the tpm chip directory. Old
501  * kernel versions created these files directly under the parent.
502  */
503 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
504 {
505 	struct attribute **i;
506 	int rc;
507 
508 	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
509 		return 0;
510 
511 	rc = __compat_only_sysfs_link_entry_to_kobj(
512 		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
513 	if (rc && rc != -ENOENT)
514 		return rc;
515 
516 	/* All the names from tpm-sysfs */
517 	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
518 		rc = __compat_only_sysfs_link_entry_to_kobj(
519 		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
520 		if (rc) {
521 			tpm_del_legacy_sysfs(chip);
522 			return rc;
523 		}
524 	}
525 
526 	return 0;
527 }
528 
529 static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
530 {
531 	struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
532 
533 	return tpm_get_random(chip, data, max);
534 }
535 
536 static int tpm_add_hwrng(struct tpm_chip *chip)
537 {
538 	if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
539 		return 0;
540 
541 	snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
542 		 "tpm-rng-%d", chip->dev_num);
543 	chip->hwrng.name = chip->hwrng_name;
544 	chip->hwrng.read = tpm_hwrng_read;
545 	return hwrng_register(&chip->hwrng);
546 }
547 
548 /*
549  * tpm_chip_register() - create a character device for the TPM chip
550  * @chip: TPM chip to use.
551  *
552  * Creates a character device for the TPM chip and adds sysfs attributes for
553  * the device. As the last step this function adds the chip to the list of TPM
554  * chips available for in-kernel use.
555  *
556  * This function should be only called after the chip initialization is
557  * complete.
558  */
559 int tpm_chip_register(struct tpm_chip *chip)
560 {
561 	int rc;
562 
563 	rc = tpm_chip_start(chip);
564 	if (rc)
565 		return rc;
566 	rc = tpm_auto_startup(chip);
567 	tpm_chip_stop(chip);
568 	if (rc)
569 		return rc;
570 
571 	tpm_sysfs_add_device(chip);
572 
573 	rc = tpm_bios_log_setup(chip);
574 	if (rc != 0 && rc != -ENODEV)
575 		return rc;
576 
577 	tpm_add_ppi(chip);
578 
579 	rc = tpm_add_hwrng(chip);
580 	if (rc)
581 		goto out_ppi;
582 
583 	rc = tpm_add_char_device(chip);
584 	if (rc)
585 		goto out_hwrng;
586 
587 	rc = tpm_add_legacy_sysfs(chip);
588 	if (rc) {
589 		tpm_chip_unregister(chip);
590 		return rc;
591 	}
592 
593 	return 0;
594 
595 out_hwrng:
596 	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
597 		hwrng_unregister(&chip->hwrng);
598 out_ppi:
599 	tpm_bios_log_teardown(chip);
600 
601 	return rc;
602 }
603 EXPORT_SYMBOL_GPL(tpm_chip_register);
604 
605 /*
606  * tpm_chip_unregister() - release the TPM driver
607  * @chip: TPM chip to use.
608  *
609  * Takes the chip first away from the list of available TPM chips and then
610  * cleans up all the resources reserved by tpm_chip_register().
611  *
612  * Once this function returns the driver call backs in 'op's will not be
613  * running and will no longer start.
614  *
615  * NOTE: This function should be only called before deinitializing chip
616  * resources.
617  */
618 void tpm_chip_unregister(struct tpm_chip *chip)
619 {
620 	tpm_del_legacy_sysfs(chip);
621 	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
622 		hwrng_unregister(&chip->hwrng);
623 	tpm_bios_log_teardown(chip);
624 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
625 		cdev_device_del(&chip->cdevs, &chip->devs);
626 	tpm_del_char_device(chip);
627 }
628 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
629