tpm_tis.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 "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 bool interrupts = true;
  50. module_param(interrupts, bool, 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. #if defined(CONFIG_PNP) && defined(CONFIG_ACPI)
  61. static int has_hid(struct acpi_device *dev, const char *hid)
  62. {
  63. struct acpi_hardware_id *id;
  64. list_for_each_entry(id, &dev->pnp.ids, list)
  65. if (!strcmp(hid, id->id))
  66. return 1;
  67. return 0;
  68. }
  69. static inline int is_itpm(struct acpi_device *dev)
  70. {
  71. return has_hid(dev, "INTC0102");
  72. }
  73. #else
  74. static inline int is_itpm(struct acpi_device *dev)
  75. {
  76. return 0;
  77. }
  78. #endif
  79. static int tpm_tcg_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  80. u8 *result)
  81. {
  82. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  83. while (len--)
  84. *result++ = ioread8(phy->iobase + addr);
  85. return 0;
  86. }
  87. static int tpm_tcg_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
  88. u8 *value)
  89. {
  90. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  91. while (len--)
  92. iowrite8(*value++, phy->iobase + addr);
  93. return 0;
  94. }
  95. static int tpm_tcg_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
  96. {
  97. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  98. *result = ioread16(phy->iobase + addr);
  99. return 0;
  100. }
  101. static int tpm_tcg_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
  102. {
  103. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  104. *result = ioread32(phy->iobase + addr);
  105. return 0;
  106. }
  107. static int tpm_tcg_write32(struct tpm_tis_data *data, u32 addr, u32 value)
  108. {
  109. struct tpm_tis_tcg_phy *phy = to_tpm_tis_tcg_phy(data);
  110. iowrite32(value, phy->iobase + addr);
  111. return 0;
  112. }
  113. static const struct tpm_tis_phy_ops tpm_tcg = {
  114. .read_bytes = tpm_tcg_read_bytes,
  115. .write_bytes = tpm_tcg_write_bytes,
  116. .read16 = tpm_tcg_read16,
  117. .read32 = tpm_tcg_read32,
  118. .write32 = tpm_tcg_write32,
  119. };
  120. static int tpm_tis_init(struct device *dev, struct tpm_info *tpm_info,
  121. acpi_handle acpi_dev_handle)
  122. {
  123. struct tpm_tis_tcg_phy *phy;
  124. int irq = -1;
  125. phy = devm_kzalloc(dev, sizeof(struct tpm_tis_tcg_phy), GFP_KERNEL);
  126. if (phy == NULL)
  127. return -ENOMEM;
  128. phy->iobase = devm_ioremap_resource(dev, &tpm_info->res);
  129. if (IS_ERR(phy->iobase))
  130. return PTR_ERR(phy->iobase);
  131. if (interrupts)
  132. irq = tpm_info->irq;
  133. if (itpm)
  134. phy->priv.flags |= TPM_TIS_ITPM_POSSIBLE;
  135. return tpm_tis_core_init(dev, &phy->priv, irq, &tpm_tcg,
  136. acpi_dev_handle);
  137. }
  138. static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
  139. static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
  140. const struct pnp_device_id *pnp_id)
  141. {
  142. struct tpm_info tpm_info = {};
  143. acpi_handle acpi_dev_handle = NULL;
  144. struct resource *res;
  145. res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0);
  146. if (!res)
  147. return -ENODEV;
  148. tpm_info.res = *res;
  149. if (pnp_irq_valid(pnp_dev, 0))
  150. tpm_info.irq = pnp_irq(pnp_dev, 0);
  151. else
  152. tpm_info.irq = -1;
  153. if (pnp_acpi_device(pnp_dev)) {
  154. if (is_itpm(pnp_acpi_device(pnp_dev)))
  155. itpm = true;
  156. acpi_dev_handle = ACPI_HANDLE(&pnp_dev->dev);
  157. }
  158. return tpm_tis_init(&pnp_dev->dev, &tpm_info, acpi_dev_handle);
  159. }
  160. static struct pnp_device_id tpm_pnp_tbl[] = {
  161. {"PNP0C31", 0}, /* TPM */
  162. {"ATM1200", 0}, /* Atmel */
  163. {"IFX0102", 0}, /* Infineon */
  164. {"BCM0101", 0}, /* Broadcom */
  165. {"BCM0102", 0}, /* Broadcom */
  166. {"NSC1200", 0}, /* National */
  167. {"ICO0102", 0}, /* Intel */
  168. /* Add new here */
  169. {"", 0}, /* User Specified */
  170. {"", 0} /* Terminator */
  171. };
  172. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  173. static void tpm_tis_pnp_remove(struct pnp_dev *dev)
  174. {
  175. struct tpm_chip *chip = pnp_get_drvdata(dev);
  176. tpm_chip_unregister(chip);
  177. tpm_tis_remove(chip);
  178. }
  179. static struct pnp_driver tis_pnp_driver = {
  180. .name = "tpm_tis",
  181. .id_table = tpm_pnp_tbl,
  182. .probe = tpm_tis_pnp_init,
  183. .remove = tpm_tis_pnp_remove,
  184. .driver = {
  185. .pm = &tpm_tis_pm,
  186. },
  187. };
  188. #define TIS_HID_USR_IDX sizeof(tpm_pnp_tbl)/sizeof(struct pnp_device_id) -2
  189. module_param_string(hid, tpm_pnp_tbl[TIS_HID_USR_IDX].id,
  190. sizeof(tpm_pnp_tbl[TIS_HID_USR_IDX].id), 0444);
  191. MODULE_PARM_DESC(hid, "Set additional specific HID for this driver to probe");
  192. #ifdef CONFIG_ACPI
  193. static int tpm_check_resource(struct acpi_resource *ares, void *data)
  194. {
  195. struct tpm_info *tpm_info = (struct tpm_info *) data;
  196. struct resource res;
  197. if (acpi_dev_resource_interrupt(ares, 0, &res))
  198. tpm_info->irq = res.start;
  199. else if (acpi_dev_resource_memory(ares, &res)) {
  200. tpm_info->res = res;
  201. tpm_info->res.name = NULL;
  202. }
  203. return 1;
  204. }
  205. static int tpm_tis_acpi_init(struct acpi_device *acpi_dev)
  206. {
  207. struct acpi_table_tpm2 *tbl;
  208. acpi_status st;
  209. struct list_head resources;
  210. struct tpm_info tpm_info = {};
  211. int ret;
  212. st = acpi_get_table(ACPI_SIG_TPM2, 1,
  213. (struct acpi_table_header **) &tbl);
  214. if (ACPI_FAILURE(st) || tbl->header.length < sizeof(*tbl)) {
  215. dev_err(&acpi_dev->dev,
  216. FW_BUG "failed to get TPM2 ACPI table\n");
  217. return -EINVAL;
  218. }
  219. if (tbl->start_method != ACPI_TPM2_MEMORY_MAPPED)
  220. return -ENODEV;
  221. INIT_LIST_HEAD(&resources);
  222. tpm_info.irq = -1;
  223. ret = acpi_dev_get_resources(acpi_dev, &resources, tpm_check_resource,
  224. &tpm_info);
  225. if (ret < 0)
  226. return ret;
  227. acpi_dev_free_resource_list(&resources);
  228. if (resource_type(&tpm_info.res) != IORESOURCE_MEM) {
  229. dev_err(&acpi_dev->dev,
  230. FW_BUG "TPM2 ACPI table does not define a memory resource\n");
  231. return -EINVAL;
  232. }
  233. if (is_itpm(acpi_dev))
  234. itpm = true;
  235. return tpm_tis_init(&acpi_dev->dev, &tpm_info, acpi_dev->handle);
  236. }
  237. static int tpm_tis_acpi_remove(struct acpi_device *dev)
  238. {
  239. struct tpm_chip *chip = dev_get_drvdata(&dev->dev);
  240. tpm_chip_unregister(chip);
  241. tpm_tis_remove(chip);
  242. return 0;
  243. }
  244. static struct acpi_device_id tpm_acpi_tbl[] = {
  245. {"MSFT0101", 0}, /* TPM 2.0 */
  246. /* Add new here */
  247. {"", 0}, /* User Specified */
  248. {"", 0} /* Terminator */
  249. };
  250. MODULE_DEVICE_TABLE(acpi, tpm_acpi_tbl);
  251. static struct acpi_driver tis_acpi_driver = {
  252. .name = "tpm_tis",
  253. .ids = tpm_acpi_tbl,
  254. .ops = {
  255. .add = tpm_tis_acpi_init,
  256. .remove = tpm_tis_acpi_remove,
  257. },
  258. .drv = {
  259. .pm = &tpm_tis_pm,
  260. },
  261. };
  262. #endif
  263. static struct platform_device *force_pdev;
  264. static int tpm_tis_plat_probe(struct platform_device *pdev)
  265. {
  266. struct tpm_info tpm_info = {};
  267. struct resource *res;
  268. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  269. if (res == NULL) {
  270. dev_err(&pdev->dev, "no memory resource defined\n");
  271. return -ENODEV;
  272. }
  273. tpm_info.res = *res;
  274. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  275. if (res) {
  276. tpm_info.irq = res->start;
  277. } else {
  278. if (pdev == force_pdev)
  279. tpm_info.irq = -1;
  280. else
  281. /* When forcing auto probe the IRQ */
  282. tpm_info.irq = 0;
  283. }
  284. return tpm_tis_init(&pdev->dev, &tpm_info, NULL);
  285. }
  286. static int tpm_tis_plat_remove(struct platform_device *pdev)
  287. {
  288. struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
  289. tpm_chip_unregister(chip);
  290. tpm_tis_remove(chip);
  291. return 0;
  292. }
  293. static struct platform_driver tis_drv = {
  294. .probe = tpm_tis_plat_probe,
  295. .remove = tpm_tis_plat_remove,
  296. .driver = {
  297. .name = "tpm_tis",
  298. .pm = &tpm_tis_pm,
  299. },
  300. };
  301. static int tpm_tis_force_device(void)
  302. {
  303. struct platform_device *pdev;
  304. static const struct resource x86_resources[] = {
  305. {
  306. .start = 0xFED40000,
  307. .end = 0xFED40000 + TIS_MEM_LEN - 1,
  308. .flags = IORESOURCE_MEM,
  309. },
  310. };
  311. if (!force)
  312. return 0;
  313. /* The driver core will match the name tpm_tis of the device to
  314. * the tpm_tis platform driver and complete the setup via
  315. * tpm_tis_plat_probe
  316. */
  317. pdev = platform_device_register_simple("tpm_tis", -1, x86_resources,
  318. ARRAY_SIZE(x86_resources));
  319. if (IS_ERR(pdev))
  320. return PTR_ERR(pdev);
  321. force_pdev = pdev;
  322. return 0;
  323. }
  324. static int __init init_tis(void)
  325. {
  326. int rc;
  327. rc = tpm_tis_force_device();
  328. if (rc)
  329. goto err_force;
  330. rc = platform_driver_register(&tis_drv);
  331. if (rc)
  332. goto err_platform;
  333. #ifdef CONFIG_ACPI
  334. rc = acpi_bus_register_driver(&tis_acpi_driver);
  335. if (rc)
  336. goto err_acpi;
  337. #endif
  338. if (IS_ENABLED(CONFIG_PNP)) {
  339. rc = pnp_register_driver(&tis_pnp_driver);
  340. if (rc)
  341. goto err_pnp;
  342. }
  343. return 0;
  344. err_pnp:
  345. #ifdef CONFIG_ACPI
  346. acpi_bus_unregister_driver(&tis_acpi_driver);
  347. err_acpi:
  348. #endif
  349. platform_driver_unregister(&tis_drv);
  350. err_platform:
  351. if (force_pdev)
  352. platform_device_unregister(force_pdev);
  353. err_force:
  354. return rc;
  355. }
  356. static void __exit cleanup_tis(void)
  357. {
  358. pnp_unregister_driver(&tis_pnp_driver);
  359. #ifdef CONFIG_ACPI
  360. acpi_bus_unregister_driver(&tis_acpi_driver);
  361. #endif
  362. platform_driver_unregister(&tis_drv);
  363. if (force_pdev)
  364. platform_device_unregister(force_pdev);
  365. }
  366. module_init(init_tis);
  367. module_exit(cleanup_tis);
  368. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  369. MODULE_DESCRIPTION("TPM Driver");
  370. MODULE_VERSION("2.0");
  371. MODULE_LICENSE("GPL");