tpm-chip.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. if (!try_module_get(chip->dev.parent->driver->owner))
  54. goto out_lock;
  55. return 0;
  56. out_lock:
  57. up_read(&chip->ops_sem);
  58. put_device(&chip->dev);
  59. return rc;
  60. }
  61. EXPORT_SYMBOL_GPL(tpm_try_get_ops);
  62. /**
  63. * tpm_put_ops() - Release a ref to the tpm_chip
  64. * @chip: Chip to put
  65. *
  66. * This is the opposite pair to tpm_try_get_ops(). After this returns chip may
  67. * be kfree'd.
  68. */
  69. void tpm_put_ops(struct tpm_chip *chip)
  70. {
  71. module_put(chip->dev.parent->driver->owner);
  72. up_read(&chip->ops_sem);
  73. put_device(&chip->dev);
  74. }
  75. EXPORT_SYMBOL_GPL(tpm_put_ops);
  76. /**
  77. * tpm_chip_find_get() - return tpm_chip for a given chip number
  78. * @chip_num: id to find
  79. *
  80. * The return'd chip has been tpm_try_get_ops'd and must be released via
  81. * tpm_put_ops
  82. */
  83. struct tpm_chip *tpm_chip_find_get(int chip_num)
  84. {
  85. struct tpm_chip *chip, *res = NULL;
  86. int chip_prev;
  87. mutex_lock(&idr_lock);
  88. if (chip_num == TPM_ANY_NUM) {
  89. chip_num = 0;
  90. do {
  91. chip_prev = chip_num;
  92. chip = idr_get_next(&dev_nums_idr, &chip_num);
  93. if (chip && !tpm_try_get_ops(chip)) {
  94. res = chip;
  95. break;
  96. }
  97. } while (chip_prev != chip_num);
  98. } else {
  99. chip = idr_find_slowpath(&dev_nums_idr, chip_num);
  100. if (chip && !tpm_try_get_ops(chip))
  101. res = chip;
  102. }
  103. mutex_unlock(&idr_lock);
  104. return res;
  105. }
  106. /**
  107. * tpm_dev_release() - free chip memory and the device number
  108. * @dev: the character device for the TPM chip
  109. *
  110. * This is used as the release function for the character device.
  111. */
  112. static void tpm_dev_release(struct device *dev)
  113. {
  114. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  115. mutex_lock(&idr_lock);
  116. idr_remove(&dev_nums_idr, chip->dev_num);
  117. mutex_unlock(&idr_lock);
  118. kfree(chip);
  119. }
  120. /**
  121. * tpm_class_shutdown() - prepare the TPM device for loss of power.
  122. * @dev: device to which the chip is associated.
  123. *
  124. * Issues a TPM2_Shutdown command prior to loss of power, as required by the
  125. * TPM 2.0 spec.
  126. * Then, calls bus- and device- specific shutdown code.
  127. *
  128. * XXX: This codepath relies on the fact that sysfs is not enabled for
  129. * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
  130. * has sysfs support enabled before TPM sysfs's implicit locking is fixed.
  131. */
  132. static int tpm_class_shutdown(struct device *dev)
  133. {
  134. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  135. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  136. down_write(&chip->ops_sem);
  137. tpm2_shutdown(chip, TPM2_SU_CLEAR);
  138. chip->ops = NULL;
  139. up_write(&chip->ops_sem);
  140. }
  141. /* Allow bus- and device-specific code to run. Note: since chip->ops
  142. * is NULL, more-specific shutdown code will not be able to issue TPM
  143. * commands.
  144. */
  145. if (dev->bus && dev->bus->shutdown)
  146. dev->bus->shutdown(dev);
  147. else if (dev->driver && dev->driver->shutdown)
  148. dev->driver->shutdown(dev);
  149. return 0;
  150. }
  151. /**
  152. * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
  153. * @dev: device to which the chip is associated
  154. * @ops: struct tpm_class_ops instance
  155. *
  156. * Allocates a new struct tpm_chip instance and assigns a free
  157. * device number for it. Caller does not have to worry about
  158. * freeing the allocated resources. When the devices is removed
  159. * devres calls tpmm_chip_remove() to do the job.
  160. */
  161. struct tpm_chip *tpmm_chip_alloc(struct device *dev,
  162. const struct tpm_class_ops *ops)
  163. {
  164. struct tpm_chip *chip;
  165. int rc;
  166. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  167. if (chip == NULL)
  168. return ERR_PTR(-ENOMEM);
  169. mutex_init(&chip->tpm_mutex);
  170. init_rwsem(&chip->ops_sem);
  171. chip->ops = ops;
  172. mutex_lock(&idr_lock);
  173. rc = idr_alloc(&dev_nums_idr, NULL, 0, TPM_NUM_DEVICES, GFP_KERNEL);
  174. mutex_unlock(&idr_lock);
  175. if (rc < 0) {
  176. dev_err(dev, "No available tpm device numbers\n");
  177. kfree(chip);
  178. return ERR_PTR(rc);
  179. }
  180. chip->dev_num = rc;
  181. scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
  182. dev_set_drvdata(dev, chip);
  183. chip->dev.class = tpm_class;
  184. chip->dev.class->shutdown = tpm_class_shutdown;
  185. chip->dev.release = tpm_dev_release;
  186. chip->dev.parent = dev;
  187. #ifdef CONFIG_ACPI
  188. chip->dev.groups = chip->groups;
  189. #endif
  190. if (chip->dev_num == 0)
  191. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  192. else
  193. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  194. dev_set_name(&chip->dev, "%s", chip->devname);
  195. device_initialize(&chip->dev);
  196. cdev_init(&chip->cdev, &tpm_fops);
  197. chip->cdev.owner = dev->driver->owner;
  198. chip->cdev.kobj.parent = &chip->dev.kobj;
  199. rc = devm_add_action(dev, (void (*)(void *)) put_device, &chip->dev);
  200. if (rc) {
  201. put_device(&chip->dev);
  202. return ERR_PTR(rc);
  203. }
  204. return chip;
  205. }
  206. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  207. static int tpm_add_char_device(struct tpm_chip *chip)
  208. {
  209. int rc;
  210. rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
  211. if (rc) {
  212. dev_err(&chip->dev,
  213. "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
  214. chip->devname, MAJOR(chip->dev.devt),
  215. MINOR(chip->dev.devt), rc);
  216. return rc;
  217. }
  218. rc = device_add(&chip->dev);
  219. if (rc) {
  220. dev_err(&chip->dev,
  221. "unable to device_register() %s, major %d, minor %d, err=%d\n",
  222. chip->devname, MAJOR(chip->dev.devt),
  223. MINOR(chip->dev.devt), rc);
  224. cdev_del(&chip->cdev);
  225. return rc;
  226. }
  227. /* Make the chip available. */
  228. mutex_lock(&idr_lock);
  229. idr_replace(&dev_nums_idr, chip, chip->dev_num);
  230. mutex_unlock(&idr_lock);
  231. return rc;
  232. }
  233. static void tpm_del_char_device(struct tpm_chip *chip)
  234. {
  235. cdev_del(&chip->cdev);
  236. device_del(&chip->dev);
  237. /* Make the chip unavailable. */
  238. mutex_lock(&idr_lock);
  239. idr_replace(&dev_nums_idr, NULL, chip->dev_num);
  240. mutex_unlock(&idr_lock);
  241. /* Make the driver uncallable. */
  242. down_write(&chip->ops_sem);
  243. chip->ops = NULL;
  244. up_write(&chip->ops_sem);
  245. }
  246. static int tpm1_chip_register(struct tpm_chip *chip)
  247. {
  248. int rc;
  249. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  250. return 0;
  251. rc = tpm_sysfs_add_device(chip);
  252. if (rc)
  253. return rc;
  254. chip->bios_dir = tpm_bios_log_setup(chip->devname);
  255. return 0;
  256. }
  257. static void tpm1_chip_unregister(struct tpm_chip *chip)
  258. {
  259. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  260. return;
  261. if (chip->bios_dir)
  262. tpm_bios_log_teardown(chip->bios_dir);
  263. tpm_sysfs_del_device(chip);
  264. }
  265. /*
  266. * tpm_chip_register() - create a character device for the TPM chip
  267. * @chip: TPM chip to use.
  268. *
  269. * Creates a character device for the TPM chip and adds sysfs attributes for
  270. * the device. As the last step this function adds the chip to the list of TPM
  271. * chips available for in-kernel use.
  272. *
  273. * This function should be only called after the chip initialization is
  274. * complete.
  275. */
  276. int tpm_chip_register(struct tpm_chip *chip)
  277. {
  278. #ifdef CONFIG_OF
  279. struct device_node *np;
  280. #endif
  281. int rc;
  282. #ifdef CONFIG_OF
  283. np = of_find_node_by_name(NULL, "vtpm");
  284. if (np) {
  285. if (of_property_read_bool(np, "powered-while-suspended"))
  286. chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
  287. }
  288. of_node_put(np);
  289. #endif
  290. rc = tpm1_chip_register(chip);
  291. if (rc)
  292. return rc;
  293. tpm_add_ppi(chip);
  294. rc = tpm_add_char_device(chip);
  295. if (rc)
  296. goto out_err;
  297. chip->flags |= TPM_CHIP_FLAG_REGISTERED;
  298. if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) {
  299. rc = __compat_only_sysfs_link_entry_to_kobj(
  300. &chip->dev.parent->kobj, &chip->dev.kobj, "ppi");
  301. if (rc && rc != -ENOENT) {
  302. tpm_chip_unregister(chip);
  303. return rc;
  304. }
  305. }
  306. return 0;
  307. out_err:
  308. tpm1_chip_unregister(chip);
  309. return rc;
  310. }
  311. EXPORT_SYMBOL_GPL(tpm_chip_register);
  312. /*
  313. * tpm_chip_unregister() - release the TPM driver
  314. * @chip: TPM chip to use.
  315. *
  316. * Takes the chip first away from the list of available TPM chips and then
  317. * cleans up all the resources reserved by tpm_chip_register().
  318. *
  319. * Once this function returns the driver call backs in 'op's will not be
  320. * running and will no longer start.
  321. *
  322. * NOTE: This function should be only called before deinitializing chip
  323. * resources.
  324. */
  325. void tpm_chip_unregister(struct tpm_chip *chip)
  326. {
  327. if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
  328. return;
  329. if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
  330. sysfs_remove_link(&chip->dev.parent->kobj, "ppi");
  331. tpm1_chip_unregister(chip);
  332. tpm_del_char_device(chip);
  333. }
  334. EXPORT_SYMBOL_GPL(tpm_chip_unregister);