Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2004 IBM Corporation
0004  * Copyright (C) 2014 Intel Corporation
0005  *
0006  * Authors:
0007  * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
0008  * Leendert van Doorn <leendert@watson.ibm.com>
0009  * Dave Safford <safford@watson.ibm.com>
0010  * Reiner Sailer <sailer@watson.ibm.com>
0011  * Kylene Hall <kjhall@us.ibm.com>
0012  *
0013  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
0014  *
0015  * TPM chip management routines.
0016  */
0017 
0018 #include <linux/poll.h>
0019 #include <linux/slab.h>
0020 #include <linux/mutex.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/freezer.h>
0023 #include <linux/major.h>
0024 #include <linux/tpm_eventlog.h>
0025 #include <linux/hw_random.h>
0026 #include "tpm.h"
0027 
0028 DEFINE_IDR(dev_nums_idr);
0029 static DEFINE_MUTEX(idr_lock);
0030 
0031 struct class *tpm_class;
0032 struct class *tpmrm_class;
0033 dev_t tpm_devt;
0034 
0035 static int tpm_request_locality(struct tpm_chip *chip)
0036 {
0037     int rc;
0038 
0039     if (!chip->ops->request_locality)
0040         return 0;
0041 
0042     rc = chip->ops->request_locality(chip, 0);
0043     if (rc < 0)
0044         return rc;
0045 
0046     chip->locality = rc;
0047     return 0;
0048 }
0049 
0050 static void tpm_relinquish_locality(struct tpm_chip *chip)
0051 {
0052     int rc;
0053 
0054     if (!chip->ops->relinquish_locality)
0055         return;
0056 
0057     rc = chip->ops->relinquish_locality(chip, chip->locality);
0058     if (rc)
0059         dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
0060 
0061     chip->locality = -1;
0062 }
0063 
0064 static int tpm_cmd_ready(struct tpm_chip *chip)
0065 {
0066     if (!chip->ops->cmd_ready)
0067         return 0;
0068 
0069     return chip->ops->cmd_ready(chip);
0070 }
0071 
0072 static int tpm_go_idle(struct tpm_chip *chip)
0073 {
0074     if (!chip->ops->go_idle)
0075         return 0;
0076 
0077     return chip->ops->go_idle(chip);
0078 }
0079 
0080 static void tpm_clk_enable(struct tpm_chip *chip)
0081 {
0082     if (chip->ops->clk_enable)
0083         chip->ops->clk_enable(chip, true);
0084 }
0085 
0086 static void tpm_clk_disable(struct tpm_chip *chip)
0087 {
0088     if (chip->ops->clk_enable)
0089         chip->ops->clk_enable(chip, false);
0090 }
0091 
0092 /**
0093  * tpm_chip_start() - power on the TPM
0094  * @chip:   a TPM chip to use
0095  *
0096  * Return:
0097  * * The response length    - OK
0098  * * -errno         - A system error
0099  */
0100 int tpm_chip_start(struct tpm_chip *chip)
0101 {
0102     int ret;
0103 
0104     tpm_clk_enable(chip);
0105 
0106     if (chip->locality == -1) {
0107         ret = tpm_request_locality(chip);
0108         if (ret) {
0109             tpm_clk_disable(chip);
0110             return ret;
0111         }
0112     }
0113 
0114     ret = tpm_cmd_ready(chip);
0115     if (ret) {
0116         tpm_relinquish_locality(chip);
0117         tpm_clk_disable(chip);
0118         return ret;
0119     }
0120 
0121     return 0;
0122 }
0123 EXPORT_SYMBOL_GPL(tpm_chip_start);
0124 
0125 /**
0126  * tpm_chip_stop() - power off the TPM
0127  * @chip:   a TPM chip to use
0128  *
0129  * Return:
0130  * * The response length    - OK
0131  * * -errno         - A system error
0132  */
0133 void tpm_chip_stop(struct tpm_chip *chip)
0134 {
0135     tpm_go_idle(chip);
0136     tpm_relinquish_locality(chip);
0137     tpm_clk_disable(chip);
0138 }
0139 EXPORT_SYMBOL_GPL(tpm_chip_stop);
0140 
0141 /**
0142  * tpm_try_get_ops() - Get a ref to the tpm_chip
0143  * @chip: Chip to ref
0144  *
0145  * The caller must already have some kind of locking to ensure that chip is
0146  * valid. This function will lock the chip so that the ops member can be
0147  * accessed safely. The locking prevents tpm_chip_unregister from
0148  * completing, so it should not be held for long periods.
0149  *
0150  * Returns -ERRNO if the chip could not be got.
0151  */
0152 int tpm_try_get_ops(struct tpm_chip *chip)
0153 {
0154     int rc = -EIO;
0155 
0156     get_device(&chip->dev);
0157 
0158     down_read(&chip->ops_sem);
0159     if (!chip->ops)
0160         goto out_ops;
0161 
0162     mutex_lock(&chip->tpm_mutex);
0163     rc = tpm_chip_start(chip);
0164     if (rc)
0165         goto out_lock;
0166 
0167     return 0;
0168 out_lock:
0169     mutex_unlock(&chip->tpm_mutex);
0170 out_ops:
0171     up_read(&chip->ops_sem);
0172     put_device(&chip->dev);
0173     return rc;
0174 }
0175 EXPORT_SYMBOL_GPL(tpm_try_get_ops);
0176 
0177 /**
0178  * tpm_put_ops() - Release a ref to the tpm_chip
0179  * @chip: Chip to put
0180  *
0181  * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
0182  * be kfree'd.
0183  */
0184 void tpm_put_ops(struct tpm_chip *chip)
0185 {
0186     tpm_chip_stop(chip);
0187     mutex_unlock(&chip->tpm_mutex);
0188     up_read(&chip->ops_sem);
0189     put_device(&chip->dev);
0190 }
0191 EXPORT_SYMBOL_GPL(tpm_put_ops);
0192 
0193 /**
0194  * tpm_default_chip() - find a TPM chip and get a reference to it
0195  */
0196 struct tpm_chip *tpm_default_chip(void)
0197 {
0198     struct tpm_chip *chip, *res = NULL;
0199     int chip_num = 0;
0200     int chip_prev;
0201 
0202     mutex_lock(&idr_lock);
0203 
0204     do {
0205         chip_prev = chip_num;
0206         chip = idr_get_next(&dev_nums_idr, &chip_num);
0207         if (chip) {
0208             get_device(&chip->dev);
0209             res = chip;
0210             break;
0211         }
0212     } while (chip_prev != chip_num);
0213 
0214     mutex_unlock(&idr_lock);
0215 
0216     return res;
0217 }
0218 EXPORT_SYMBOL_GPL(tpm_default_chip);
0219 
0220 /**
0221  * tpm_find_get_ops() - find and reserve a TPM chip
0222  * @chip:   a &struct tpm_chip instance, %NULL for the default chip
0223  *
0224  * Finds a TPM chip and reserves its class device and operations. The chip must
0225  * be released with tpm_put_ops() after use.
0226  * This function is for internal use only. It supports existing TPM callers
0227  * by accepting NULL, but those callers should be converted to pass in a chip
0228  * directly.
0229  *
0230  * Return:
0231  * A reserved &struct tpm_chip instance.
0232  * %NULL if a chip is not found.
0233  * %NULL if the chip is not available.
0234  */
0235 struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
0236 {
0237     int rc;
0238 
0239     if (chip) {
0240         if (!tpm_try_get_ops(chip))
0241             return chip;
0242         return NULL;
0243     }
0244 
0245     chip = tpm_default_chip();
0246     if (!chip)
0247         return NULL;
0248     rc = tpm_try_get_ops(chip);
0249     /* release additional reference we got from tpm_default_chip() */
0250     put_device(&chip->dev);
0251     if (rc)
0252         return NULL;
0253     return chip;
0254 }
0255 
0256 /**
0257  * tpm_dev_release() - free chip memory and the device number
0258  * @dev: the character device for the TPM chip
0259  *
0260  * This is used as the release function for the character device.
0261  */
0262 static void tpm_dev_release(struct device *dev)
0263 {
0264     struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
0265 
0266     mutex_lock(&idr_lock);
0267     idr_remove(&dev_nums_idr, chip->dev_num);
0268     mutex_unlock(&idr_lock);
0269 
0270     kfree(chip->log.bios_event_log);
0271     kfree(chip->work_space.context_buf);
0272     kfree(chip->work_space.session_buf);
0273     kfree(chip->allocated_banks);
0274     kfree(chip);
0275 }
0276 
0277 /**
0278  * tpm_class_shutdown() - prepare the TPM device for loss of power.
0279  * @dev: device to which the chip is associated.
0280  *
0281  * Issues a TPM2_Shutdown command prior to loss of power, as required by the
0282  * TPM 2.0 spec. Then, calls bus- and device- specific shutdown code.
0283  *
0284  * Return: always 0 (i.e. success)
0285  */
0286 static int tpm_class_shutdown(struct device *dev)
0287 {
0288     struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
0289 
0290     down_write(&chip->ops_sem);
0291     if (chip->flags & TPM_CHIP_FLAG_TPM2) {
0292         if (!tpm_chip_start(chip)) {
0293             tpm2_shutdown(chip, TPM2_SU_CLEAR);
0294             tpm_chip_stop(chip);
0295         }
0296     }
0297     chip->ops = NULL;
0298     up_write(&chip->ops_sem);
0299 
0300     return 0;
0301 }
0302 
0303 /**
0304  * tpm_chip_alloc() - allocate a new struct tpm_chip instance
0305  * @pdev: device to which the chip is associated
0306  *        At this point pdev mst be initialized, but does not have to
0307  *        be registered
0308  * @ops: struct tpm_class_ops instance
0309  *
0310  * Allocates a new struct tpm_chip instance and assigns a free
0311  * device number for it. Must be paired with put_device(&chip->dev).
0312  */
0313 struct tpm_chip *tpm_chip_alloc(struct device *pdev,
0314                 const struct tpm_class_ops *ops)
0315 {
0316     struct tpm_chip *chip;
0317     int rc;
0318 
0319     chip = kzalloc(sizeof(*chip), GFP_KERNEL);
0320     if (chip == NULL)
0321         return ERR_PTR(-ENOMEM);
0322 
0323     mutex_init(&chip->tpm_mutex);
0324     init_rwsem(&chip->ops_sem);
0325 
0326     chip->ops = ops;
0327 
0328     mutex_lock(&idr_lock);
0329     rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
0330     mutex_unlock(&idr_lock);
0331     if (rc < 0) {
0332         dev_err(pdev, "No available tpm device numbers\n");
0333         kfree(chip);
0334         return ERR_PTR(rc);
0335     }
0336     chip->dev_num = rc;
0337 
0338     device_initialize(&chip->dev);
0339 
0340     chip->dev.class = tpm_class;
0341     chip->dev.class->shutdown_pre = tpm_class_shutdown;
0342     chip->dev.release = tpm_dev_release;
0343     chip->dev.parent = pdev;
0344     chip->dev.groups = chip->groups;
0345 
0346     if (chip->dev_num == 0)
0347         chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
0348     else
0349         chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
0350 
0351     rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
0352     if (rc)
0353         goto out;
0354 
0355     if (!pdev)
0356         chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
0357 
0358     cdev_init(&chip->cdev, &tpm_fops);
0359     chip->cdev.owner = THIS_MODULE;
0360 
0361     rc = tpm2_init_space(&chip->work_space, TPM2_SPACE_BUFFER_SIZE);
0362     if (rc) {
0363         rc = -ENOMEM;
0364         goto out;
0365     }
0366 
0367     chip->locality = -1;
0368     return chip;
0369 
0370 out:
0371     put_device(&chip->dev);
0372     return ERR_PTR(rc);
0373 }
0374 EXPORT_SYMBOL_GPL(tpm_chip_alloc);
0375 
0376 /**
0377  * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
0378  * @pdev: parent device to which the chip is associated
0379  * @ops: struct tpm_class_ops instance
0380  *
0381  * Same as tpm_chip_alloc except devm is used to do the put_device
0382  */
0383 struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
0384                  const struct tpm_class_ops *ops)
0385 {
0386     struct tpm_chip *chip;
0387     int rc;
0388 
0389     chip = tpm_chip_alloc(pdev, ops);
0390     if (IS_ERR(chip))
0391         return chip;
0392 
0393     rc = devm_add_action_or_reset(pdev,
0394                       (void (*)(void *)) put_device,
0395                       &chip->dev);
0396     if (rc)
0397         return ERR_PTR(rc);
0398 
0399     dev_set_drvdata(pdev, chip);
0400 
0401     return chip;
0402 }
0403 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
0404 
0405 static int tpm_add_char_device(struct tpm_chip *chip)
0406 {
0407     int rc;
0408 
0409     rc = cdev_device_add(&chip->cdev, &chip->dev);
0410     if (rc) {
0411         dev_err(&chip->dev,
0412             "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
0413             dev_name(&chip->dev), MAJOR(chip->dev.devt),
0414             MINOR(chip->dev.devt), rc);
0415         return rc;
0416     }
0417 
0418     if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip)) {
0419         rc = tpm_devs_add(chip);
0420         if (rc)
0421             goto err_del_cdev;
0422     }
0423 
0424     /* Make the chip available. */
0425     mutex_lock(&idr_lock);
0426     idr_replace(&dev_nums_idr, chip, chip->dev_num);
0427     mutex_unlock(&idr_lock);
0428 
0429     return 0;
0430 
0431 err_del_cdev:
0432     cdev_device_del(&chip->cdev, &chip->dev);
0433     return rc;
0434 }
0435 
0436 static void tpm_del_char_device(struct tpm_chip *chip)
0437 {
0438     cdev_device_del(&chip->cdev, &chip->dev);
0439 
0440     /* Make the chip unavailable. */
0441     mutex_lock(&idr_lock);
0442     idr_replace(&dev_nums_idr, NULL, chip->dev_num);
0443     mutex_unlock(&idr_lock);
0444 
0445     /* Make the driver uncallable. */
0446     down_write(&chip->ops_sem);
0447 
0448     /*
0449      * Check if chip->ops is still valid: In case that the controller
0450      * drivers shutdown handler unregisters the controller in its
0451      * shutdown handler we are called twice and chip->ops to NULL.
0452      */
0453     if (chip->ops) {
0454         if (chip->flags & TPM_CHIP_FLAG_TPM2) {
0455             if (!tpm_chip_start(chip)) {
0456                 tpm2_shutdown(chip, TPM2_SU_CLEAR);
0457                 tpm_chip_stop(chip);
0458             }
0459         }
0460         chip->ops = NULL;
0461     }
0462     up_write(&chip->ops_sem);
0463 }
0464 
0465 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
0466 {
0467     struct attribute **i;
0468 
0469     if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) ||
0470         tpm_is_firmware_upgrade(chip))
0471         return;
0472 
0473     sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
0474 
0475     for (i = chip->groups[0]->attrs; *i != NULL; ++i)
0476         sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
0477 }
0478 
0479 /* For compatibility with legacy sysfs paths we provide symlinks from the
0480  * parent dev directory to selected names within the tpm chip directory. Old
0481  * kernel versions created these files directly under the parent.
0482  */
0483 static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
0484 {
0485     struct attribute **i;
0486     int rc;
0487 
0488     if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL) ||
0489         tpm_is_firmware_upgrade(chip))
0490         return 0;
0491 
0492     rc = compat_only_sysfs_link_entry_to_kobj(
0493             &chip->dev.parent->kobj, &chip->dev.kobj, "ppi", NULL);
0494     if (rc && rc != -ENOENT)
0495         return rc;
0496 
0497     /* All the names from tpm-sysfs */
0498     for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
0499         rc = compat_only_sysfs_link_entry_to_kobj(
0500             &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name, NULL);
0501         if (rc) {
0502             tpm_del_legacy_sysfs(chip);
0503             return rc;
0504         }
0505     }
0506 
0507     return 0;
0508 }
0509 
0510 static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
0511 {
0512     struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
0513 
0514     return tpm_get_random(chip, data, max);
0515 }
0516 
0517 static int tpm_add_hwrng(struct tpm_chip *chip)
0518 {
0519     if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM) || tpm_is_firmware_upgrade(chip))
0520         return 0;
0521 
0522     snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
0523          "tpm-rng-%d", chip->dev_num);
0524     chip->hwrng.name = chip->hwrng_name;
0525     chip->hwrng.read = tpm_hwrng_read;
0526     return hwrng_register(&chip->hwrng);
0527 }
0528 
0529 static int tpm_get_pcr_allocation(struct tpm_chip *chip)
0530 {
0531     int rc;
0532 
0533     if (tpm_is_firmware_upgrade(chip))
0534         return 0;
0535 
0536     rc = (chip->flags & TPM_CHIP_FLAG_TPM2) ?
0537          tpm2_get_pcr_allocation(chip) :
0538          tpm1_get_pcr_allocation(chip);
0539 
0540     if (rc > 0)
0541         return -ENODEV;
0542 
0543     return rc;
0544 }
0545 
0546 /*
0547  * tpm_chip_register() - create a character device for the TPM chip
0548  * @chip: TPM chip to use.
0549  *
0550  * Creates a character device for the TPM chip and adds sysfs attributes for
0551  * the device. As the last step this function adds the chip to the list of TPM
0552  * chips available for in-kernel use.
0553  *
0554  * This function should be only called after the chip initialization is
0555  * complete.
0556  */
0557 int tpm_chip_register(struct tpm_chip *chip)
0558 {
0559     int rc;
0560 
0561     rc = tpm_chip_start(chip);
0562     if (rc)
0563         return rc;
0564     rc = tpm_auto_startup(chip);
0565     if (rc) {
0566         tpm_chip_stop(chip);
0567         return rc;
0568     }
0569 
0570     rc = tpm_get_pcr_allocation(chip);
0571     tpm_chip_stop(chip);
0572     if (rc)
0573         return rc;
0574 
0575     tpm_sysfs_add_device(chip);
0576 
0577     tpm_bios_log_setup(chip);
0578 
0579     tpm_add_ppi(chip);
0580 
0581     rc = tpm_add_hwrng(chip);
0582     if (rc)
0583         goto out_ppi;
0584 
0585     rc = tpm_add_char_device(chip);
0586     if (rc)
0587         goto out_hwrng;
0588 
0589     rc = tpm_add_legacy_sysfs(chip);
0590     if (rc) {
0591         tpm_chip_unregister(chip);
0592         return rc;
0593     }
0594 
0595     return 0;
0596 
0597 out_hwrng:
0598     if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
0599         hwrng_unregister(&chip->hwrng);
0600 out_ppi:
0601     tpm_bios_log_teardown(chip);
0602 
0603     return rc;
0604 }
0605 EXPORT_SYMBOL_GPL(tpm_chip_register);
0606 
0607 /*
0608  * tpm_chip_unregister() - release the TPM driver
0609  * @chip: TPM chip to use.
0610  *
0611  * Takes the chip first away from the list of available TPM chips and then
0612  * cleans up all the resources reserved by tpm_chip_register().
0613  *
0614  * Once this function returns the driver call backs in 'op's will not be
0615  * running and will no longer start.
0616  *
0617  * NOTE: This function should be only called before deinitializing chip
0618  * resources.
0619  */
0620 void tpm_chip_unregister(struct tpm_chip *chip)
0621 {
0622     tpm_del_legacy_sysfs(chip);
0623     if (IS_ENABLED(CONFIG_HW_RANDOM_TPM) && !tpm_is_firmware_upgrade(chip))
0624         hwrng_unregister(&chip->hwrng);
0625     tpm_bios_log_teardown(chip);
0626     if (chip->flags & TPM_CHIP_FLAG_TPM2 && !tpm_is_firmware_upgrade(chip))
0627         tpm_devs_remove(chip);
0628     tpm_del_char_device(chip);
0629 }
0630 EXPORT_SYMBOL_GPL(tpm_chip_unregister);