tpm-chip.c 14.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2004 IBM Corporation
 * Copyright (C) 2014 Intel Corporation
 *
 * Authors:
 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
 * Leendert van Doorn <leendert@watson.ibm.com>
 * Dave Safford <safford@watson.ibm.com>
 * Reiner Sailer <sailer@watson.ibm.com>
 * Kylene Hall <kjhall@us.ibm.com>
 *
 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
 *
 * TPM chip management routines.
 */

#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/freezer.h>
J
Jarkko Sakkinen 已提交
23
#include <linux/major.h>
24
#include <linux/tpm_eventlog.h>
25
#include <linux/hw_random.h>
26 27
#include "tpm.h"

28 29
DEFINE_IDR(dev_nums_idr);
static DEFINE_MUTEX(idr_lock);
30

J
Jarkko Sakkinen 已提交
31
struct class *tpm_class;
32
struct class *tpmrm_class;
J
Jarkko Sakkinen 已提交
33 34
dev_t tpm_devt;

35
static int tpm_request_locality(struct tpm_chip *chip)
36 37 38 39 40 41 42 43 44 45 46 47 48 49
{
	int rc;

	if (!chip->ops->request_locality)
		return 0;

	rc = chip->ops->request_locality(chip, 0);
	if (rc < 0)
		return rc;

	chip->locality = rc;
	return 0;
}

50
static void tpm_relinquish_locality(struct tpm_chip *chip)
51 52 53 54 55 56 57 58 59 60 61 62 63
{
	int rc;

	if (!chip->ops->relinquish_locality)
		return;

	rc = chip->ops->relinquish_locality(chip, chip->locality);
	if (rc)
		dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);

	chip->locality = -1;
}

64
static int tpm_cmd_ready(struct tpm_chip *chip)
65 66 67 68 69 70 71
{
	if (!chip->ops->cmd_ready)
		return 0;

	return chip->ops->cmd_ready(chip);
}

72
static int tpm_go_idle(struct tpm_chip *chip)
73 74 75 76 77 78 79
{
	if (!chip->ops->go_idle)
		return 0;

	return chip->ops->go_idle(chip);
}

80 81 82 83 84 85 86 87 88 89 90 91
static void tpm_clk_enable(struct tpm_chip *chip)
{
	if (chip->ops->clk_enable)
		chip->ops->clk_enable(chip, true);
}

static void tpm_clk_disable(struct tpm_chip *chip)
{
	if (chip->ops->clk_enable)
		chip->ops->clk_enable(chip, false);
}

92 93 94 95 96 97 98 99
/**
 * tpm_chip_start() - power on the TPM
 * @chip:	a TPM chip to use
 *
 * Return:
 * * The response length	- OK
 * * -errno			- A system error
 */
100
int tpm_chip_start(struct tpm_chip *chip)
101 102 103
{
	int ret;

104
	tpm_clk_enable(chip);
105 106

	if (chip->locality == -1) {
107
		ret = tpm_request_locality(chip);
108
		if (ret) {
109
			tpm_clk_disable(chip);
110 111 112 113
			return ret;
		}
	}

114
	ret = tpm_cmd_ready(chip);
115
	if (ret) {
116
		tpm_relinquish_locality(chip);
117
		tpm_clk_disable(chip);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		return ret;
	}

	return 0;
}
EXPORT_SYMBOL_GPL(tpm_chip_start);

/**
 * tpm_chip_stop() - power off the TPM
 * @chip:	a TPM chip to use
 *
 * Return:
 * * The response length	- OK
 * * -errno			- A system error
 */
133
void tpm_chip_stop(struct tpm_chip *chip)
134
{
135 136
	tpm_go_idle(chip);
	tpm_relinquish_locality(chip);
137
	tpm_clk_disable(chip);
138 139 140
}
EXPORT_SYMBOL_GPL(tpm_chip_stop);

