tpm-chip.c 15 KB

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