tpm-chip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. * Authors:
  6. * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  7. * Leendert van Doorn <leendert@watson.ibm.com>
  8. * Dave Safford <safford@watson.ibm.com>
  9. * Reiner Sailer <sailer@watson.ibm.com>
  10. * Kylene Hall <kjhall@us.ibm.com>
  11. *
  12. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  13. *
  14. * TPM chip management routines.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation, version 2 of the
  19. * License.
  20. *
  21. */
  22. #include <linux/poll.h>
  23. #include <linux/slab.h>
  24. #include <linux/mutex.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/freezer.h>
  27. #include <linux/major.h>
  28. #include <linux/of.h>
  29. #include "tpm.h"
  30. #include "tpm_eventlog.h"
  31. DEFINE_IDR(dev_nums_idr);
  32. static DEFINE_MUTEX(idr_lock);
  33. struct class *tpm_class;
  34. dev_t tpm_devt;
  35. /**
  36. * tpm_try_get_ops() - Get a ref to the tpm_chip
  37. * @chip: Chip to ref
  38. *
  39. * The caller must already have some kind of locking to ensure that chip is
  40. * valid. This function will lock the chip so that the ops member can be
  41. * accessed safely. The locking prevents tpm_chip_unregister from
  42. * completing, so it should not be held for long periods.
  43. *
  44. * Returns -ERRNO if the chip could not be got.
  45. */
  46. int tpm_try_get_ops(struct tpm_chip *chip)
  47. {
  48. int rc = -EIO;
  49. get_device(&chip->dev);
  50. down_read(&chip->ops_sem);
  51. if (!chip->ops)
  52. goto out_lock;
  53. return 0;
  54. out_lock:
  55. up_read(&chip->ops_sem);
  56. put_device(&chip->dev);
  57. return rc;
  58. }
  59. EXPORT_SYMBOL_GPL(tpm_try_get_ops);
  60. /**
  61. * tpm_put_ops() - Release a ref to the tpm_chip
  62. * @chip: Chip to put
  63. *
  64. * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
  65. * be kfree'd.
  66. */
  67. void tpm_put_ops(struct tpm_chip *chip)
  68. {
  69. up_read(&chip->ops_sem);
  70. put_device(&chip->dev);
  71. }
  72. EXPORT_SYMBOL_GPL(tpm_put_ops);
  73. /**
  74. * tpm_chip_find_get() - return tpm_chip for a given chip number
  75. * @chip_num: id to find
  76. *
  77. * The return'd chip has been tpm_try_get_ops'd and must be released via
  78. * tpm_put_ops
  79. */
  80. struct tpm_chip *tpm_chip_find_get(int chip_num)
  81. {
  82. struct tpm_chip *chip, *res = NULL;
  83. int chip_prev;
  84. mutex_lock(&idr_lock);
  85. if (chip_num == TPM_ANY_NUM) {
  86. chip_num = 0;
  87. do {
  88. chip_prev = chip_num;
  89. chip = idr_get_next(&dev_nums_idr, &chip_num);
  90. if (chip && !tpm_try_get_ops(chip)) {
  91. res = chip;
  92. break;
  93. }
  94. } while (chip_prev != chip_num);
  95. } else {
  96. chip = idr_find_slowpath(&dev_nums_idr, chip_num);
  97. if (chip && !tpm_try_get_ops(chip))
  98. res = chip;
  99. }
  100. mutex_unlock(&idr_lock);
  101. return res;
  102. }
  103. /**
  104. * tpm_dev_release() - free chip memory and the device number
  105. * @dev: the character device for the TPM chip
  106. *
  107. * This is used as the release function for the character device.
  108. */
  109. static void tpm_dev_release(struct device *dev)
  110. {
  111. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  112. mutex_lock(&idr_lock);
  113. idr_remove(&dev_nums_idr, chip->dev_num);
  114. mutex_unlock(&idr_lock);
  115. kfree(chip);
  116. }
  117. /**
  118. * tpm_class_shutdown() - prepare the TPM device for loss of power.
  119. * @dev: device to which the chip is associated.
  120. *
  121. * Issues a TPM2_Shutdown command prior to loss of power, as required by the
  122. * TPM 2.0 spec.
  123. * Then, calls bus- and device- specific shutdown code.
  124. *
  125. * XXX: This codepath relies on the fact that sysfs is not enabled for
  126. * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
  127. * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
  128. */
  129. static int tpm_class_shutdown(struct device *dev)
  130. {
  131. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  132. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  133. down_write(&chip->ops_sem);
  134. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  135. chip->ops = NULL;
  136. up_write(&chip->ops_sem);
  137. }
  138. /* Allow bus- and device-specific code to run. Note: since chip->ops
  139. * is NULL, more-specific shutdown code will not be able to issue TPM
  140. * commands.
  141. */
  142. if (dev->bus && dev->bus->shutdown)
  143. dev->bus->shutdown(dev);
  144. else if (dev->driver && dev->driver->shutdown)
  145. dev->driver->shutdown(dev);
  146. return 0;
  147. }
  148. /**
  149. * tpm_chip_alloc() - allocate a new struct tpm_chip instance
  150. * @pdev: device to which the chip is associated
  151. * At this point pdev mst be initialized, but does not have to
  152. * be registered
  153. * @ops: struct tpm_class_ops instance
  154. *
  155. * Allocates a new struct tpm_chip instance and assigns a free
  156. * device number for it. Must be paired with put_device(&chip->dev).
  157. */
  158. struct tpm_chip *tpm_chip_alloc(struct device *pdev,
  159. const struct tpm_class_ops *ops)
  160. {
  161. struct tpm_chip *chip;
  162. int rc;
  163. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  164. if (chip == NULL)
  165. return ERR_PTR(-ENOMEM);
  166. mutex_init(&chip->tpm_mutex);
  167. init_rwsem(&chip->ops_sem);
  168. chip->ops = ops;
  169. mutex_lock(&idr_lock);
  170. rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
  171. mutex_unlock(&idr_lock);
  172. if (rc < 0) {
  173. dev_err(pdev, "No available tpm device numbers\n");
  174. kfree(chip);
  175. return ERR_PTR(rc);
  176. }
  177. chip->dev_num = rc;
  178. device_initialize(&chip->dev);
  179. chip->dev.class = tpm_class;
  180. chip->dev.class->shutdown = tpm_class_shutdown;
  181. chip->dev.release = tpm_dev_release;
  182. chip->dev.parent = pdev;
  183. chip->dev.groups = chip->groups;
  184. if (chip->dev_num == 0)
  185. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  186. else
  187. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  188. rc = dev_set_name(&chip->dev, "tpm%d", chip->dev_num);
  189. if (rc)
  190. goto out;
  191. if (!pdev)
  192. chip->flags |= TPM_CHIP_FLAG_VIRTUAL;
  193. cdev_init(&chip->cdev, &tpm_fops);
  194. chip->cdev.owner = THIS_MODULE;
  195. chip->cdev.kobj.parent = &chip->dev.kobj;
  196. return chip;
  197. out:
  198. put_device(&chip->dev);
  199. return ERR_PTR(rc);
  200. }
  201. EXPORT_SYMBOL_GPL(tpm_chip_alloc);
  202. /**
  203. * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
  204. * @pdev: parent device to which the chip is associated
  205. * @ops: struct tpm_class_ops instance
  206. *
  207. * Same as tpm_chip_alloc except devm is used to do the put_device
  208. */
  209. struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
  210. const struct tpm_class_ops *ops)
  211. {
  212. struct tpm_chip *chip;
  213. int rc;
  214. chip = tpm_chip_alloc(pdev, ops);
  215. if (IS_ERR(chip))
  216. return chip;
  217. rc = devm_add_action_or_reset(pdev,
  218. (void (*)(void *)) put_device,
  219. &chip->dev);
  220. if (rc)
  221. return ERR_PTR(rc);
  222. dev_set_drvdata(pdev, chip);
  223. return chip;
  224. }
  225. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  226. static int tpm_add_char_device(struct tpm_chip *chip)
  227. {
  228. int rc;
  229. rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
  230. if (rc) {
  231. dev_err(&chip->dev,
  232. "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
  233. dev_name(&chip->dev), MAJOR(chip->dev.devt),
  234. MINOR(chip->dev.devt), rc);
  235. return rc;
  236. }
  237. rc = device_add(&chip->dev);
  238. if (rc) {
  239. dev_err(&chip->dev,
  240. "unable to device_register() %s, major %d, minor %d, err=%d\n",
  241. dev_name(&chip->dev), MAJOR(chip->dev.devt),
  242. MINOR(chip->dev.devt), rc);
  243. cdev_del(&chip->cdev);
  244. return rc;
  245. }
  246. /* Make the chip available. */
  247. mutex_lock(&idr_lock);
  248. idr_replace(&dev_nums_idr, chip, chip->dev_num);
  249. mutex_unlock(&idr_lock);
  250. return rc;
  251. }
  252. static void tpm_del_char_device(struct tpm_chip *chip)
  253. {
  254. cdev_del(&chip->cdev);
  255. device_del(&chip->dev);
  256. /* Make the chip unavailable. */
  257. mutex_lock(&idr_lock);
  258. idr_replace(&dev_nums_idr, NULL, chip->dev_num);
  259. mutex_unlock(&idr_lock);
  260. /* Make the driver uncallable. */
  261. down_write(&chip->ops_sem);
  262. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  263. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  264. chip->ops = NULL;
  265. up_write(&chip->ops_sem);
  266. }
  267. static int tpm1_chip_register(struct tpm_chip *chip)
  268. {
  269. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  270. return 0;
  271. tpm_sysfs_add_device(chip);
  272. chip->bios_dir = tpm_bios_log_setup(dev_name(&chip->dev));
  273. return 0;
  274. }
  275. static void tpm1_chip_unregister(struct tpm_chip *chip)
  276. {
  277. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  278. return;
  279. if (chip->bios_dir)
  280. tpm_bios_log_teardown(chip->bios_dir);
  281. }
  282. static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
  283. {
  284. struct attribute **i;
  285. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  286. return;
  287. sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
  288. for (i = chip->groups[0]->attrs; *i != NULL; ++i)
  289. sysfs_remove_link(&chip->dev.parent->kobj, (*i)->name);
  290. }
  291. /* For compatibility with legacy sysfs paths we provide symlinks from the
  292. * parent dev directory to selected names within the tpm chip directory. Old
  293. * kernel versions created these files directly under the parent.
  294. */
  295. static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
  296. {
  297. struct attribute **i;
  298. int rc;
  299. if (chip->flags & (TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_VIRTUAL))
  300. return 0;
  301. rc = __compat_only_sysfs_link_entry_to_kobj(
  302. &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
  303. if (rc && rc != -ENOENT)
  304. return rc;
  305. /* All the names from tpm-sysfs */
  306. for (i = chip->groups[0]->attrs; *i != NULL; ++i) {
  307. rc = __compat_only_sysfs_link_entry_to_kobj(
  308. &chip->dev.parent->kobj, &chip->dev.kobj, (*i)->name);
  309. if (rc) {
  310. tpm_del_legacy_sysfs(chip);
  311. return rc;
  312. }
  313. }
  314. return 0;
  315. }
  316. /*
  317. * tpm_chip_register() - create a character device for the TPM chip
  318. * @chip: TPM chip to use.
  319. *
  320. * Creates a character device for the TPM chip and adds sysfs attributes for
  321. * the device. As the last step this function adds the chip to the list of TPM
  322. * chips available for in-kernel use.
  323. *
  324. * This function should be only called after the chip initialization is
  325. * complete.
  326. */
  327. int tpm_chip_register(struct tpm_chip *chip)
  328. {
  329. #ifdef CONFIG_OF
  330. struct device_node *np;
  331. #endif
  332. int rc;
  333. #ifdef CONFIG_OF
  334. np = of_find_node_by_name(NULL, "vtpm");
  335. if (np) {
  336. if (of_property_read_bool(np, "powered-while-suspended"))
  337. chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
  338. }
  339. of_node_put(np);
  340. #endif
  341. if (chip->ops->flags & TPM_OPS_AUTO_STARTUP) {
  342. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  343. rc = tpm2_auto_startup(chip);
  344. else
  345. rc = tpm1_auto_startup(chip);
  346. if (rc)
  347. return rc;
  348. }
  349. rc = tpm1_chip_register(chip);
  350. if (rc)
  351. return rc;
  352. tpm_add_ppi(chip);
  353. rc = tpm_add_char_device(chip);
  354. if (rc) {
  355. tpm1_chip_unregister(chip);
  356. return rc;
  357. }
  358. chip->flags |= TPM_CHIP_FLAG_REGISTERED;
  359. rc = tpm_add_legacy_sysfs(chip);
  360. if (rc) {
  361. tpm_chip_unregister(chip);
  362. return rc;
  363. }
  364. return 0;
  365. }
  366. EXPORT_SYMBOL_GPL(tpm_chip_register);
  367. /*
  368. * tpm_chip_unregister() - release the TPM driver
  369. * @chip: TPM chip to use.
  370. *
  371. * Takes the chip first away from the list of available TPM chips and then
  372. * cleans up all the resources reserved by tpm_chip_register().
  373. *
  374. * Once this function returns the driver call backs in 'op's will not be
  375. * running and will no longer start.
  376. *
  377. * NOTE: This function should be only called before deinitializing chip
  378. * resources.
  379. */
  380. void tpm_chip_unregister(struct tpm_chip *chip)
  381. {
  382. if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
  383. return;
  384. tpm_del_legacy_sysfs(chip);
  385. tpm1_chip_unregister(chip);
  386. tpm_del_char_device(chip);
  387. }
  388. EXPORT_SYMBOL_GPL(tpm_chip_unregister);