tpm_tis.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 <linux/kernel.h>
  34. #include <linux/dmi.h>
  35. #include "tpm.h"
  36. #include "tpm_tis_core.h"
  37. struct tpm_info {
  38. struct resource res;
  39. /* irq > 0 means: use irq $irq;
  40. * irq = 0 means: autoprobe for an irq;
  41. * irq = -1 means: no irq support
  42. */
  43. int irq;
  44. };
  45. struct tpm_tis_tcg_phy {
  46. struct tpm_tis_data priv;
  47. void __iomem *iobase;
  48. };
  49. static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
  50. {
  51. return container_of(data, struct tpm_tis_tcg_phy, priv);
  52. }
  53. static int interrupts = -1;
  54. module_param(interrupts, int, 0444);
  55. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  56. static bool itpm;
  57. module_param(itpm, bool, 0444);
  58. MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
  59. static bool force;
  60. #ifdef CONFIG_X86
  61. module_param(force, bool, 0444);
  62. MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
  63. #endif
  64. static int tpm_tis_disable_irq(const struct dmi_system_id *d)
  65. {
  66. if (interrupts == -1) {
  67. pr_notice("tpm_tis: %s detected: disabling interrupts.\n", d->ident);
  68. interrupts = 0;
  69. }
  70. return 0;
  71. }
  72. static const struct dmi_system_id tpm_tis_dmi_table[] = {
  73. {
  74. .callback = tpm_tis_disable_irq,
  75. .ident = "ThinkPad T490s",
  76. .matches = {
  77. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  78. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T490s"),
  79. },
  80. },
  81. {}
  82. };
  83. #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
  84. static int has_hid(struct acpi_device *dev, const char *hid)
  85. {
  86. struct acpi_hardware_id *id;
  87. list_for_each_entry(id, &dev->pnp.ids, list)
  88. if (!strcmp(hid, id->id))
  89. return 1;
  90. return 0;
  91. }
  92. static inline int is_itpm(struct acpi_device *dev)
  93. {
  94. if (!dev)
  95. return 0;
  96. return has_hid(dev, "INTC0102");
  97. }
  98. #else
  99. static inline int is_itpm(struct acpi_device *dev)
  100. {
  101. return 0;
  102. }
  103. #endif
  104. #if defined(CONFIG_ACPI)
  105. #define DEVICE_IS_TPM2 1
  106. static const struct acpi_device_id tpm_acpi_tbl[] = {
  107. {"MSFT0101", DEVICE_IS_TPM2},
  108. {},
  109. };
  110. MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
  111. static int check_acpi_tpm2(struct device *dev)
  112. {
  113. const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
  114. struct acpi_table_tpm2 *tbl;
  115. acpi_status st;
  116. if (!aid || aid->driver_data != DEVICE_IS_TPM2)
  117. return 0;
  118. /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
  119. * table is mandatory
  120. */
  121. st =
  122. acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
  123. if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
  124. dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
  125. return -EINVAL;
  126. }
  127. /* The tpm2_crb driver handles this device */
  128. if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
  129. return -ENODEV;
  130. return 0;
  131. }
  132. #else
  133. static int check_acpi_tpm2(struct device *dev)
  134. {
  135. return 0;
  136. }
  137. #endif
  138. static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  139. u8 *result)
  140. {
  141. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  142. while (len--)
  143. *result++ = ioread8(phy->iobase + addr);
  144. return 0;
  145. }
  146. static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  147. const u8 *value)
  148. {
  149. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  150. while (len--)
  151. iowrite8(*value++, phy->iobase + addr);
  152. return 0;
  153. }
  154. static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
  155. {
  156. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  157. *result = ioread16(phy->iobase + addr);
  158. return 0;
  159. }
  160. static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
  161. {
  162. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  163. *result = ioread32(phy->iobase + addr);
  164. return 0;
  165. }
  166. static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
  167. {
  168. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  169. iowrite32(value, phy->iobase + addr);
  170. return 0;
  171. }
  172. static const struct tpm_tis_phy_ops tpm_tcg = {
  173. .read_bytes = tpm_tcg_read_bytes,
  174. .write_bytes = tpm_tcg_write_bytes,
  175. .read16 = tpm_tcg_read16,
  176. .read32 = tpm_tcg_read32,
  177. .write32 = tpm_tcg_write32,
  178. };
  179. static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
  180. {
  181. struct tpm_tis_tcg_phy *phy;
  182. int irq = -1;
  183. int rc;
  184. dmi_check_system(tpm_tis_dmi_table);
  185. rc = check_acpi_tpm2(dev);
  186. if (rc)
  187. return rc;
  188. phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
  189. if (phy == NULL)
  190. return -ENOMEM;
  191. phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
  192. if (IS_ERR(phy->iobase))
  193. return PTR_ERR(phy->iobase);
  194. if (interrupts)
  195. irq = tpm_info->irq;
  196. if (itpm || is_itpm(ACPI_COMPANION(dev)))
  197. phy->priv.flags |= TPM_TIS_ITPM_WORKAROUND;
  198. return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
  199. ACPI_HANDLE(dev));
  200. }
  201. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  202. static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  203. const struct pnp_device_id *pnp_id)
  204. {
  205. struct tpm_info tpm_info = {};
  206. struct resource *res;
  207. res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
  208. if (!res)
  209. return -ENODEV;
  210. tpm_info.res = *res;
  211. if (pnp_irq_valid(pnp_dev, 0))
  212. tpm_info.irq = pnp_irq(pnp_dev, 0);
  213. else
  214. tpm_info.irq = -1;
  215. return tpm_tis_init(&pnp_dev->dev, &tpm_info);
  216. }
  217. static struct pnp_device_id tpm_pnp_tbl[] = {
  218. {"PNP0C31", 0}, /* TPM */
  219. {"ATM1200", 0}, /* Atmel */
  220. {"IFX0102", 0}, /* Infineon */
  221. {"BCM0101", 0}, /* Broadcom */
  222. {"BCM0102", 0}, /* Broadcom */
  223. {"NSC1200", 0}, /* National */
  224. {"ICO0102", 0}, /* Intel */
  225. /* Add new here */
  226. {"", 0}, /* User Specified */
  227. {"", 0} /* Terminator */
  228. };
  229. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  230. static void tpm_tis_pnp_remove(struct pnp_dev *dev)
  231. {
  232. struct tpm_chip *chip = pnp_get_drvdata(dev);
  233. tpm_chip_unregister(chip);
  234. tpm_tis_remove(chip);
  235. }
  236. static struct pnp_driver tis_pnp_driver = {
  237. .name = "tpm_tis",
  238. .id_table = tpm_pnp_tbl,
  239. .probe = tpm_tis_pnp_init,
  240. .remove = tpm_tis_pnp_remove,
  241. .driver = {
  242. .pm = &tpm_tis_pm,
  243. },
  244. };
  245. #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
  246. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  247. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  248. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  249. static struct platform_device *force_pdev;
  250. static int tpm_tis_plat_probe(struct platform_device *pdev)
  251. {
  252. struct tpm_info tpm_info = {};
  253. struct resource *res;
  254. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  255. if (res == NULL) {
  256. dev_err(&pdev->dev, "no memory resource defined\n");
  257. return -ENODEV;
  258. }
  259. tpm_info.res = *res;
  260. tpm_info.irq = platform_get_irq(pdev, 0);
  261. if (tpm_info.irq <= 0) {
  262. if (pdev != force_pdev)
  263. tpm_info.irq = -1;
  264. else
  265. /* When forcing auto probe the IRQ */
  266. tpm_info.irq = 0;
  267. }
  268. return tpm_tis_init(&pdev->dev, &tpm_info);
  269. }
  270. static int tpm_tis_plat_remove(struct platform_device *pdev)
  271. {
  272. struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
  273. tpm_chip_unregister(chip);
  274. tpm_tis_remove(chip);
  275. return 0;
  276. }
  277. #ifdef CONFIG_OF
  278. static const struct of_device_id tis_of_platform_match[] = {
  279. {.compatible = "tcg,tpm-tis-mmio"},
  280. {},
  281. };
  282. MODULE_DEVICE_TABLE(of, tis_of_platform_match);
  283. #endif
  284. static struct platform_driver tis_drv = {
  285. .probe = tpm_tis_plat_probe,
  286. .remove = tpm_tis_plat_remove,
  287. .driver = {
  288. .name = "tpm_tis",
  289. .pm = &tpm_tis_pm,
  290. .of_match_table = of_match_ptr(tis_of_platform_match),
  291. .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
  292. },
  293. };
  294. static int tpm_tis_force_device(void)
  295. {
  296. struct platform_device *pdev;
  297. static const struct resource x86_resources[] = {
  298. {
  299. .start = 0xFED40000,
  300. .end = 0xFED40000 + TIS_MEM_LEN - 1,
  301. .flags = IORESOURCE_MEM,
  302. },
  303. };
  304. if (!force)
  305. return 0;
  306. /* The driver core will match the name tpm_tis of the device to
  307. * the tpm_tis platform driver and complete the setup via
  308. * tpm_tis_plat_probe
  309. */
  310. pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
  311. ARRAY_SIZE(x86_resources));
  312. if (IS_ERR(pdev))
  313. return PTR_ERR(pdev);
  314. force_pdev = pdev;
  315. return 0;
  316. }
  317. static int __init init_tis(void)
  318. {
  319. int rc;
  320. rc = tpm_tis_force_device();
  321. if (rc)
  322. goto err_force;
  323. rc = platform_driver_register(&tis_drv);
  324. if (rc)
  325. goto err_platform;
  326. if (IS_ENABLED(CONFIG_PNP)) {
  327. rc = pnp_register_driver(&tis_pnp_driver);
  328. if (rc)
  329. goto err_pnp;
  330. }
  331. return 0;
  332. err_pnp:
  333. platform_driver_unregister(&tis_drv);
  334. err_platform:
  335. if (force_pdev)
  336. platform_device_unregister(force_pdev);
  337. err_force:
  338. return rc;
  339. }
  340. static void __exit cleanup_tis(void)
  341. {
  342. pnp_unregister_driver(&tis_pnp_driver);
  343. platform_driver_unregister(&tis_drv);
  344. if (force_pdev)
  345. platform_device_unregister(force_pdev);
  346. }
  347. module_init(init_tis);
  348. module_exit(cleanup_tis);
  349. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  350. MODULE_DESCRIPTION("TPM Driver");
  351. MODULE_VERSION("2.0");
  352. MODULE_LICENSE("GPL");