141 142 143 144 145 146 147 148 149 150
/**
 * tpm_try_get_ops() - Get a ref to the tpm_chip
 * @chip: Chip to ref
 *
 * The caller must already have some kind of locking to ensure that chip is
 * valid. This function will lock the chip so that the ops member can be
 * accessed safely. The locking prevents tpm_chip_unregister from
 * completing, so it should not be held for long periods.
 *
 * Returns -ERRNO if the chip could not be got.
151
 */
152 153 154 155 156 157 158 159
int tpm_try_get_ops(struct tpm_chip *chip)
{
	int rc = -EIO;

	get_device(&chip->dev);

	down_read(&chip->ops_sem);
	if (!chip->ops)
160
		goto out_ops;
161

162
	mutex_lock(&chip->tpm_mutex);
163
	rc = tpm_chip_start(chip);
164 165 166
	if (rc)
		goto out_lock;

167 168
	return 0;
out_lock:
169 170
	mutex_unlock(&chip->tpm_mutex);
out_ops:
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	up_read(&chip->ops_sem);
	put_device(&chip->dev);
	return rc;
}
EXPORT_SYMBOL_GPL(tpm_try_get_ops);

/**
 * tpm_put_ops() - Release a ref to the tpm_chip
 * @chip: Chip to put
 *
 * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
 * be kfree'd.
 */
void tpm_put_ops(struct tpm_chip *chip)
{
186
	tpm_chip_stop(chip);
187
	mutex_unlock(&chip->tpm_mutex);
188 189 190 191 192
	up_read(&chip->ops_sem);
	put_device(&chip->dev);
}
EXPORT_SYMBOL_GPL(tpm_put_ops);

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
/**
 * tpm_default_chip() - find a TPM chip and get a reference to it
 */
struct tpm_chip *tpm_default_chip(void)
{
	struct tpm_chip *chip, *res = NULL;
	int chip_num = 0;
	int chip_prev;

	mutex_lock(&idr_lock);

	do {
		chip_prev = chip_num;
		chip = idr_get_next(&dev_nums_idr, &chip_num);
		if (chip) {
			get_device(&chip->dev);
			res = chip;
			break;
		}
	} while (chip_prev != chip_num);

	mutex_unlock(&idr_lock);

	return res;
}
EXPORT_SYMBOL_GPL(tpm_default_chip);

220
/**
221
 * tpm_find_get_ops() - find and reserve a TPM chip
222
 * @chip:	a &struct tpm_chip instance, %NULL for the default chip
223
 *
224
 * Finds a TPM chip and reserves its class device and operations. The chip must
225 226 227 228
 * be released with tpm_put_ops() after use.
 * This function is for internal use only. It supports existing TPM callers
 * by accepting NULL, but those callers should be converted to pass in a chip
 * directly.
229 230 231 232 233
 *
 * Return:
 * A reserved &struct tpm_chip instance.
 * %NULL if a chip is not found.
 * %NULL if the chip is not available.
234
 */
235
struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
236
{
237
	int rc;
238

239
	if (chip) {
240
		if (!tpm_try_get_ops(chip))
241 242
			return chip;
		return NULL;
243
	}
244

245 246 247 248 249 250 251 252 253
	chip = tpm_default_chip();
	if (!chip)
		return NULL;
	rc = tpm_try_get_ops(chip);
	/* release additional reference we got from tpm_default_chip() */
	put_device(&chip->dev);
	if (rc)
		return NULL;
	return chip;
254 255 256
}

/**
J
Jarkko Sakkinen 已提交
257 258
 * tpm_dev_release() - free chip memory and the device number
 * @dev: the character device for the TPM chip
259
 *
J
Jarkko Sakkinen 已提交
260
 * This is used as the release function for the character device.
261
 */
J
Jarkko Sakkinen 已提交
262
static void tpm_dev_release(struct device *dev)
263
{
J
Jarkko Sakkinen 已提交
264
	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
265

266 267 268 269
	mutex_lock(&idr_lock);
	idr_remove(&dev_nums_idr, chip->dev_num);
	mutex_unlock(&idr_lock);

N
Nayna Jain 已提交
270
	kfree(chip->log.bios_event_log);
271
	kfree(chip->work_space.context_buf);
272
	kfree(chip->work_space.session_buf);
273
	kfree(chip->allocated_banks);
274 275 276
	kfree(chip);
}

