tpm_tis.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 <leendert@watson.ibm.com>
  8. * Kylene Hall <kjhall@us.ibm.com>
  9. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  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/kernel.h>
  29. #include "tpm.h"
  30. #include "tpm_tis_core.h"
  31. struct tpm_info {
  32. struct resource res;
  33. /* irq > 0 means: use irq $irq;
  34. * irq = 0 means: autoprobe for an irq;
  35. * irq = -1 means: no irq support
  36. */
  37. int irq;
  38. };
  39. struct tpm_tis_tcg_phy {
  40. struct tpm_tis_data priv;
  41. void __iomem *iobase;
  42. };
  43. static inline struct tpm_tis_tcg_phy *to_tpm_tis_tcg_phy(struct tpm_tis_data *data)
  44. {
  45. return container_of(data, struct tpm_tis_tcg_phy, priv);
  46. }
  47. #ifdef CONFIG_PREEMPT_RT
  48. /*
  49. * Flush previous write operations with a dummy read operation to the
  50. * TPM MMIO base address.
  51. */
  52. static inline void tpm_tis_flush(void __iomem *iobase)
  53. {
  54. ioread8(iobase + TPM_ACCESS(0));
  55. }
  56. #else
  57. #define tpm_tis_flush(iobase) do { } while (0)
  58. #endif
  59. /*
  60. * Write a byte word to the TPM MMIO address, and flush the write queue.
  61. * The flush ensures that the data is sent immediately over the bus and not
  62. * aggregated with further requests and transferred later in a batch. The large
  63. * write requests can lead to unwanted latency spikes by blocking the CPU until
  64. * the complete batch has been transferred.
  65. */
  66. static inline void tpm_tis_iowrite8(u8 b, void __iomem *iobase, u32 addr)
  67. {
  68. iowrite8(b, iobase + addr);
  69. tpm_tis_flush(iobase);
  70. }
  71. /*
  72. * Write a 32-bit word to the TPM MMIO address, and flush the write queue.
  73. * The flush ensures that the data is sent immediately over the bus and not
  74. * aggregated with further requests and transferred later in a batch. The large
  75. * write requests can lead to unwanted latency spikes by blocking the CPU until
  76. * the complete batch has been transferred.
  77. */
  78. static inline void tpm_tis_iowrite32(u32 b, void __iomem *iobase, u32 addr)
  79. {
  80. iowrite32(b, iobase + addr);
  81. tpm_tis_flush(iobase);
  82. }
  83. static bool interrupts;
  84. module_param(interrupts, bool, 0444);
  85. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  86. static bool itpm;
  87. module_param(itpm, bool, 0444);
  88. MODULE_PARM_DESC(itpm, "Force iTPM workarounds (found on some Lenovo laptops)");
  89. static bool force;
  90. #ifdef CONFIG_X86
  91. module_param(force, bool, 0444);
  92. MODULE_PARM_DESC(force, "Force device probe rather than using ACPI entry");
  93. #endif
  94. #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
  95. static int has_hid(struct acpi_device *dev, const char *hid)
  96. {
  97. struct acpi_hardware_id *id;
  98. list_for_each_entry(id, &dev->pnp.ids, list)
  99. if (!strcmp(hid, id->id))
  100. return 1;
  101. return 0;
  102. }
  103. static inline int is_itpm(struct acpi_device *dev)
  104. {
  105. if (!dev)
  106. return 0;
  107. return has_hid(dev, "INTC0102");
  108. }
  109. #else
  110. static inline int is_itpm(struct acpi_device *dev)
  111. {
  112. return 0;
  113. }
  114. #endif
  115. #if defined(CONFIG_ACPI)
  116. #define DEVICE_IS_TPM2 1
  117. static const struct acpi_device_id tpm_acpi_tbl[] = {
  118. {"MSFT0101", DEVICE_IS_TPM2},
  119. {},
  120. };
  121. MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
  122. static int check_acpi_tpm2(struct device *dev)
  123. {
  124. const struct acpi_device_id *aid = acpi_match_device(tpm_acpi_tbl, dev);
  125. struct acpi_table_tpm2 *tbl;
  126. acpi_status st;
  127. int ret = 0;
  128. if (!aid || aid->driver_data != DEVICE_IS_TPM2)
  129. return 0;
  130. /* If the ACPI TPM2 signature is matched then a global ACPI_SIG_TPM2
  131. * table is mandatory
  132. */
  133. st = acpi_get_table(ACPI_SIG_TPM2, 1, (struct acpi_table_header **)&tbl);
  134. if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
  135. dev_err(dev, FW_BUG "failed to get TPM2 ACPI table\n");
  136. return -EINVAL;
  137. }
  138. /* The tpm2_crb driver handles this device */
  139. if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
  140. ret = -ENODEV;
  141. acpi_put_table((struct acpi_table_header *)tbl);
  142. return ret;
  143. }
  144. #else
  145. static int check_acpi_tpm2(struct device *dev)
  146. {
  147. return 0;
  148. }
  149. #endif
  150. static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  151. u8 *result, enum tpm_tis_io_mode io_mode)
  152. {
  153. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  154. __le16 result_le16;
  155. __le32 result_le32;
  156. switch (io_mode) {
  157. case TPM_TIS_PHYS_8:
  158. while (len--)
  159. *result++ = ioread8(phy->iobase + addr);
  160. break;
  161. case TPM_TIS_PHYS_16:
  162. result_le16 = cpu_to_le16(ioread16(phy->iobase + addr));
  163. memcpy(result, &result_le16, sizeof(u16));
  164. break;
  165. case TPM_TIS_PHYS_32:
  166. result_le32 = cpu_to_le32(ioread32(phy->iobase + addr));
  167. memcpy(result, &result_le32, sizeof(u32));
  168. break;
  169. }
  170. return 0;
  171. }
  172. static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  173. const u8 *value, enum tpm_tis_io_mode io_mode)
  174. {
  175. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  176. switch (io_mode) {
  177. case TPM_TIS_PHYS_8:
  178. while (len--)
  179. tpm_tis_iowrite8(*value++, phy->iobase, addr);
  180. break;
  181. case TPM_TIS_PHYS_16:
  182. return -EINVAL;
  183. case TPM_TIS_PHYS_32:
  184. tpm_tis_iowrite32(le32_to_cpu(*((__le32 *)value)), phy->iobase, addr);
  185. break;
  186. }
  187. return 0;
  188. }
  189. static const struct tpm_tis_phy_ops tpm_tcg = {
  190. .read_bytes = tpm_tcg_read_bytes,
  191. .write_bytes = tpm_tcg_write_bytes,
  192. };
  193. static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info)
  194. {
  195. struct tpm_tis_tcg_phy *phy;
  196. int irq = -1;
  197. int rc;
  198. rc = check_acpi_tpm2(dev);
  199. if (rc)
  200. return rc;
  201. phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
  202. if (phy == NULL)
  203. return -ENOMEM;
  204. phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
  205. if (IS_ERR(phy->iobase))
  206. return PTR_ERR(phy->iobase);
  207. if (interrupts)
  208. irq = tpm_info->irq;
  209. if (itpm || is_itpm(ACPI_COMPANION(dev)))
  210. set_bit(TPM_TIS_ITPM_WORKAROUND, &phy->priv.flags);
  211. return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
  212. ACPI_HANDLE(dev));
  213. }
  214. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  215. static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  216. const struct pnp_device_id *pnp_id)
  217. {
  218. struct tpm_info tpm_info = {};
  219. struct resource *res;
  220. res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
  221. if (!res)
  222. return -ENODEV;
  223. tpm_info.res = *res;
  224. if (pnp_irq_valid(pnp_dev, 0))
  225. tpm_info.irq = pnp_irq(pnp_dev, 0);
  226. else
  227. tpm_info.irq = -1;
  228. return tpm_tis_init(&pnp_dev->dev, &tpm_info);
  229. }
  230. /*
  231. * There is a known bug caused by 93e1b7d42e1e ("[PATCH] tpm: add HID module
  232. * parameter"). This commit added IFX0102 device ID, which is also used by
  233. * tpm_infineon but ignored to add quirks to probe which driver ought to be
  234. * used.
  235. */
  236. static struct pnp_device_id tpm_pnp_tbl[] = {
  237. {"PNP0C31", 0}, /* TPM */
  238. {"ATM1200", 0}, /* Atmel */
  239. {"IFX0102", 0}, /* Infineon */
  240. {"BCM0101", 0}, /* Broadcom */
  241. {"BCM0102", 0}, /* Broadcom */
  242. {"NSC1200", 0}, /* National */
  243. {"ICO0102", 0}, /* Intel */
  244. /* Add new here */
  245. {"", 0}, /* User Specified */
  246. {"", 0} /* Terminator */
  247. };
  248. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  249. static void tpm_tis_pnp_remove(struct pnp_dev *dev)
  250. {
  251. struct tpm_chip *chip = pnp_get_drvdata(dev);
  252. tpm_chip_unregister(chip);
  253. tpm_tis_remove(chip);
  254. }
  255. static struct pnp_driver tis_pnp_driver = {
  256. .name = "tpm_tis",
  257. .id_table = tpm_pnp_tbl,
  258. .probe = tpm_tis_pnp_init,
  259. .remove = tpm_tis_pnp_remove,
  260. .driver = {
  261. .pm = &tpm_tis_pm,
  262. },
  263. };
  264. #define TIS_HID_USR_IDX (ARRAY_SIZE(tpm_pnp_tbl) - 2)
  265. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  266. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  267. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  268. static struct platform_device *force_pdev;
  269. static int tpm_tis_plat_probe(struct platform_device *pdev)
  270. {
  271. struct tpm_info tpm_info = {};
  272. struct resource *res;
  273. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  274. if (res == NULL) {
  275. dev_err(&pdev->dev, "no memory resource defined\n");
  276. return -ENODEV;
  277. }
  278. tpm_info.res = *res;
  279. tpm_info.irq = platform_get_irq_optional(pdev, 0);
  280. if (tpm_info.irq <= 0) {
  281. if (pdev != force_pdev)
  282. tpm_info.irq = -1;
  283. else
  284. /* When forcing auto probe the IRQ */
  285. tpm_info.irq = 0;
  286. }
  287. return tpm_tis_init(&pdev->dev, &tpm_info);
  288. }
  289. static void tpm_tis_plat_remove(struct platform_device *pdev)
  290. {
  291. struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
  292. tpm_chip_unregister(chip);
  293. tpm_tis_remove(chip);
  294. }
  295. #ifdef CONFIG_OF
  296. static const struct of_device_id tis_of_platform_match[] = {
  297. {.compatible = "atmel,at97sc3204"},
  298. {.compatible = "tcg,tpm-tis-mmio"},
  299. {},
  300. };
  301. MODULE_DEVICE_TABLE(of, tis_of_platform_match);
  302. #endif
  303. static struct platform_driver tis_drv = {
  304. .probe = tpm_tis_plat_probe,
  305. .remove_new = tpm_tis_plat_remove,
  306. .driver = {
  307. .name = "tpm_tis",
  308. .pm = &tpm_tis_pm,
  309. .of_match_table = of_match_ptr(tis_of_platform_match),
  310. .acpi_match_table = ACPI_PTR(tpm_acpi_tbl),
  311. },
  312. };
  313. static int tpm_tis_force_device(void)
  314. {
  315. struct platform_device *pdev;
  316. static const struct resource x86_resources[] = {
  317. DEFINE_RES_MEM(0xFED40000, TIS_MEM_LEN)
  318. };
  319. if (!force)
  320. return 0;
  321. /* The driver core will match the name tpm_tis of the device to
  322. * the tpm_tis platform driver and complete the setup via
  323. * tpm_tis_plat_probe
  324. */
  325. pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
  326. ARRAY_SIZE(x86_resources));
  327. if (IS_ERR(pdev))
  328. return PTR_ERR(pdev);
  329. force_pdev = pdev;
  330. return 0;
  331. }
  332. static int __init init_tis(void)
  333. {
  334. int rc;
  335. rc = tpm_tis_force_device();
  336. if (rc)
  337. goto err_force;
  338. rc = platform_driver_register(&tis_drv);
  339. if (rc)
  340. goto err_platform;
  341. if (IS_ENABLED(CONFIG_PNP)) {
  342. rc = pnp_register_driver(&tis_pnp_driver);
  343. if (rc)
  344. goto err_pnp;
  345. }
  346. return 0;
  347. err_pnp:
  348. platform_driver_unregister(&tis_drv);
  349. err_platform:
  350. if (force_pdev)
  351. platform_device_unregister(force_pdev);
  352. err_force:
  353. return rc;
  354. }
  355. static void __exit cleanup_tis(void)
  356. {
  357. pnp_unregister_driver(&tis_pnp_driver);
  358. platform_driver_unregister(&tis_drv);
  359. if (force_pdev)
  360. platform_device_unregister(force_pdev);
  361. }
  362. module_init(init_tis);
  363. module_exit(cleanup_tis);
  364. MODULE_AUTHOR("Leendert van Doorn <leendert@watson.ibm.com>");
  365. MODULE_DESCRIPTION("TPM Driver");
  366. MODULE_VERSION("2.0");
  367. MODULE_LICENSE("GPL");