tpm_tis.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005, 2006 IBM Corporation
  4. * Copyright (C) 2014, 2015 Intel Corporation
  5. *
  6. * Authors:
  7. * Leendert van Doorn <[email protected]>
  8. * Kylene Hall <[email protected]>
  9. *
  10. * Maintained by: <[email protected]>
  11. *
  12. * Device driver for TCG/TCPA TPM (trusted platform module).
  13. * Specifications at www.trustedcomputinggroup.org
  14. *
  15. * This device driver implements the TPM interface as defined in
  16. * the TCG TPM Interface Spec version 1.2, revision 1.0.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/pnp.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/wait.h>
  25. #include <linux/acpi.h>
  26. #include <linux/freezer.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/kernel.h>
  30. #include <linux/dmi.h>
  31. #include "tpm.h"
  32. #include "tpm_tis_core.h"
  33. struct tpm_info {
  34. struct resource res;
  35. /* irq > 0 means: use irq $irq;
  36. * irq = 0 means: autoprobe for an irq;
  37. * irq = -1 means: no irq support
  38. */
  39. int irq;
  40. };
  41. struct tpm_tis_tcg_phy {
  42. struct tpm_tis_data priv;
  43. void __iomem *iobase;
  44. };
  45. static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
  46. {
  47. return container_of(data, struct tpm_tis_tcg_phy, priv);
  48. }
  49. static int interrupts = -1;
  50. module_param(interrupts, int, 0444);
  51. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  52. static bool itpm;
  53. module_param(itpm, bool, 0444);
  54. MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
  55. static bool force;
  56. #ifdef CONFIG_X86
  57. module_param(force, bool, 0444);
  58. MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
  59. #endif
  60. static int tpm_tis_disable_irq(const struct dmi_system_id *d)
  61. {
  62. if (interrupts == -1) {
  63. pr_notice("tpm_tis: %s detected: disabling interrupts.\n", d->ident);
  64. interrupts = 0;
  65. }
  66. return 0;
  67. }
  68. static const struct dmi_system_id tpm_tis_dmi_table[] = {
  69. {
  70. .callback = tpm_tis_disable_irq,
  71. .ident = "ThinkPad T490s",
  72. .matches = {
  73. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  74. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T490s"),
  75. },
  76. },
  77. {
  78. .callback = tpm_tis_disable_irq,
  79. .ident = "ThinkStation P360 Tiny",
  80. .matches = {
  81. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  82. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkStation P360 Tiny"),
  83. },
  84. },
  85. {
  86. .callback = tpm_tis_disable_irq,
  87. .ident = "ThinkPad L490",
  88. .matches = {
  89. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  90. DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad L490"),
  91. },
  92. },
  93. {}
  94. };
  95. #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
  96. static int has_hid(struct acpi_device *dev, const char *hid)
  97. {
  98. struct acpi_hardware_id *id;
  99. list_for_each_entry(id, &dev->pnp.ids, list)
  100. if (!strcmp(hid, id->id))
  101. return 1;
  102. return 0;
  103. }
  104. static inline int is_itpm(struct acpi_device *dev)
  105. {
  106. if (!dev)
  107. return 0;
  108. return has_hid(dev, "INTC0102");
  109. }
  110. #else
  111. static inline int is_itpm(struct acpi_device *dev)
  112. {
  113. return 0;
  114. }
  115. #endif
  116. #if defined(CONFIG_ACPI)
  117. #define DEVICE_IS_TPM2 1
  118. static const struct acpi_device_id tpm_acpi_tbl[] = {
  119. {"MSFT0101", DEVICE_IS_TPM2},
  120. {},
  121. };
  122. MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
  123. static int check_acpi_tpm2(struct device *dev)
  124. {
  125. const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
  126. struct acpi_table_tpm2 *tbl;
  127. acpi_status st;
  128. int ret = 0;
  129. if (!aid || aid->driver_data != DEVICE_IS_TPM2)
  130. return 0;
  131. /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
  132. * table is mandatory
  133. */
  134. st = acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
  135. if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
  136. dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
  137. return -EINVAL;
  138. }
  139. /* The tpm2_crb driver handles this device */
  140. if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
  141. ret = -ENODEV;
  142. acpi_put_table((struct acpi_table_header *)tbl);
  143. return ret;
  144. }
  145. #else
  146. static int check_acpi_tpm2(struct device *dev)
  147. {
  148. return 0;
  149. }
  150. #endif
  151. static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  152. u8 *result, enum tpm_tis_io_mode io_mode)
  153. {
  154. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  155. __le16 result_le16;
  156. __le32 result_le32;
  157. switch (io_mode) {
  158. case TPM_TIS_PHYS_8:
  159. while (len--)
  160. *result++ = ioread8(phy->iobase + addr);
  161. break;
  162. case TPM_TIS_PHYS_16:
  163. result_le16 = cpu_to_le16(ioread16(phy->iobase + addr));
  164. memcpy(result, &result_le16, sizeof(u16));
  165. break;
  166. case TPM_TIS_PHYS_32:
  167. result_le32 = cpu_to_le32(ioread32(phy->iobase + addr));
  168. memcpy(result, &result_le32, sizeof(u32));
  169. break;
  170. }
  171. return 0;
  172. }
  173. static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  174. const u8 *value, enum tpm_tis_io_mode io_mode)
  175. {
  176. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  177. switch (io_mode) {
  178. case TPM_TIS_PHYS_8:
  179. while (len--)
  180. iowrite8(*value++, phy->iobase + addr);
  181. break;
  182. case TPM_TIS_PHYS_16:
  183. return -EINVAL;
  184. case TPM_TIS_PHYS_32:
  185. iowrite32(le32_to_cpu(*((__le32 *)value)), phy->iobase + addr);
  186. break;
  187. }
  188. return 0;
  189. }
  190. static const struct tpm_tis_phy_ops tpm_tcg = {
  191. .read_bytes = tpm_tcg_read_bytes,
  192. .write_bytes = tpm_tcg_write_bytes,
  193. };
  194. static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
  195. {
  196. struct tpm_tis_tcg_phy *phy;
  197. int irq = -1;
  198. int rc;
  199. dmi_check_system(tpm_tis_dmi_table);
  200. rc = check_acpi_tpm2(dev);
  201. if (rc)
  202. return rc;
  203. phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
  204. if (phy == NULL)
  205. return -ENOMEM;
  206. phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
  207. if (IS_ERR(phy->iobase))
  208. return PTR_ERR(phy->iobase);
  209. if (interrupts)
  210. irq = tpm_info->irq;
  211. if (itpm || is_itpm(ACPI_COMPANION(dev)))
  212. set_bit(TPM_TIS_ITPM_WORKAROUND, &phy->priv.flags);
  213. return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
  214. ACPI_HANDLE(dev));
  215. }
  216. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  217. static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  218. const struct pnp_device_id *pnp_id)
  219. {
  220. struct tpm_info tpm_info = {};
  221. struct resource *res;
  222. res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
  223. if (!res)
  224. return -ENODEV;
  225. tpm_info.res = *res;
  226. if (pnp_irq_valid(pnp_dev, 0))
  227. tpm_info.irq = pnp_irq(pnp_dev, 0);
  228. else
  229. tpm_info.irq = -1;
  230. return tpm_tis_init(&pnp_dev->dev, &tpm_info);
  231. }
  232. /*
  233. * There is a known bug caused by 93e1b7d42e1e ("[PATCH] tpm: add HID module
  234. * parameter"). This commit added IFX0102 device ID, which is also used by
  235. * tpm_infineon but ignored to add quirks to probe which driver ought to be
  236. * used.
  237. */
  238. static struct pnp_device_id tpm_pnp_tbl[] = {
  239. {"PNP0C31", 0}, /* TPM */
  240. {"ATM1200", 0}, /* Atmel */
  241. {"IFX0102", 0}, /* Infineon */
  242. {"BCM0101", 0}, /* Broadcom */
  243. {"BCM0102", 0}, /* Broadcom */
  244. {"NSC1200", 0}, /* National */
  245. {"ICO0102", 0}, /* Intel */
  246. /* Add new here */
  247. {"", 0}, /* User Specified */
  248. {"", 0} /* Terminator */
  249. };
  250. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  251. static void tpm_tis_pnp_remove(struct pnp_dev *dev)
  252. {
  253. struct tpm_chip *chip = pnp_get_drvdata(dev);
  254. tpm_chip_unregister(chip);
  255. tpm_tis_remove(chip);
  256. }
  257. static struct pnp_driver tis_pnp_driver = {
  258. .name = "tpm_tis",
  259. .id_table = tpm_pnp_tbl,
  260. .probe = tpm_tis_pnp_init,
  261. .remove = tpm_tis_pnp_remove,
  262. .driver = {
  263. .pm = &tpm_tis_pm,
  264. },
  265. };
  266. #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
  267. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  268. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  269. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  270. static struct platform_device *force_pdev;
  271. static int tpm_tis_plat_probe(struct platform_device *pdev)
  272. {
  273. struct tpm_info tpm_info = {};
  274. struct resource *res;
  275. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  276. if (res == NULL) {
  277. dev_err(&pdev->dev, "no memory resource defined\n");
  278. return -ENODEV;
  279. }
  280. tpm_info.res = *res;
  281. tpm_info.irq = platform_get_irq_optional(pdev, 0);
  282. if (tpm_info.irq <= 0) {
  283. if (pdev != force_pdev)
  284. tpm_info.irq = -1;
  285. else
  286. /* When forcing auto probe the IRQ */
  287. tpm_info.irq = 0;
  288. }
  289. return tpm_tis_init(&pdev->dev, &tpm_info);
  290. }
  291. static int tpm_tis_plat_remove(struct platform_device *pdev)
  292. {
  293. struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
  294. tpm_chip_unregister(chip);
  295. tpm_tis_remove(chip);
  296. return 0;
  297. }
  298. #ifdef CONFIG_OF
  299. static const struct of_device_id tis_of_platform_match[] = {
  300. {.compatible = "tcg,tpm-tis-mmio"},
  301. {},
  302. };
  303. MODULE_DEVICE_TABLE(of, tis_of_platform_match);
  304. #endif
  305. static struct platform_driver tis_drv = {
  306. .probe = tpm_tis_plat_probe,
  307. .remove = tpm_tis_plat_remove,
  308. .driver = {
  309. .name = "tpm_tis",
  310. .pm = &tpm_tis_pm,
  311. .of_match_table = of_match_ptr(tis_of_platform_match),
  312. .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
  313. },
  314. };
  315. static int tpm_tis_force_device(void)
  316. {
  317. struct platform_device *pdev;
  318. static const struct resource x86_resources[] = {
  319. DEFINE_RES_MEM(0xFED40000, TIS_MEM_LEN)
  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. if (IS_ENABLED(CONFIG_PNP)) {
  344. rc = pnp_register_driver(&tis_pnp_driver);
  345. if (rc)
  346. goto err_pnp;
  347. }
  348. return 0;
  349. err_pnp:
  350. platform_driver_unregister(&tis_drv);
  351. err_platform:
  352. if (force_pdev)
  353. platform_device_unregister(force_pdev);
  354. err_force:
  355. return rc;
  356. }
  357. static void __exit cleanup_tis(void)
  358. {
  359. pnp_unregister_driver(&tis_pnp_driver);
  360. platform_driver_unregister(&tis_drv);
  361. if (force_pdev)
  362. platform_device_unregister(force_pdev);
  363. }
  364. module_init(init_tis);
  365. module_exit(cleanup_tis);
  366. MODULE_AUTHOR("Leendert van Doorn ([email protected])");
  367. MODULE_DESCRIPTION("TPM Driver");
  368. MODULE_VERSION("2.0");
  369. MODULE_LICENSE("GPL");