277 278 279 280 281 282 283 284
static void tpm_devs_release(struct device *dev)
{
	struct tpm_chip *chip = container_of(dev, struct tpm_chip, devs);

	/* release the master device reference */
	put_device(&chip->dev);
}

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
/**
 * tpm_class_shutdown() - prepare the TPM device for loss of power.
 * @dev: device to which the chip is associated.
 *
 * Issues a TPM2_Shutdown command prior to loss of power, as required by the
 * TPM 2.0 spec.
 * Then, calls bus- and device- specific shutdown code.
 *
 * XXX: This codepath relies on the fact that sysfs is not enabled for
 * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
 * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
 */
static int tpm_class_shutdown(struct device *dev)
{
	struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);

301
	down_write(&chip->ops_sem);
302
	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
303
		if (!tpm_chip_start(chip)) {
304
			tpm2_shutdown(chip, TPM2_SU_CLEAR);
305
			tpm_chip_stop(chip);
306
		}
307
	}
308 309
	chip->ops = NULL;
	up_write(&chip->ops_sem);
310

311 312 313
	return 0;
}

314
/**
315 316 317 318
 * tpm_chip_alloc() - allocate a new struct tpm_chip instance
 * @pdev: device to which the chip is associated
 *        At this point pdev mst be initialized, but does not have to
 *        be registered
319 320 321
 * @ops: struct tpm_class_ops instance
 *
 * Allocates a new struct tpm_chip instance and assigns a free
322
 * device number for it. Must be paired with put_device(&chip->dev).
323
 */
324
struct tpm_chip *tpm_chip_alloc(struct device *pdev,
325
				const struct tpm_class_ops *ops)
326 327
{
	struct tpm_chip *chip;
328
	int rc;
329 330 331 332 333 334

	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
	if (chip == NULL)
		return ERR_PTR(-ENOMEM);

	mutex_init(&chip->tpm_mutex);
335
	init_rwsem(&chip->ops_sem);
336 337 338

	chip->ops = ops;

339 340 341 342
	mutex_lock(&idr_lock);
	rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
	mutex_unlock(&idr_lock);
	if (rc < 0) {
343
		dev_err(pdev, "No available tpm device numbers\n");
344
		kfree(chip);
345
		return ERR_PTR(rc);
346
	}
347
	chip->dev_num = rc;
348

J
Jason Gunthorpe 已提交
349
	device_initialize(&chip->dev);
350
	device_initialize(&chip->devs);
351

J
Jarkko Sakkinen 已提交
352
	chip->dev.class = tpm_class;
353
	chip->dev.class->shutdown_pre = tpm_class_shutdown;
J
Jarkko Sakkinen 已提交
354
	chip->dev.release = tpm_dev_release;
355
	chip->dev.parent = pdev;
356
	chip->dev.groups = chip->groups;
J
Jarkko Sakkinen 已提交
357

358 359 360 361 362 363
	chip->devs.parent = pdev;
	chip->devs.class = tpmrm_class;
	chip->devs.release = tpm_devs_release;
	/* get extra reference on main device to hold on
	 * behalf of devs.  This holds the chip structure
	 * while cdevs is in use.  The corresponding put
364
	 * is in the tpm_devs_release (TPM2 only)
365
	 */
366 367
	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		get_device(&chip->dev);
368

J
Jarkko Sakkinen 已提交
369 370 371 372 373
	if (chip->dev_num == 0)
		chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
	else
		chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);

374 375 376
	chip->devs.devt =
		MKDEV(MAJOR(tpm_devt), chip->dev_num + TPM_NUM_DEVICES);

J
Jason Gunthorpe 已提交
377
	rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
378 379 380
	if (rc)
		goto out;
	rc = dev_set_name(&chip->devs, "tpmrm%d", chip->dev_num);
