tpm_tis.c 9.0 KB

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