tpm-chip.c 15 KB

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