J
Jason Gunthorpe 已提交
381 382
	if (rc)
		goto out;
J
Jarkko Sakkinen 已提交
383

384
	if (!pdev)
385 386
		chip->flags |= TPM_CHIP_FLAG_VIRTUAL;

J
Jarkko Sakkinen 已提交
387
	cdev_init(&chip->cdev, &tpm_fops);
388
	cdev_init(&chip->cdevs, &tpmrm_fops);
S
Stefan Berger 已提交
389
	chip->cdev.owner = THIS_MODULE;
390
	chip->cdevs.owner = THIS_MODULE;
J
Jarkko Sakkinen 已提交
391

392 393 394 395 396
	chip->work_space.context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (!chip->work_space.context_buf) {
		rc = -ENOMEM;
		goto out;
	}
397 398 399 400 401
	chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (!chip->work_space.session_buf) {
		rc = -ENOMEM;
		goto out;
	}
402

403
	chip->locality = -1;
404 405 406
	return chip;

out:
407
	put_device(&chip->devs);
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	put_device(&chip->dev);
	return ERR_PTR(rc);
}
EXPORT_SYMBOL_GPL(tpm_chip_alloc);

/**
 * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
 * @pdev: parent device to which the chip is associated
 * @ops: struct tpm_class_ops instance
 *
 * Same as tpm_chip_alloc except devm is used to do the put_device
 */
struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
				 const struct tpm_class_ops *ops)
{
	struct tpm_chip *chip;
	int rc;

	chip = tpm_chip_alloc(pdev, ops);
	if (IS_ERR(chip))
		return chip;

430 431 432 433
	rc = devm_add_action_or_reset(pdev,
				      (void (*)(void *)) put_device,
				      &chip->dev);
	if (rc)
434
		return ERR_PTR(rc);
435

436
	dev_set_drvdata(pdev, chip);
J
Jason Gunthorpe 已提交
437

438
	return chip;
439 440 441
}
EXPORT_SYMBOL_GPL(tpmm_chip_alloc);

442
static int tpm_add_char_device(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
443 444 445
{
	int rc;

446
	rc = cdev_device_add(&chip->cdev, &chip->dev);
J
Jarkko Sakkinen 已提交
447 448
	if (rc) {
		dev_err(&chip->dev,
449
			"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
J
Jason Gunthorpe 已提交
450
			dev_name(&chip->dev), MAJOR(chip->dev.devt),
J
Jarkko Sakkinen 已提交
451 452 453 454
			MINOR(chip->dev.devt), rc);
		return rc;
	}

455 456 457 458 459 460 461 462 463
	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
		rc = cdev_device_add(&chip->cdevs, &chip->devs);
		if (rc) {
			dev_err(&chip->devs,
				"unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
				dev_name(&chip->devs), MAJOR(chip->devs.devt),
				MINOR(chip->devs.devt), rc);
			return rc;
		}
464 465
	}

466 467 468 469 470
	/* Make the chip available. */
	mutex_lock(&idr_lock);
	idr_replace(&dev_nums_idr, chip, chip->dev_num);
	mutex_unlock(&idr_lock);

J
Jarkko Sakkinen 已提交
471 472 473
	return rc;
}

474
static void tpm_del_char_device(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
475
{
476
	cdev_device_del(&chip->cdev, &chip->dev);
477 478 479 480 481

	/* Make the chip unavailable. */
	mutex_lock(&idr_lock);
	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
	mutex_unlock(&idr_lock);
482 483 484

	/* Make the driver uncallable. */
	down_write(&chip->ops_sem);
485
	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
486
		if (!tpm_chip_start(chip)) {
487
			tpm2_shutdown(chip, TPM2_SU_CLEAR);
488
			tpm_chip_stop(chip);
489 490
		}
	}
491 492
	chip->ops = NULL;
	up_write(&chip->ops_sem);
J
Jarkko Sakkinen 已提交
493 494
}

