tpm-chip.c 15 KB

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