tpm_tis.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (C) 2005, 2006 IBM Corporation
  3. * Copyright (C) 2014, 2015 Intel Corporation
  4. *
  5. * Authors:
  6. * Leendert van Doorn <leendert@watson.ibm.com>
  7. * Kylene Hall <kjhall@us.ibm.com>
  8. *
  9. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  10. *
  11. * Device driver for TCG/TCPA TPM (trusted platform module).
  12. * Specifications at www.trustedcomputinggroup.org
  13. *
  14. * This device driver implements the TPM interface as defined in
  15. * the TCG TPM Interface Spec version 1.2, revision 1.0.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation, version 2 of the
  20. * License.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/pnp.h>
  26. #include <linux/slab.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/wait.h>
  29. #include <linux/acpi.h>
  30. #include <linux/freezer.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include "tpm.h"
  34. #include "tpm_tis_core.h"
  35. struct tpm_info {
  36. struct resource res;
  37. /* irq > 0 means: use irq $irq;
  38. * irq = 0 means: autoprobe for an irq;
  39. * irq = -1 means: no irq support
  40. */
  41. int irq;
  42. };
  43. struct tpm_tis_tcg_phy {
  44. struct tpm_tis_data priv;
  45. void __iomem *iobase;
  46. };
  47. static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
  48. {
  49. return container_of(data, struct tpm_tis_tcg_phy, priv);
  50. }
  51. static bool interrupts = true;
  52. module_param(interrupts, bool, 0444);
  53. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  54. static bool itpm;
  55. module_param(itpm, bool, 0444);
  56. MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
  57. static bool force;
  58. #ifdef CONFIG_X86
  59. module_param(force, bool, 0444);
  60. MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
  61. #endif
  62. #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
  63. static int has_hid(struct acpi_device *dev, const char *hid)
  64. {
  65. struct acpi_hardware_id *id;
  66. list_for_each_entry(id, &dev->pnp.ids, list)
  67. if (!strcmp(hid, id->id))
  68. return 1;
  69. return 0;
  70. }
  71. static inline int is_itpm(struct acpi_device *dev)
  72. {
  73. return has_hid(dev, "INTC0102");
  74. }
  75. #else
  76. static inline int is_itpm(struct acpi_device *dev)
  77. {
  78. return 0;
  79. }
  80. #endif
  81. static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  82. u8 *result)
  83. {
  84. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  85. while (len--)
  86. *result++ = ioread8(phy->iobase + addr);
  87. return 0;
  88. }
  89. static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  90. u8 *value)
  91. {
  92. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  93. while (len--)
  94. iowrite8(*value++, phy->iobase + addr);
  95. return 0;
  96. }
  97. static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
  98. {
  99. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  100. *result = ioread16(phy->iobase + addr);
  101. return 0;
  102. }
  103. static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
  104. {
  105. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  106. *result = ioread32(phy->iobase + addr);
  107. return 0;
  108. }
  109. static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
  110. {
  111. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  112. iowrite32(value, phy->iobase + addr);
  113. return 0;
  114. }
  115. static const struct tpm_tis_phy_ops tpm_tcg = {
  116. .read_bytes = tpm_tcg_read_bytes,
  117. .write_bytes = tpm_tcg_write_bytes,
  118. .read16 = tpm_tcg_read16,
  119. .read32 = tpm_tcg_read32,
  120. .write32 = tpm_tcg_write32,
  121. };
  122. static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info,
  123. acpi_handle acpi_dev_handle)
  124. {
  125. struct tpm_tis_tcg_phy *phy;
  126. int irq = -1;
  127. phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
  128. if (phy == NULL)
  129. return -ENOMEM;
  130. phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
  131. if (IS_ERR(phy->iobase))
  132. return PTR_ERR(phy->iobase);
  133. if (interrupts)
  134. irq = tpm_info->irq;
  135. if (itpm)
  136. phy->priv.flags |= TPM_TIS_ITPM_POSSIBLE;
  137. return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
  138. acpi_dev_handle);
  139. }
  140. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  141. static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  142. const struct pnp_device_id *pnp_id)
  143. {
  144. struct tpm_info tpm_info = {};
  145. acpi_handle acpi_dev_handle = NULL;
  146. struct resource *res;
  147. res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
  148. if (!res)
  149. return -ENODEV;
  150. tpm_info.res = *res;
  151. if (pnp_irq_valid(pnp_dev, 0))
  152. tpm_info.irq = pnp_irq(pnp_dev, 0);
  153. else
  154. tpm_info.irq = -1;
  155. if (pnp_acpi_device(pnp_dev)) {
  156. if (is_itpm(pnp_acpi_device(pnp_dev)))
  157. itpm = true;
  158. acpi_dev_handle = ACPI_HANDLE(&pnp_dev->dev);
  159. }
  160. return tpm_tis_init(&pnp_dev->dev, &tpm_info, acpi_dev_handle);
  161. }
  162. static struct pnp_device_id tpm_pnp_tbl[] = {
  163. {"PNP0C31", 0}, /* TPM */
  164. {"ATM1200", 0}, /* Atmel */
  165. {"IFX0102", 0}, /* Infineon */
  166. {"BCM0101", 0}, /* Broadcom */
  167. {"BCM0102", 0}, /* Broadcom */
  168. {"NSC1200", 0}, /* National */
  169. {"ICO0102", 0}, /* Intel */
  170. /* Add new here */
  171. {"", 0}, /* User Specified */
  172. {"", 0} /* Terminator */
  173. };
  174. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  175. static void tpm_tis_pnp_remove(struct pnp_dev *dev)
  176. {
  177. struct tpm_chip *chip = pnp_get_drvdata(dev);
  178. tpm_chip_unregister(chip);
  179. tpm_tis_remove(chip);
  180. }
  181. static struct pnp_driver tis_pnp_driver = {
  182. .name = "tpm_tis",
  183. .id_table = tpm_pnp_tbl,
  184. .probe = tpm_tis_pnp_init,
  185. .remove = tpm_tis_pnp_remove,
  186. .driver = {
  187. .pm = &tpm_tis_pm,
  188. },
  189. };
  190. #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
  191. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  192. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  193. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  194. #ifdef CONFIG_ACPI
  195. static int tpm_check_resource(struct acpi_resource *ares, void *data)
  196. {
  197. struct tpm_info *tpm_info = (struct tpm_info *) data;
  198. struct resource res;
  199. if (acpi_dev_resource_interrupt(ares, 0, &res))
  200. tpm_info->irq = res.start;
  201. else if (acpi_dev_resource_memory(ares, &res)) {
  202. tpm_info->res = res;
  203. tpm_info->res.name = NULL;
  204. }
  205. return 1;
  206. }
  207. static int tpm_tis_acpi_init(struct acpi_device *acpi_dev)
  208. {
  209. struct acpi_table_tpm2 *tbl;
  210. acpi_status st;
  211. struct list_head resources;
  212. struct tpm_info tpm_info = {};
  213. int ret;
  214. st = acpi_get_table(ACPI_SIG_TPM2, 1,
  215. (struct acpi_table_header **) &tbl);
  216. if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
  217. dev_err(&acpi_dev->dev,
  218. FW_BUG "failed to get TPM2 ACPI table\n");
  219. return -EINVAL;
  220. }
  221. if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
  222. return -ENODEV;
  223. INIT_LIST_HEAD(&resources);
  224. tpm_info.irq = -1;
  225. ret = acpi_dev_get_resources(acpi_dev, &resources, tpm_check_resource,
  226. &tpm_info);
  227. if (ret < 0)
  228. return ret;
  229. acpi_dev_free_resource_list(&resources);
  230. if (resource_type(&tpm_info.res) != IORESOURCE_MEM) {
  231. dev_err(&acpi_dev->dev,
  232. FW_BUG "TPM2 ACPI table does not define a memory resource\n");
  233. return -EINVAL;
  234. }
  235. if (is_itpm(acpi_dev))
  236. itpm = true;
  237. return tpm_tis_init(&acpi_dev->dev, &tpm_info, acpi_dev->handle);
  238. }
  239. static int tpm_tis_acpi_remove(struct acpi_device *dev)
  240. {
  241. struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
  242. tpm_chip_unregister(chip);
  243. tpm_tis_remove(chip);
  244. return 0;
  245. }
  246. static struct acpi_device_id tpm_acpi_tbl[] = {
  247. {"MSFT0101", 0}, /* TPM 2.0 */
  248. /* Add new here */
  249. {"", 0}, /* User Specified */
  250. {"", 0} /* Terminator */
  251. };
  252. MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
  253. static struct acpi_driver tis_acpi_driver = {
  254. .name = "tpm_tis",
  255. .ids = tpm_acpi_tbl,
  256. .ops = {
  257. .add = tpm_tis_acpi_init,
  258. .remove = tpm_tis_acpi_remove,
  259. },
  260. .drv = {
  261. .pm = &tpm_tis_pm,
  262. },
  263. };
  264. #endif
  265. static struct platform_device *force_pdev;
  266. static int tpm_tis_plat_probe(struct platform_device *pdev)
  267. {
  268. struct tpm_info tpm_info = {};
  269. struct resource *res;
  270. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  271. if (res == NULL) {
  272. dev_err(&pdev->dev, "no memory resource defined\n");
  273. return -ENODEV;
  274. }
  275. tpm_info.res = *res;
  276. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  277. if (res) {
  278. tpm_info.irq = res->start;
  279. } else {
  280. if (pdev == force_pdev)
  281. tpm_info.irq = -1;
  282. else
  283. /* When forcing auto probe the IRQ */
  284. tpm_info.irq = 0;
  285. }
  286. return tpm_tis_init(&pdev->dev, &tpm_info, NULL);
  287. }
  288. static int tpm_tis_plat_remove(struct platform_device *pdev)
  289. {
  290. struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
  291. tpm_chip_unregister(chip);
  292. tpm_tis_remove(chip);
  293. return 0;
  294. }
  295. #ifdef CONFIG_OF
  296. static const struct of_device_id tis_of_platform_match[] = {
  297. {.compatible = "tcg,tpm-tis-mmio"},
  298. {},
  299. };
  300. MODULE_DEVICE_TABLE(of, tis_of_platform_match);
  301. #endif
  302. static struct platform_driver tis_drv = {
  303. .probe = tpm_tis_plat_probe,
  304. .remove = tpm_tis_plat_remove,
  305. .driver = {
  306. .name = "tpm_tis",
  307. .pm = &tpm_tis_pm,
  308. .of_match_table = of_match_ptr(tis_of_platform_match),
  309. },
  310. };
  311. static int tpm_tis_force_device(void)
  312. {
  313. struct platform_device *pdev;
  314. static const struct resource x86_resources[] = {
  315. {
  316. .start = 0xFED40000,
  317. .end = 0xFED40000 + TIS_MEM_LEN - 1,
  318. .flags = IORESOURCE_MEM,
  319. },
  320. };
  321. if (!force)
  322. return 0;
  323. /* The driver core will match the name tpm_tis of the device to
  324. * the tpm_tis platform driver and complete the setup via
  325. * tpm_tis_plat_probe
  326. */
  327. pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
  328. ARRAY_SIZE(x86_resources));
  329. if (IS_ERR(pdev))
  330. return PTR_ERR(pdev);
  331. force_pdev = pdev;
  332. return 0;
  333. }
  334. static int __init init_tis(void)
  335. {
  336. int rc;
  337. rc = tpm_tis_force_device();
  338. if (rc)
  339. goto err_force;
  340. rc = platform_driver_register(&tis_drv);
  341. if (rc)
  342. goto err_platform;
  343. #ifdef CONFIG_ACPI
  344. rc = acpi_bus_register_driver(&tis_acpi_driver);
  345. if (rc)
  346. goto err_acpi;
  347. #endif
  348. if (IS_ENABLED(CONFIG_PNP)) {
  349. rc = pnp_register_driver(&tis_pnp_driver);
  350. if (rc)
  351. goto err_pnp;
  352. }
  353. return 0;
  354. err_pnp:
  355. #ifdef CONFIG_ACPI
  356. acpi_bus_unregister_driver(&tis_acpi_driver);
  357. err_acpi:
  358. #endif
  359. platform_device_unregister(force_pdev);
  360. err_platform:
  361. if (force_pdev)
  362. platform_device_unregister(force_pdev);
  363. err_force:
  364. return rc;
  365. }
  366. static void __exit cleanup_tis(void)
  367. {
  368. pnp_unregister_driver(&tis_pnp_driver);
  369. #ifdef CONFIG_ACPI
  370. acpi_bus_unregister_driver(&tis_acpi_driver);
  371. #endif
  372. platform_driver_unregister(&tis_drv);
  373. if (force_pdev)
  374. platform_device_unregister(force_pdev);
  375. }
  376. module_init(init_tis);
  377. module_exit(cleanup_tis);
  378. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  379. MODULE_DESCRIPTION("TPM Driver");
  380. MODULE_VERSION("2.0");
  381. MODULE_LICENSE("GPL");