495 496 497 498
static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
{
	struct attribute **i;

499
	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
500
		return;
501

502 503 504 505
	sysfs_remove_link(&chip->dev.parent->kobj, "ppi");

	for (i = chip->groups[0]->attrs; *i != NULL; ++i)
		sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
506 507
}

508 509 510 511 512 513 514 515 516
/* For compatibility with legacy sysfs paths we provide symlinks from the
 * parent dev directory to selected names within the tpm chip directory. Old
 * kernel versions created these files directly under the parent.
 */
static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
{
	struct attribute **i;
	int rc;

517
	if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
		return 0;

	rc = __compat_only_sysfs_link_entry_to_kobj(
		    &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
	if (rc && rc != -ENOENT)
		return rc;

	/* All the names from tpm-sysfs */
	for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
		rc = __compat_only_sysfs_link_entry_to_kobj(
		    &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
		if (rc) {
			tpm_del_legacy_sysfs(chip);
			return rc;
		}
	}

	return 0;
}
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
{
	struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);

	return tpm_get_random(chip, data, max);
}

static int tpm_add_hwrng(struct tpm_chip *chip)
{
	if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
		return 0;

	snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
		 "tpm-rng-%d", chip->dev_num);
	chip->hwrng.name = chip->hwrng_name;
	chip->hwrng.read = tpm_hwrng_read;
	return hwrng_register(&chip->hwrng);
}

557
/*
J
Jarkko Sakkinen 已提交
558
 * tpm_chip_register() - create a character device for the TPM chip
559 560
 * @chip: TPM chip to use.
 *
561 562 563
 * Creates a character device for the TPM chip and adds sysfs attributes for
 * the device. As the last step this function adds the chip to the list of TPM
 * chips available for in-kernel use.
564
 *
565 566
 * This function should be only called after the chip initialization is
 * complete.
567 568 569 570 571
 */
int tpm_chip_register(struct tpm_chip *chip)
{
	int rc;

572
	rc = tpm_chip_start(chip);
573 574
	if (rc)
		return rc;
575
	rc = tpm_auto_startup(chip);
576
	tpm_chip_stop(chip);
577 578
	if (rc)
		return rc;
579

580 581 582
	tpm_sysfs_add_device(chip);

	rc = tpm_bios_log_setup(chip);
583
	if (rc != 0 && rc != -ENODEV)
584
		return rc;
585

586 587
	tpm_add_ppi(chip);

588 589 590 591
	rc = tpm_add_hwrng(chip);
	if (rc)
		goto out_ppi;

592
	rc = tpm_add_char_device(chip);
593 594
	if (rc)
		goto out_hwrng;
595

596 597 598 599
	rc = tpm_add_legacy_sysfs(chip);
	if (rc) {
		tpm_chip_unregister(chip);
		return rc;
600 601
	}

602
	return 0;
603 604 605 606 607 608 609 610

out_hwrng:
	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
		hwrng_unregister(&chip->hwrng);
out_ppi:
	tpm_bios_log_teardown(chip);

	return rc;
611 612 613 614 615 616 617 618 619 620
}
EXPORT_SYMBOL_GPL(tpm_chip_register);

/*
 * tpm_chip_unregister() - release the TPM driver
 * @chip: TPM chip to use.
 *
 * Takes the chip first away from the list of available TPM chips and then
 * cleans up all the resources reserved by tpm_chip_register().
 *
621 622 623
 * Once this function returns the driver call backs in 'op's will not be
 * running and will no longer start.
 *
624 625 626 627 628
 * NOTE: This function should be only called before deinitializing chip
 * resources.
 */
void tpm_chip_unregister(struct tpm_chip *chip)
{
629
	tpm_del_legacy_sysfs(chip);
630 631
	if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
		hwrng_unregister(&chip->hwrng);
632
	tpm_bios_log_teardown(chip);
633 634
	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		cdev_device_del(&chip->cdevs, &chip->devs);
635
	tpm_del_char_device(chip);
636 637
}
EXPORT_SYMBOL_GPL(tpm_chip_unregister);