tpm-chip.c 14 KB

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